codeparrot/xlcost-text-to-code huggingface dataset:
Program to check if all characters have even frequency | C ++ implementation of the above approach ; creating a frequency array ; Finding length of s ; counting frequency of all characters ; checking if any odd frequency is there or not ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool check ( string s ) { int freq [ 26 ] = { 0 } ; int n = s . length ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s [ i ] - 97 ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] % 2 == 1 ) return false ; return true ; } int main ( ) { string s = " abaccaba " ; check ( s ) ? cout << " Yes " << endl : cout << " No " << endl ; return 0 ; }
Maximum Consecutive Zeroes in Concatenated Binary String | C ++ program to find maximum number of consecutive zeroes after concatenating a binary string ; returns the maximum size of a substring consisting only of zeroes after k concatenation ; stores the maximum length of the required substring ; if the current character is 0 ; stores maximum length of current substrings with zeroes ; if the whole string is filled with zero ; computes the length of the maximal prefix which contains only zeroes ; computes the length of the maximal suffix which contains only zeroes ; if more than 1 concatenations are to be made ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_length_substring ( string st , int n , int k ) { int max_len = 0 ; int len = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( st [ i ] == '0' ) len ++ ; else len = 0 ; max_len = max ( max_len , len ) ; } if ( max_len == n ) return n * k ; int pref = 0 , suff = 0 ; for ( int i = 0 ; st [ i ] == '0' ; ++ i , ++ pref ) ; for ( int i = n - 1 ; st [ i ] == '0' ; -- i , ++ suff ) ; if ( k > 1 ) max_len = max ( max_len , pref + suff ) ; return max_len ; } int main ( ) { int n = 6 ; int k = 3 ; string st = "110010" ; int ans = max_length_substring ( st , n , k ) ; cout << ans ; }
Sub | C ++ implementation of the approach ; Function that checks if the string contain exactly K characters having ASCII value greater than p ; if ASCII value is greater than ' p ' ; if count of satisfying characters is equal to ' K ' then return true ; otherwise return false ; function to count sub - strings ; length of the string ; count of sub - strings ; ' i ' is the starting index for the sub - string ; ' j ' is the no . of characters to include in the sub - string ; check if the sub - string satisfies the condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValidSubString ( string r , int K , int p ) { int c = 0 ; for ( int i = 0 ; i < r . length ( ) ; i ++ ) { if ( ( int ) r [ i ] > p ) c ++ ; } if ( c == K ) return true ; else return false ; } void countSubStrings ( string s , int K , int p ) { int l = s . length ( ) ; int count = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = K ; ( i + j ) <= l ; j ++ ) { string r = s . substr ( i , j ) ; if ( isValidSubString ( r , K , p ) ) count ++ ; } } cout << count << " STRNEWLINE " ; } int main ( ) { string s = " abepztydba " ; int K = 4 ; int p = 110 ; countSubStrings ( s , K , p ) ; return 0 ; }
Count number of substrings with numeric value greater than X | C ++ implementation of the approach ; Function that counts valid sub - strings ; Only take those numbers that do not start with '0' . ; converting the sub - string starting from index ' i ' and having length ' len ' to int and checking if it is greater than X or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( string S , int X ) { int count = 0 ; const int N = S . length ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] != '0' ) { for ( int len = 1 ; ( i + len ) <= N ; ++ len ) { if ( stoi ( S . substr ( i , len ) ) > X ) count ++ ; } } } return count ; } int main ( ) { string S = "2222" ; int X = 97 ; cout << count ( S , X ) ; return 0 ; }
Check whether a binary string can be formed by concatenating given N numbers sequentially | C ++ implementation of the approach ; Function that returns false if the number passed as argument contains digit ( s ) other than '0' or '1' ; Function that checks whether the binary string can be formed or not ; Empty string for storing the binary number ; check if a [ i ] can be a part of the binary string ; Conversion of int into string ; if a [ i ] can 't be a part  then break the loop ; possible to create binary string ; impossible to create binary string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isBinary ( int n ) { while ( n != 0 ) { int temp = n % 10 ; if ( temp != 0 && temp != 1 ) { return false ; } n = n / 10 ; } return true ; } void formBinaryStr ( int n , int a [ ] ) { bool flag = true ; string s = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( isBinary ( a [ i ] ) ) s += to_string ( a [ i ] ) ; else { flag = false ; break ; } } if ( flag ) cout << s << " STRNEWLINE " ; else cout << " - 1 STRNEWLINE " ; } int main ( ) { int a [ ] = { 10 , 1 , 0 , 11 , 10 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; formBinaryStr ( N , a ) ; return 0 ; }
Check if all the palindromic sub | C ++ implementation of the approach # include < bitsstdc ++ . h > ; Function to check if the string is palindrome ; Function that checks whether all the palindromic sub - strings are of odd length . ; Creating each substring ; If the sub - string is of even length and is a palindrome then , we return False ; Driver code
using namespace std ; bool checkPalindrome ( string s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] != s [ s . length ( ) - i - 1 ] ) return false ; } return true ; } bool CheckOdd ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string x = " " ; for ( int j = i ; j < n ; j ++ ) { x += s [ j ] ; if ( x . length ( ) % 2 == 0 && checkPalindrome ( x ) == true ) return false ; } } return true ; } int main ( ) { string s = " geeksforgeeks " ; if ( CheckOdd ( s ) ) cout << ( " YES " ) ; else cout << ( " NO " ) ; }
Number of permutations of a string in which all the occurrences of a given character occurs together | C ++ implementation of the approach ; Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't  present in the string then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver Code ; Assuming the string and the character are all in uppercase
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int fact ( int n ) { long long result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } int getResult ( string str , char ch ) { int has [ 26 ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) has [ str [ i ] - ' A ' ] ++ ; int particular = has [ ch - ' A ' ] ; if ( particular == 0 ) return 0 ; has [ ch - ' A ' ] = 0 ; int total = str . length ( ) ; total = total - particular + 1 ; long long int result = fact ( total ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } int main ( ) { string str = " MISSISSIPPI " ; cout << getResult ( str , ' S ' ) << endl ; return 0 ; }
Check if there exists any sub | C ++ program to check if there exists at least 1 sub - sequence in a string which is not palindrome ; Function to check if there exists at least 1 sub - sequence in a string which is not palindrome ; use set to count number of distinct characters ; insert each character in set ; If there is more than 1 unique characters , return true ; Else , return false ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAnyNotPalindrome ( string s ) { set < char > unique ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) unique . insert ( s [ i ] ) ; if ( unique . size ( ) > 1 ) return true ; else return false ; } int main ( ) { string s = " aaaaab " ; if ( isAnyNotPalindrome ( s ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Arrangement of words without changing the relative position of vowel and consonants | C ++ program for Arrangement of words without changing the relative position of vowel and consonants ; this function return n ! ; this will return total number of ways ; freq maintains frequency of each character in word ; check character is vowel or not ; the characters that are not vowel must be consonant ; number of ways to arrange vowel ; multiply both as these are independent ; Driver function ; string contains only capital letters ; this will contain ans
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long int NEW_LINE ll factorial ( ll n ) { ll res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) res = res * i ; return res ; } ll count ( string word ) { ll freq [ 27 ] = { 0 } ; ll vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { freq [ word [ i ] - ' A ' ] ++ ; if ( word [ i ] == ' A ' word [ i ] == ' E ' word [ i ] == ' I ' word [ i ] == ' O ' word [ i ] == ' U ' ) { vowel ++ ; } else consonant ++ ; } ll vowelArrange ; vowelArrange = factorial ( vowel ) ; vowelArrange /= factorial ( freq [ 0 ] ) ; vowelArrange /= factorial ( freq [ 4 ] ) ; vowelArrange /= factorial ( freq [ 8 ] ) ; vowelArrange /= factorial ( freq [ 14 ] ) ; vowelArrange /= factorial ( freq [ 20 ] ) ; ll consonantArrange ; consonantArrange = factorial ( consonant ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 ) consonantArrange /= factorial ( freq [ i ] ) ; } ll total = vowelArrange * consonantArrange ; return total ; } int main ( ) { string word = " COMPUTER " ; ll ans = count ( word ) ; cout << ans << endl ; return 0 ; }
Check if it is possible to create a palindrome string from given N | C ++ implementation of the above approach ; Function to check if a string is palindrome or not ; String that stores characters of s in reverse order ; Length of the string s ; String used to form substring using N ; Variable to store sum of digits of N ; Forming the substring by traversing N ; Appending the substr to str till it 's length becomes equal to sum ; Trimming the string str so that it 's length becomes equal to sum ; Driver code ; Calling function isPalindrome to check if str is Palindrome or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string s ) { string s1 = " " ; int N = s . length ( ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) s1 += s [ i ] ; if ( s == s1 ) return true ; return false ; } bool createString ( int N ) { string str = " " ; string s = to_string ( N ) ; string letters = " abcdefghij " ; int sum = 0 ; string substr = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s [ i ] - '0' ; substr += letters [ digit ] ; sum += digit ; } while ( str . length ( ) <= sum ) { str += substr ; } str = str . substr ( 0 , sum ) ; return isPalindrome ( str ) ; } int main ( ) { int N = 61 ; bool flag = createString ( N ) ; if ( flag ) cout << " YES " ; else cout << " NO " ; }
Program to find the product of ASCII values of characters in a string | C ++ program to find product of ASCII value of characters in string ; Function to find product of ASCII value of characters in string ; Traverse string to find the product ; Return the product ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long productAscii ( string str ) { long long prod = 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { prod *= ( int ) str [ i ] ; } return prod ; } int main ( ) { string str = " GfG " ; cout << productAscii ( str ) ; return 0 ; }
Check if a large number can be divided into two or more segments of equal sum | C ++ program to Check if a large number can be divided into two or more segments of equal sum ; Function to check if a number can be divided into segments ; length of string ; array to store prefix sum ; first index ; calculate the prefix ; iterate for all number from second number ; sum from 0 th index to i - 1 th index ; counter turns true when sum is obtained from a segment ; iterate till the last number ; sum of segments ; if segment sum is equal to first segment ; when greater than not possible ; if at the end all values are traversed and all segments have sum equal to first segment then it is possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s ) { int n = s . length ( ) ; int Presum [ n ] ; Presum [ 0 ] = s [ 0 ] - '0' ; for ( int i = 1 ; i < n ; i ++ ) { Presum [ i ] = Presum [ i - 1 ] + ( s [ i ] - '0' ) ; } for ( int i = 1 ; i <= n - 1 ; i ++ ) { int sum = Presum [ i - 1 ] ; int presum = 0 ; int it = i ; int flag = 0 ; while ( it < n ) { presum += s [ it ] - '0' ; if ( presum == sum ) { presum = 0 ; flag = 1 ; } else if ( presum > sum ) { break ; } it ++ ; } if ( presum == 0 && it == n && flag == 1 ) { return true ; } } return false ; } int main ( ) { string s = "73452" ; if ( check ( s ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Balance a string after removing extra brackets | C ++ implementation of above approach ; Print balanced and remove extra brackets from string ; Maintain a count for opening brackets Traversing string ; check if opening bracket ; print str [ i ] and increment count by 1 ; check if closing bracket and count != 0 ; decrement count by 1 ; if str [ i ] not a closing brackets print it ; balanced brackets if opening brackets are more then closing brackets ; print remaining closing brackets ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void balancedString ( string str ) { int count = 0 , i ; int n = str . length ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) { cout << str [ i ] ; count ++ ; } else if ( str [ i ] == ' ) ' && count != 0 ) { cout << str [ i ] ; count -- ; } else if ( str [ i ] != ' ) ' ) cout << str [ i ] ; } if ( count != 0 ) for ( i = 0 ; i < count ; i ++ ) cout << " ) " ; } int main ( ) { string str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " ; balancedString ( str ) ; return 0 ; }
Minimum operation require to make first and last character same | CPP program to minimum operation require to make first and last character same ; Recursive function call ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = INT_MAX ; int minOperation ( string & s , int i , int j , int count ) { if ( ( i >= s . size ( ) && j < 0 ) || ( i == j ) ) return MAX ; if ( s [ i ] == s [ j ] ) return count ; if ( i >= s . size ( ) ) return minOperation ( s , i , j - 1 , count + 1 ) ; else if ( j < 0 ) return minOperation ( s , i + 1 , j , count + 1 ) ; else return min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } int main ( ) { string s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . size ( ) - 1 , 0 ) ; if ( ans == MAX ) cout << -1 ; else cout << ans ; return 0 ; }
Count strings with consonants and vowels at alternate position | C ++ implementation of above approach ; Function to find the count of strings ; Variable to store the final result ; Loop iterating through string ; If ' $ ' is present at the even position in the string ; ' sum ' is multiplied by 21 ; If ' $ ' is present at the odd position in the string ; ' sum ' is multiplied by 5 ; Driver code ; Let the string ' str ' be s$$e$ ; Print result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countStrings ( string s ) { long sum = 1 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( i % 2 == 0 && s [ i ] == ' $ ' ) sum *= 21 ; else if ( s [ i ] == ' $ ' ) sum *= 5 ; } return sum ; } int main ( ) { string str = " s $ $ e $ " ; cout << countStrings ( str ) << endl ; return 0 ; }
Remove duplicates from a string in O ( 1 ) extra space | C ++ implementation of above approach ; Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant string ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <string> NEW_LINE using namespace std ; string removeDuplicatesFromString ( string str ) { int counter = 0 ; int i = 0 ; int size = str . size ( ) ; int x ; int length = 0 ; while ( i < size ) { x = str [ i ] - 97 ; if ( ( counter & ( 1 << x ) ) == 0 ) { str [ length ] = ' a ' + x ; counter = counter | ( 1 << x ) ; length ++ ; } i ++ ; } return str . substr ( 0 , length ) ; } int main ( ) { string str = " geeksforgeeks " ; cout << removeDuplicatesFromString ( str ) ; return 0 ; }
Remove duplicates from a string in O ( 1 ) extra space | C ++ implementation of above approach ; Method to remove duplicates ; Table to keep track of visited characters ; To keep track of end index of resultant string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeDuplicatesFromString ( string str ) { vector < int > table ( 256 , 0 ) ; vector < char > chars ; for ( auto i : str ) chars . push_back ( i ) ; int endIndex = 0 ; for ( int i = 0 ; i < chars . size ( ) ; i ++ ) { if ( table [ chars [ i ] ] == 0 ) { table [ chars [ i ] ] = -1 ; chars [ endIndex ++ ] = chars [ i ] ; } } string ans = " " ; for ( int i = 0 ; i < endIndex ; i ++ ) ans += chars [ i ] ; return ans ; } int main ( ) { string str = " geeksforgeeks " ; cout << ( removeDuplicatesFromString ( str ) ) << endl ; }
Check if the characters in a string form a Palindrome in O ( 1 ) extra space | CPP program to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; Utility function to get the position of first character in the string ; Get the position of first character in the string ; Utility function to get the position of last character in the string ; Get the position of last character in the string ; Function to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; break , when all letters are checked ; if mismatch found , break the loop ; Driver code
#include <iostream> NEW_LINE using namespace std ; int firstPos ( string str , int start , int end ) { int firstChar = -1 ; for ( int i = start ; i <= end ; i ++ ) { if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { firstChar = i ; break ; } } return firstChar ; } int lastPos ( string str , int start , int end ) { int lastChar = -1 ; for ( int i = start ; i >= end ; i -- ) { if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { lastChar = i ; break ; } } return lastChar ; } bool isPalindrome ( string str ) { int firstChar = 0 , lastChar = str . length ( ) - 1 ; bool ch = true ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { firstChar = firstPos ( str , firstChar , lastChar ) ; lastChar = lastPos ( str , lastChar , firstChar ) ; if ( lastChar < 0 firstChar < 0 ) break ; if ( str [ firstChar ] == str [ lastChar ] ) { firstChar ++ ; lastChar -- ; continue ; } ch = false ; break ; } return ( ch ) ; } int main ( ) { string str = " m TABSYMBOL a ▁ 343 ▁ la ▁ y ▁ a ▁ l ▁ am " ; if ( isPalindrome ( str ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Maximum length subsequence possible of the form R ^ N K ^ N | C ++ program to find the maximum length of a substring of form R ^ nK ^ n ; function to calculate the maximum length of substring of the form R ^ nK ^ n ; Count no . Of R 's before a K ; Count no . Of K 's after a K ; Update maximum length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( string s ) { int max = 0 , i , j = 0 , countk = 0 , countr = 0 ; int table [ s . length ( ) ] [ 2 ] ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == ' R ' ) countr ++ ; else table [ j ++ ] [ 0 ] = countr ; } j -- ; for ( i = s . length ( ) - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' K ' ) { countk ++ ; table [ j -- ] [ 1 ] = countk ; } if ( min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) > max ) max = min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) ; } return max ; } int main ( ) { string s = " RKRRRKKRRKKKKRR " ; int n = find ( s ) ; cout << ( n ) ; }
Longest common anagram subsequence from N strings | C ++ program to find longest possible subsequence anagram of N strings . ; function to store frequency of each character in each string ; function to Find longest possible sequence of N strings which is anagram to each other ; to get lexicographical largest sequence . ; find minimum of that character ; print that character minimum number of times ; Driver code ; to store frequency of each character in each string ; to get frequency of each character ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; void frequency ( int fre [ ] [ MAX_CHAR ] , string s [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { string str = s [ i ] ; for ( int j = 0 ; j < str . size ( ) ; j ++ ) fre [ i ] [ str [ j ] - ' a ' ] ++ ; } } void LongestSequence ( int fre [ ] [ MAX_CHAR ] , int n ) { for ( int i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { int mi = fre [ 0 ] [ i ] ; for ( int j = 1 ; j < n ; j ++ ) mi = min ( fre [ j ] [ i ] , mi ) ; while ( mi -- ) cout << ( char ) ( ' a ' + i ) ; } } int main ( ) { string s [ ] = { " loo " , " lol " , " olive " } ; int n = sizeof ( s ) / sizeof ( s [ 0 ] ) ; int fre [ n ] [ 26 ] = { 0 } ; frequency ( fre , s , n ) ; LongestSequence ( fre , n ) ; return 0 ; }
Check if two strings are permutation of each other | C ++ program to check whether two strings are Permutations of each other ; function to check whether two strings are Permutation of each other ; Get lengths of both strings ; If length of both strings is not same , then they cannot be Permutation ; Sort both strings ; Compare sorted strings ; Driver program to test to print printDups
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool arePermutation ( string str1 , string str2 ) { int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; if ( n1 != n2 ) return false ; sort ( str1 . begin ( ) , str1 . end ( ) ) ; sort ( str2 . begin ( ) , str2 . end ( ) ) ; for ( int i = 0 ; i < n1 ; i ++ ) if ( str1 [ i ] != str2 [ i ] ) return false ; return true ; } int main ( ) { string str1 = " test " ; string str2 = " ttew " ; if ( arePermutation ( str1 , str2 ) ) printf ( " Yes " ) ; else printf ( " No " ) ; return 0 ; }
Check if two strings are permutation of each other | C ++ function to check whether two strings are Permutations of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array
bool arePermutation ( string str1 , string str2 ) { int count [ NO_OF_CHARS ] = { 0 } ; int i ; for ( i = 0 ; str1 [ i ] && str2 [ i ] ; i ++ ) { count [ str1 [ i ] ] ++ ; count [ str2 [ i ] ] -- ; } if ( str1 [ i ] str2 [ i ] ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] ) return false ; return true ; }
Maximum power of jump required to reach the end of string | C ++ program to calculate power of jump ; Function to calculate the maximum power of the jump ; Initialize the count with 1 ; Find the character at last index ; Start traversing the string ; Check if the current char is equal to the last character ; max_so_far stores maximum value of the power of the jump from starting to ith position ; Reset the count to 1 ; Else , increment the number of jumps / count ; Return the maximum number of jumps ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int powerOfJump ( string s ) { int count = 1 ; int max_so_far = INT_MIN ; char ch = s [ s . length ( ) - 1 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == ch ) { if ( count > max_so_far ) { max_so_far = count ; } count = 1 ; } else count ++ ; } return max_so_far ; } int main ( ) { string st = "1010101" ; cout << powerOfJump ( st ) ; }