codeparrot/xlcost-text-to-code huggingface dataset:
Next word that does not contain a palindrome and has characters from first k | CPP program to find lexicographically next word which contains first K letters of the English alphabet and does not contain a palindrome as it 's substring of length more than one. ; function to return lexicographically next word ; we made m as m + 97 that means our required string contains not more than m + 97 ( as per ASCII value ) in it . ; increment last alphabet to make next lexicographically next word . ; if i - th alphabet not in first k letters then make it as " a " and then increase ( i - 1 ) th letter ; to check whether formed string palindrome or not . ; increment i . ; if i less than or equals to one that means we not formed such word . ; Driver code for above function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNextWord ( string s , int m ) { m += 97 ; int n = s . length ( ) ; int i = s . length ( ) - 1 ; s [ i ] ++ ; while ( i >= 0 && i <= n - 1 ) { if ( s [ i ] >= m ) { s [ i ] = ' a ' ; s [ -- i ] ++ ; } else if ( s [ i ] == s [ i - 1 ] s [ i ] == s [ i - 2 ] ) s [ i ] ++ ; else i ++ ; } if ( i <= -1 ) cout << " - 1" ; else cout << s ; } int main ( ) { string str = " abcd " ; int k = 4 ; findNextWord ( str , k ) ; return 0 ; }
Program to implement ASCII lookup table | C ++ implementation of ASCII lookup table ; Function to convert decimal value to equivalent octal value ; Function to convert decimal value to equivalent hexadecimal value ; Function to convert decimal value to equivalent HTML value ; ASCII lookup table ; Implicit typecasting converts the character into it 's equivalent ASCII ; Driver function
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int Octal ( int decimal ) { int octal = 0 ; string temp = " " ; while ( decimal > 0 ) { int remainder = decimal % 8 ; temp = to_string ( remainder ) + temp ; decimal /= 8 ; } for ( int i = 0 ; i < temp . length ( ) ; i ++ ) octal = ( octal * 10 ) + ( temp [ i ] - '0' ) ; return octal ; } string Hexadecimal ( int decimal ) { string hex = " " ; while ( decimal > 0 ) { int remainder = decimal % 16 ; if ( remainder >= 0 && remainder <= 9 ) hex = to_string ( remainder ) + hex ; else hex = ( char ) ( ' A ' + remainder % 10 ) + hex ; decimal /= 16 ; } return hex ; } string HTML ( int decimal ) { string html = to_string ( decimal ) ; html = " & # " + html + " ; " ; return html ; } void ASCIIlookuptable ( char ch ) { int decimal = ch ; cout << " Octal ▁ value : ▁ " << Octal ( decimal ) << endl ; cout << " Decimal ▁ value : ▁ " << decimal << endl ; cout << " Hexadecimal ▁ value : ▁ " << Hexadecimal ( decimal ) << endl ; cout << " HTML ▁ value : ▁ " << HTML ( decimal ) ; } int main ( ) { char ch = ' @ ' ; ASCIIlookuptable ( ch ) ; return 0 ; }
Replace a character c1 with c2 and c2 with c1 in a string S | CPP program to replace c1 with c2 and c2 with c1 ; loop to traverse in the string ; check for c1 and replace ; check for c2 and replace ; Driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string replace ( string s , char c1 , char c2 ) { int l = s . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( s [ i ] == c1 ) s [ i ] = c2 ; else if ( s [ i ] == c2 ) s [ i ] = c1 ; } return s ; } int main ( ) { string s = " grrksfoegrrks " ; char c1 = ' e ' , c2 = ' r ' ; cout << replace ( s , c1 , c2 ) ; return 0 ; }
Fibonacci Word | program for nth Fibonacci word ; Returns n - th Fibonacci word ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string fibWord ( int n ) { string Sn_1 = "0" ; string Sn = "01" ; string tmp ; for ( int i = 2 ; i <= n ; i ++ ) { tmp = Sn ; Sn += Sn_1 ; Sn_1 = tmp ; } return Sn ; } int main ( ) { int n = 6 ; cout << fibWord ( n ) ; return 0 ; }
Decimal to octal conversion with minimum use of arithmetic operators | C ++ implementation of decimal to octal conversion with minimum use of arithmetic operators ; function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Function to find octal equivalent of binary ; add min 0 's in the beginning to make  string length divisible by 3 ; create map between binary and its equivalent octal code ; one by one extract from left , substring of size 3 and add its octal code ; required octal number ; function to find octal equivalent of decimal ; convert decimal to binary ; convert binary to octal required octal equivalent of decimal ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decToBin ( int n ) { if ( n == 0 ) return "0" ; string bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } string convertBinToOct ( string bin ) { int l = bin . size ( ) ; for ( int i = 1 ; i <= ( 3 - l % 3 ) % 3 ; i ++ ) bin = '0' + bin ; unordered_map < string , char > bin_oct_map ; bin_oct_map [ "000" ] = '0' ; bin_oct_map [ "001" ] = '1' ; bin_oct_map [ "010" ] = '2' ; bin_oct_map [ "011" ] = '3' ; bin_oct_map [ "100" ] = '4' ; bin_oct_map [ "101" ] = '5' ; bin_oct_map [ "110" ] = '6' ; bin_oct_map [ "111" ] = '7' ; int i = 0 ; string octal = " " ; while ( 1 ) { octal += bin_oct_map [ bin . substr ( i , 3 ) ] ; i += 3 ; if ( i == bin . size ( ) ) break ; } return octal ; } string decToOctal ( int n ) { string bin = decToBin ( n ) ; return convertBinToOct ( bin ) ; } int main ( ) { int n = 151 ; cout << decToOctal ( n ) ; return 0 ; }
Split the string into substrings using delimiter | C ++ implementation to split string into substrings on the basis of delimiter ; function to split string into substrings on the basis of delimiter and return the substrings after split ; to count the number of split strings ; adding delimiter character at the end of ' str ' ; length of ' str ' ; traversing ' str ' from left to right ; if str [ i ] is not equal to the delimiter character then accumulate it to ' word ' ; if ' word ' is not an empty string , then add this ' word ' to the array ' substr _ list [ ] ' ; reset ' word ' ; return the splitted strings ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > splitStrings ( string str , char dl ) { string word = " " ; int num = 0 ; str = str + dl ; int l = str . size ( ) ; vector < string > substr_list ; for ( int i = 0 ; i < l ; i ++ ) { if ( str [ i ] != dl ) word = word + str [ i ] ; else { if ( ( int ) word . size ( ) != 0 ) substr_list . push_back ( word ) ; word = " " ; } } return substr_list ; } int main ( ) { string str = " geeks ; for ; geeks " ; char dl = ' ; ' ; vector < string > res = splitStrings ( str , dl ) ; for ( auto x : res ) cout << x << endl ; return 0 ; }
Construct lexicographically smallest palindrome | CPP for constructing smallest palindrome ; function for printing palindrome ; iterate till i < j ; continue if str [ i ] == str [ j ] ; update str [ i ] = str [ j ] = ' a ' if both are ' * ' ; update str [ i ] = str [ j ] if only str [ i ] = ' * ' ; update str [ j ] = str [ i ] if only str [ j ] = ' * ' ; else print not possible and return ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string constructPalin ( string str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != ' * ' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; continue ; } else if ( str [ i ] == ' * ' ) { str [ i ] = str [ j ] ; continue ; } else if ( str [ j ] == ' * ' ) { str [ j ] = str [ i ] ; continue ; } cout << " Not ▁ Possible " ; return " " ; } return str ; } int main ( ) { string str = " bca * xc * * b " ; int len = str . size ( ) ; cout << constructPalin ( str , len ) ; return 0 ; }
Change string to a new character set | CPP program to change the sentence with virtual dictionary ; Converts str to given character set ; hashing for new character set ; conversion of new character set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void conversion ( char charSet [ ] , string & str ) { int n = str . length ( ) ; char hashChar [ 26 ] ; for ( int i = 0 ; i < 27 ; i ++ ) hashChar [ charSet [ i ] - ' a ' ] = ' a ' + i ; for ( int i = 0 ; i < n ; i ++ ) str [ i ] = hashChar [ str [ i ] - ' a ' ] ; } int main ( ) { char charSet [ 27 ] = " qwertyuiopasdfghjklzxcvbnm " ; string str = " egrt " ; conversion ( charSet , str ) ; cout << str ; return 0 ; }
Different substrings in a string that start and end with given strings | Cpp program to find number of different sub strings ; function to return number of different sub - strings ; initially our answer is zero . ; find the length of given strings ; currently make array and initially put zero . ; find occurrence of " a " and " b " in string " s " ; We use a hash to make sure that same substring is not counted twice . ; go through all the positions to find occurrence of " a " first . ; if we found occurrence of " a " . ; then go through all the positions to find occurrence of " b " . ; if we do found " b " at index j then add it to already existed substring . ; if we found occurrence of " b " . ; now add string " b " to already existed substring . ; If current substring is not included already . ; put any non negative integer to make this string as already existed . ; make substring null . ; return answer . ; Driver program for above function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfDifferentSubstrings ( string s , string a , string b ) { int ans = 0 ; int ls = s . size ( ) , la = a . size ( ) , lb = b . size ( ) ; int x [ ls ] = { 0 } , y [ ls ] = { 0 } ; for ( int i = 0 ; i < ls ; i ++ ) { if ( s . substr ( i , la ) == a ) x [ i ] = 1 ; if ( s . substr ( i , lb ) == b ) y [ i ] = 1 ; } unordered_set < string > hash ; string curr_substr = " " ; for ( int i = 0 ; i < ls ; i ++ ) { if ( x [ i ] ) { for ( int j = i ; j < ls ; j ++ ) { if ( ! y [ j ] ) curr_substr += s [ j ] ; if ( y [ j ] ) { curr_substr += s . substr ( j , lb ) ; if ( hash . find ( curr_substr ) == hash . end ( ) ) ans ++ ; hash . insert ( curr_substr ) ; } } curr_substr = " " ; } } return ans ; } int main ( ) { string s = " codecppforfood " ; string begin = " c " ; string end = " d " ; cout << numberOfDifferentSubstrings ( s , begin , end ) << endl ; return 0 ; }
Printing string in plus ‘ + ’ pattern in the matrix | CPP program to print the string in ' plus ' pattern ; Function to make a cross in the matrix ; As , it is not possible to make the cross exactly in the middle of the matrix with an even length string . ; declaring a 2D array i . e a matrix ; Now , we will fill all the elements of the array with ' X ' ; Now , we will place the characters of the string in the matrix , such that a cross is formed in it . ; here the characters of the string will be added in the middle column of our array . ; here the characters of the string will be added in the middle row of our array . ; Now finally , we will print the array ; driver code
#include <bits/stdc++.h> NEW_LINE #define max  100 NEW_LINE using namespace std ; void carveCross ( string str ) { int n = str . length ( ) ; if ( n % 2 == 0 ) { cout << " Not ▁ possible . ▁ Please ▁ enter ▁ " << " odd ▁ length ▁ string . STRNEWLINE " ; } else { char arr [ max ] [ max ] ; int m = n / 2 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { arr [ i ] [ j ] = ' X ' ; } } for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] [ m ] = str [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ m ] [ i ] = str [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << arr [ i ] [ j ] << " ▁ " ; } cout << " STRNEWLINE " ; } } } int main ( ) { string str = " PICTURE " ; carveCross ( str ) ; return 0 ; }
Binary String of given length that without a palindrome of size 3 | CPP program find a binary string of given length that doesn 't contain a palindrome of size 3. ; Printing the character according to i ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generatestring ( int n ) { for ( int i = 0 ; i < n ; i ++ ) putchar ( i & 2 ? ' b ' : ' a ' ) ; puts ( " " ) ; } int main ( ) { int n = 5 ; generatestring ( n ) ; n = 8 ; generatestring ( n ) ; n = 10 ; generatestring ( n ) ; }
Print all subsequences of a string | Iterative Method | C ++ program to print all Subsequences of a string in iterative manner ; function to find subsequence ; check if jth bit in binary is 1 ; if jth bit is 1 , include it in subsequence ; function to print all subsequences ; map to store subsequence lexicographically by length ; Total number of non - empty subsequence in string is 2 ^ len - 1 ; i = 0 , corresponds to empty subsequence ; subsequence for binary pattern i ; storing sub in map ; it . first is length of Subsequence it . second is set < string > ; ii is iterator of type set < string > ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string subsequence ( string s , int binary , int len ) { string sub = " " ; for ( int j = 0 ; j < len ; j ++ ) if ( binary & ( 1 << j ) ) sub += s [ j ] ; return sub ; } void possibleSubsequences ( string s ) { map < int , set < string > > sorted_subsequence ; int len = s . size ( ) ; int limit = pow ( 2 , len ) ; for ( int i = 1 ; i <= limit - 1 ; i ++ ) { string sub = subsequence ( s , i , len ) ; sorted_subsequence [ sub . length ( ) ] . insert ( sub ) ; } for ( auto it : sorted_subsequence ) { cout << " Subsequences ▁ of ▁ length ▁ = ▁ " << it . first << " ▁ are : " << endl ; for ( auto ii : it . second ) cout << ii << " ▁ " ; cout << endl ; } } int main ( ) { string s = " aabc " ; possibleSubsequences ( s ) ; return 0 ; }
Print all subsequences of a string | Iterative Method | C ++ code all Subsequences of a string in iterative manner ; function to find subsequence ; loop while binary is greater than 0 ; get the position of rightmost set bit ; append at beginning as we are going from LSB to MSB ; resets bit at pos in binary ; function to print all subsequences ; map to store subsequence lexicographically by length ; Total number of non - empty subsequence in string is 2 ^ len - 1 ; i = 0 , corresponds to empty subsequence ; subsequence for binary pattern i ; storing sub in map ; it . first is length of Subsequence it . second is set < string > ; ii is iterator of type set < string > ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string subsequence ( string s , int binary ) { string sub = " " ; int pos ; while ( binary > 0 ) { pos = log2 ( binary & - binary ) + 1 ; sub = s [ pos - 1 ] + sub ; binary = ( binary & ~ ( 1 << ( pos - 1 ) ) ) ; } reverse ( sub . begin ( ) , sub . end ( ) ) ; return sub ; } void possibleSubsequences ( string s ) { map < int , set < string > > sorted_subsequence ; int len = s . size ( ) ; int limit = pow ( 2 , len ) ; for ( int i = 1 ; i <= limit - 1 ; i ++ ) { string sub = subsequence ( s , i ) ; sorted_subsequence [ sub . length ( ) ] . insert ( sub ) ; } for ( auto it : sorted_subsequence ) { cout << " Subsequences ▁ of ▁ length ▁ = ▁ " << it . first << " ▁ are : " << endl ; for ( auto ii : it . second ) cout << ii << " ▁ " ; cout << endl ; } } int main ( ) { string s = " aabc " ; possibleSubsequences ( s ) ; return 0 ; }
Program to find remainder when large number is divided by 11 | CPP implementation to find remainder when a large number is divided by 11 ; Function to return remainder ; len is variable to store the length of number string . ; loop that find remainder ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int remainder ( string str ) { int len = str . length ( ) ; int num , rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = rem * 10 + ( str [ i ] - '0' ) ; rem = num % 11 ; } return rem ; } int main ( ) { string str = "3435346456547566345436457867978" ; cout << remainder ( str ) ; return 0 ; }
Longest subsequence of the form 0 * 1 * 0 * in a binary string | CPP program to find longest subsequence of the form 0 * 1 * 0 * in a binary string ; Returns length of the longest subsequence of the form 0 * 1 * 0 * ; Precomputing values in three arrays pre_count_0 [ i ] is going to store count of 0 s in prefix str [ 0. . i - 1 ] pre_count_1 [ i ] is going to store count of 1 s in prefix str [ 0. . i - 1 ] post_count_0 [ i ] is going to store count of 0 s in suffix str [ i - 1. . n - 1 ] ; string is made up of all 0 s or all 1 s ; Compute result using precomputed values ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubseq ( string s ) { int n = s . length ( ) ; int pre_count_0 [ n + 2 ] ; int pre_count_1 [ n + 1 ] ; int post_count_0 [ n + 1 ] ; pre_count_0 [ 0 ] = 0 ; post_count_0 [ n + 1 ] = 0 ; pre_count_1 [ 0 ] = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { pre_count_0 [ j ] = pre_count_0 [ j - 1 ] ; pre_count_1 [ j ] = pre_count_1 [ j - 1 ] ; post_count_0 [ n - j + 1 ] = post_count_0 [ n - j + 2 ] ; if ( s [ j - 1 ] == '0' ) pre_count_0 [ j ] ++ ; else pre_count_1 [ j ] ++ ; if ( s [ n - j ] == '0' ) post_count_0 [ n - j + 1 ] ++ ; } if ( pre_count_0 [ n ] == n pre_count_0 [ n ] == 0 ) return n ; int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) ans = max ( pre_count_0 [ i - 1 ] + pre_count_1 [ j ] - pre_count_1 [ i - 1 ] + post_count_0 [ j + 1 ] , ans ) ; return ans ; } int main ( ) { string s = "000011100000" ; cout << longestSubseq ( s ) ; return 0 ; }
Distinct permutations of the string | Set 2 | C ++ program to distinct permutations of the string ; Returns true if str [ curr ] does not matches with any of the characters after str [ start ] ; Prints all distinct permutations in str [ 0. . n - 1 ] ; Proceed further for str [ i ] only if it doesn 't match with any of the characters  after str[index] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool shouldSwap ( char str [ ] , int start , int curr ) { for ( int i = start ; i < curr ; i ++ ) if ( str [ i ] == str [ curr ] ) return 0 ; return 1 ; } void findPermutations ( char str [ ] , int index , int n ) { if ( index >= n ) { cout << str << endl ; return ; } for ( int i = index ; i < n ; i ++ ) { bool check = shouldSwap ( str , index , i ) ; if ( check ) { swap ( str [ index ] , str [ i ] ) ; findPermutations ( str , index + 1 , n ) ; swap ( str [ index ] , str [ i ] ) ; } } } int main ( ) { char str [ ] = " ABCA " ; int n = strlen ( str ) ; findPermutations ( str , 0 , n ) ; return 0 ; }
Generate permutations with only adjacent swaps allowed | CPP program to generate permutations with only one swap allowed . ; don 't swap the current position ; Swap with the next character and revert the changes . As explained above , swapping with previous is is not needed as it anyways happens for next character . ; Driver code
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void findPermutations ( char str [ ] , int index , int n ) { if ( index >= n || ( index + 1 ) >= n ) { cout << str << endl ; return ; } findPermutations ( str , index + 1 , n ) ; swap ( str [ index ] , str [ index + 1 ] ) ; findPermutations ( str , index + 2 , n ) ; swap ( str [ index ] , str [ index + 1 ] ) ; } int main ( ) { char str [ ] = { "12345" } ; int n = strlen ( str ) ; findPermutations ( str , 0 , n ) ; return 0 ; }
Decode a median string to the original string | C ++ program to decode a median string to the original string ; function to calculate the median back string ; length of string ; initialize a blank string ; Flag to check if length is even or odd ; traverse from first to last ; if len is even then add first character to beginning of new string and second character to end ; if current length is odd and is greater than 1 ; add first character to end and second character to beginning ; if length is 1 , add character to end ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decodeMedianString ( string s ) { int l = s . length ( ) ; string s1 = " " ; bool isEven = ( l % 2 == 0 ) ? true : false ; for ( int i = 0 ; i < l ; i += 2 ) { if ( isEven ) { s1 = s [ i ] + s1 ; s1 += s [ i + 1 ] ; } else { if ( l - i > 1 ) { s1 += s [ i ] ; s1 = s [ i + 1 ] + s1 ; } else { s1 += s [ i ] ; } } } return s1 ; } int main ( ) { string s = " eekgs " ; cout << decodeMedianString ( s ) ; return 0 ; }
Maximum number of characters between any two same character in a string | Simple C ++ program to find maximum number of characters between two occurrences of same character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumChars ( string & str ) { int n = str . length ( ) ; int res = -1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( str [ i ] == str [ j ] ) res = max ( res , abs ( j - i - 1 ) ) ; return res ; } int main ( ) { string str = " abba " ; cout << maximumChars ( str ) ; return 0 ; }
Maximum number of characters between any two same character in a string | Efficient C ++ program to find maximum number of characters between two occurrences of same character ; Initialize all indexes as - 1. ; If this is first occurrence ; Else find distance from previous occurrence and update result ( if required ) . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; int maximumChars ( string & str ) { int n = str . length ( ) ; int res = -1 ; int firstInd [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) firstInd [ i ] = -1 ; for ( int i = 0 ; i < n ; i ++ ) { int first_ind = firstInd [ str [ i ] ] ; if ( first_ind == -1 ) firstInd [ str [ i ] ] = i ; else res = max ( res , abs ( i - first_ind - 1 ) ) ; } return res ; } int main ( ) { string str = " abba " ; cout << maximumChars ( str ) ; return 0 ; }
Check if an encoding represents a unique binary string | C ++ program to check if given encoding represents a single string . ; Return true if sum becomes k ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isUnique ( int a [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; sum += n - 1 ; return ( sum == k ) ; } int main ( ) { int a [ ] = { 3 , 3 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 12 ; if ( isUnique ( a , n , k ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Lexicographically next string | C ++ program to find lexicographically next string ; If string is empty . ; Find first character from right which is not z . ; If all characters are ' z ' , append an ' a ' at the end . ; If there are some non - z characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string nextWord ( string s ) { if ( s == " " ) return " a " ; int i = s . length ( ) - 1 ; while ( s [ i ] == ' z ' && i >= 0 ) i -- ; if ( i == -1 ) s = s + ' a ' ; else s [ i ] ++ ; return s ; } int main ( ) { string str = " samez " ; cout << nextWord ( str ) ; return 0 ; }
Length of the longest substring with equal 1 s and 0 s | C ++ program to find the length of the longest balanced substring ; Function to check if a string contains equal number of one and zeros or not ; Function to find the length of the longest balanced substring ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( string p ) { int n = p . length ( ) ; int c1 = 0 , c0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] == '0' ) c0 ++ ; if ( p [ i ] == '1' ) c1 ++ ; } return ( c0 == c1 ) ? true : false ; } int longestSub ( string s ) { int max_len = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( isValid ( s . substr ( i , j - i + 1 ) ) && max_len < j - i + 1 ) max_len = j - i + 1 ; } } return max_len ; } int main ( ) { string s = "101001000" ; cout << longestSub ( s ) ; return 0 ; }