codeparrot/xlcost-text-to-code huggingface dataset:
Find the Mid | C ++ program to find the Mid - Alphabet for each index of the given Pair of Strings ; Function to find the mid alphabets ; For every character pair ; Get the average of the characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMidAlphabet ( string s1 , string s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int mid = ( s1 [ i ] + s2 [ i ] ) / 2 ; cout << ( char ) mid ; } } int main ( ) { string s1 = " akzbqzgw " ; string s2 = " efhctcsz " ; int n = s1 . length ( ) ; findMidAlphabet ( s1 , s2 , n ) ; return 0 ; }
Queries to find the count of vowels in the substrings of the given string | C ++ implementation of the approach ; Function that returns true if ch is a vowel ; pre [ i ] will store the count of vowels in the substring str [ 0. . . i ] ; Fill the pre [ ] array ; If current character is a vowel ; If its a consonant ; For every query ; Find the count of vowels for the current query ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  2 NEW_LINE bool isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } void performQueries ( string str , int len , int queries [ ] [ N ] , int q ) { int pre [ len ] ; if ( isVowel ( str [ 0 ] ) ) pre [ 0 ] = 1 ; else pre [ 0 ] = 0 ; for ( int i = 1 ; i < len ; i ++ ) { if ( isVowel ( str [ i ] ) ) pre [ i ] = 1 + pre [ i - 1 ] ; else pre [ i ] = pre [ i - 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) { if ( queries [ i ] [ 0 ] == 0 ) { cout << pre [ queries [ i ] [ 1 ] ] << " STRNEWLINE " ; } else { cout << ( pre [ queries [ i ] [ 1 ] ] - pre [ queries [ i ] [ 0 ] - 1 ] ) << " STRNEWLINE " ; } } } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; int queries [ ] [ N ] = { { 1 , 3 } , { 2 , 4 } , { 1 , 9 } } ; int q = ( sizeof ( queries ) / sizeof ( queries [ 0 ] ) ) ; performQueries ( str , len , queries , q ) ; return 0 ; }
Check whether the given decoded string is divisible by 6 | C ++ implementation of the approach ; Function to return the sum of the digits of n ; Function that return true if the decoded string is divisible by 6 ; To store the sum of the digits ; For each character , get the sum of the digits ; If the sum of digits is not divisible by 3 ; Get the last digit of the number formed ; If the last digit is not divisible by 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumDigits ( int n ) { int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; sum += digit ; n /= 10 ; } return sum ; } bool isDivBySix ( string str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) ( str [ i ] - ' a ' + 1 ) ; } if ( sum % 3 != 0 ) return false ; int lastDigit = ( ( int ) ( str [ n - 1 ] - ' a ' + 1 ) ) % 10 ; if ( lastDigit % 2 != 0 ) return false ; return true ; } int main ( ) { string str = " ab " ; int n = str . length ( ) ; if ( isDivBySix ( str , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check the divisibility of Hexadecimal numbers | C ++ implementation of the approach ; Function that returns true if s is divisible by m ; Map to map characters to real values ; To store the remainder at any stage ; Find the remainder ; Check the value of remainder ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const string CHARS = "0123456789ABCDEF " ; const int DIGITS = 16 ; bool isDivisible ( string s , int m ) { unordered_map < char , int > mp ; for ( int i = 0 ; i < DIGITS ; i ++ ) { mp [ CHARS [ i ] ] = i ; } int r = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { r = ( r * 16 + mp [ s [ i ] ] ) % m ; } if ( ! r ) return true ; return false ; } int main ( ) { string s = "10" ; int m = 3 ; if ( isDivisible ( s , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum number of splits of a binary number | C ++ implementation of the approach ; Function to return the required count ; If the splitting is not possible ; To store the final ans ; Counting the number of zeros ; Return the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntSplits ( string s ) { if ( s [ s . size ( ) - 1 ] == '1' ) return -1 ; int ans = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) ans += ( s [ i ] == '0' ) ; return ans ; } int main ( ) { string s = "10010" ; cout << cntSplits ( s ) ; return 0 ; }
Modify the string such that every character gets replaced with the next character in the keyboard | C ++ implementation of the approach ; Function to return the modified string ; Map to store the next character on the keyboard for every possible lowercase character ; Update the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const string CHARS = " qwertyuiopasdfghjklzxcvbnm " ; const int MAX = 26 ; string getString ( string str , int n ) { unordered_map < char , char > uMap ; for ( int i = 0 ; i < MAX ; i ++ ) { uMap [ CHARS [ i ] ] = CHARS [ ( i + 1 ) % MAX ] ; } for ( int i = 0 ; i < n ; i ++ ) { str [ i ] = uMap [ str [ i ] ] ; } return str ; } int main ( ) { string str = " geeks " ; int n = str . length ( ) ; cout << getString ( str , n ) ; return 0 ; }
Queries to find the count of characters preceding the given location | C ++ implementation of the approach ; returns character at index pos - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Count ( string s , int pos ) { int c = s [ pos - 1 ] ; int counter = 0 ; for ( int i = 0 ; i < pos - 1 ; i ++ ) { if ( s [ i ] == c ) counter = counter + 1 ; } return counter ; } int main ( ) { string s = " abacsddaa " ; int pos ; int n = s . length ( ) ; int query [ ] = { 9 , 3 , 2 } ; int Q = sizeof ( query ) / sizeof ( query [ 0 ] ) ; for ( int i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; cout << Count ( s , pos ) << endl ; } return 0 ; }
Queries to find the count of characters preceding the given location | C ++ implementation of the approach ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Count ( vector < int > temp ) { int query [ ] = { 9 , 3 , 2 } ; int Q = sizeof ( query ) / sizeof ( query [ 0 ] ) ; for ( int i = 0 ; i < Q ; i ++ ) { int pos = query [ i ] ; cout << ( temp [ pos - 1 ] ) << endl ; } } vector < int > processing ( string s , int len ) { vector < int > temp ( len ) ; map < char , int > d ; for ( int i = 0 ; i < len ; i ++ ) { if ( d . find ( s [ i ] ) == d . end ( ) ) { d [ s [ i ] ] = i ; } else { temp [ i ] = temp [ d [ s [ i ] ] ] + 1 ; d [ s [ i ] ] = i ; } } return temp ; } int main ( ) { string s = " abacsddaa " ; int n = s . length ( ) ; vector < int > temp = processing ( s , n ) ; Count ( temp ) ; }
Count of matchsticks required to represent the given number | C ++ implementation of the approach ; stick [ i ] stores the count of sticks required to represent the digit i ; Function to return the count of matchsticks required to represent the given number ; For every digit of the given number ; Add the count of sticks required to represent the current digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sticks [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int countSticks ( string str , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cnt += ( sticks [ str [ i ] - '0' ] ) ; } return cnt ; } int main ( ) { string str = "56" ; int n = str . length ( ) ; cout << countSticks ( str , n ) ; return 0 ; }
Print characters in decreasing order of frequency | C ++ implementation of the approach ; Function to print the characters of the given string in decreasing order of their frequencies ; To store the ; Map 's size ; While there are elements in the map ; Finding the maximum value from the map ; Print the character alongwith its frequency ; Delete the maximum value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printChar ( string str , int len ) { unordered_map < char , int > occ ; for ( int i = 0 ; i < len ; i ++ ) occ [ str [ i ] ] ++ ; int size = occ . size ( ) ; unordered_map < char , int > :: iterator it ; while ( size -- ) { unsigned currentMax = 0 ; char arg_max ; for ( it = occ . begin ( ) ; it != occ . end ( ) ; ++ it ) { if ( it -> second > currentMax || ( it -> second == currentMax && it -> first > arg_max ) ) { arg_max = it -> first ; currentMax = it -> second ; } } cout << arg_max << " ▁ - ▁ " << currentMax << endl ; occ . erase ( arg_max ) ; } } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; printChar ( str , len ) ; return 0 ; }
Modulo of a large Binary String | C ++ implementation of the approach ; Function to return the value of ( str % k ) ; pwrTwo [ i ] will store ( ( 2 ^ i ) % k ) ; To store the result ; If current bit is 1 ; Add the current power of 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMod ( string str , int n , int k ) { int pwrTwo [ n ] ; pwrTwo [ 0 ] = 1 % k ; for ( int i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } int res = 0 ; int i = 0 , j = n - 1 ; while ( i < n ) { if ( str [ j ] == '1' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } int main ( ) { string str = "1101" ; int n = str . length ( ) ; int k = 45 ; cout << getMod ( str , n , k ) << endl ; }
Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1 's | C ++ implementation of the approach ; Function to return the count of all possible valid strings ; Fill 0 's in the dp array ; Base cases ; dp [ i ] [ j ] = number of possible strings such that '1' just appeared consecutively j times upto the ith index ; Taking previously calculated value ; Taking all possible cases that can appear at the Nth position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const long MOD = 1000000007 ; long countStrings ( long N ) { long dp [ N + 1 ] [ 3 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD ; } long ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD ; return ans ; } int main ( ) { long N = 3 ; cout << countStrings ( N ) ; return 0 ; }
Find the maximum possible Binary Number from given string | C ++ implementation of the approach ; Function to return maximum number that can be formed from the string ; To store the frequency of ' z ' and ' n ' in the given string ; Number of zeroes ; Number of ones ; To store the required number ; Add all the ones ; Add all the zeroes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string maxNumber ( string str , int n ) { int freq [ 2 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' z ' ) { freq [ 0 ] ++ ; } else if ( str [ i ] == ' n ' ) { freq [ 1 ] ++ ; } } string num = " " ; for ( int i = 0 ; i < freq [ 1 ] ; i ++ ) num += '1' ; for ( int i = 0 ; i < freq [ 0 ] ; i ++ ) num += '0' ; return num ; } int main ( ) { string str = " roenenzooe " ; int n = str . length ( ) ; cout << maxNumber ( str , n ) ; return 0 ; }
Contiguous unique substrings with the given length L | C ++ implementation ; Function to print the unique sub - string of length n ; set to store the strings ; if the size of the string is equal to 1 then insert ; inserting unique sub - string of length L ; Printing the set of strings ; Driver Code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; void result ( string s , int n ) { unordered_set < string > st ; for ( int i = 0 ; i < ( int ) s . size ( ) ; i ++ ) { string ans = " " ; for ( int j = i ; j < ( int ) s . size ( ) ; j ++ ) { ans += s [ j ] ; if ( ans . size ( ) == n ) { st . insert ( ans ) ; break ; } } } for ( auto it : st ) cout << it << " ▁ " ; } int main ( ) { string s = " abca " ; int n = 3 ; result ( s , n ) ; return 0 ; }
Find the occurrence of the given binary pattern in the binary representation of the array elements | C ++ implementation of the approach ; Function to return the binary representation of n ; Array to store binary representation ; Counter for binary array ; Storing remainder in binary array ; To store the binary representation as a string ; Function to return the frequency of pat in the given string txt ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Function to find the occurrence of the given pattern in the binary representation of elements of arr [ ] ; For every element of the array ; Find its binary representation ; Print the occurrence of the given pattern in its binary representation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decToBinary ( int n ) { int binaryNum [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } string binary = " " ; for ( int j = i - 1 ; j >= 0 ; j -- ) binary += to_string ( binaryNum [ j ] ) ; return binary ; } int countFreq ( string & pat , string & txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int res = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) if ( txt [ i + j ] != pat [ j ] ) break ; if ( j == M ) { res ++ ; j = 0 ; } } return res ; } void findOccurrence ( int arr [ ] , int n , string pattern ) { for ( int i = 0 ; i < n ; i ++ ) { string binary = decToBinary ( arr [ i ] ) ; cout << countFreq ( pattern , binary ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 106 , 7 , 8 } ; string pattern = "10" ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findOccurrence ( arr , n , pattern ) ; return 0 ; }
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | CPP implementation of the approach ; Function that returns true if the sequence can be balanced by changing the position of at most one bracket ; Odd length string can never be balanced ; Add ' ( ' in the beginning and ' ) ' in the end of the string ; If its an opening bracket then append it to the temp string ; If its a closing bracket ; There was an opening bracket to match it with ; No opening bracket to match it with ; Sequence is balanced ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeBalanced ( string s , int n ) { if ( n % 2 == 1 ) return false ; string k = " ( " ; k += s + " ) " ; vector < string > d ; int cnt = 0 ; for ( int i = 0 ; i < k . length ( ) ; i ++ ) { if ( k [ i ] == ' ( ' ) d . push_back ( " ( " ) ; else { if ( d . size ( ) != 0 ) d . pop_back ( ) ; else return false ; } } if ( d . empty ( ) ) return true ; return false ; } int main ( int argc , char const * argv [ ] ) { string s = " ) ( ( ) " ; int n = s . length ( ) ; ( canBeBalanced ( s , n ) ) ? cout << " Yes " << endl : cout << " No " << endl ; return 0 ; }
Find the winner of the game | C ++ implementation of the approach ; Function to find the winner of the game ; To store the strings for both the players ; If the index is even ; Append the current character to player A 's string ; If the index is odd ; Append the current character to player B 's string ; Sort both the strings to get the lexicographically smallest string possible ; Copmpare both the strings to find the winner of the game ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_winner ( string str , int n ) { string str1 = " " , str2 = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { str1 += str [ i ] ; } else { str2 += str [ i ] ; } } sort ( str1 . begin ( ) , str1 . end ( ) ) ; sort ( str2 . begin ( ) , str2 . end ( ) ) ; if ( str1 < str2 ) cout << " A " ; else if ( str2 < str1 ) cout << " B " ; else cout << " Tie " ; } int main ( ) { string str = " geeksforgeeks " ; int n = str . length ( ) ; find_winner ( str , n ) ; return 0 ; }
Count of characters in str1 such that after deleting anyone of them str1 becomes str2 | Below is C ++ implementation of the approach ; Function to return the count of required indices ; Solution doesn 't exist ; Find the length of the longest common prefix of strings ; Find the length of the longest common suffix of strings ; If solution does not exist ; Return the count of indices ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Find_Index ( string str1 , string str2 ) { int n = str1 . size ( ) ; int m = str2 . size ( ) ; int l = 0 ; int r = 0 ; if ( n != m + 1 ) { return -1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( str1 [ i ] == str2 [ i ] ) { l += 1 ; } else { break ; } } int i = n - 1 ; int j = m - 1 ; while ( i >= 0 && j >= 0 && str1 [ i ] == str2 [ j ] ) { r += 1 ; i -= 1 ; j -= 1 ; } if ( l + r < m ) { return -1 ; } else { i = max ( n - r , 1 ) ; j = min ( l + 1 , n ) ; return ( j - i + 1 ) ; } } int main ( ) { string str1 = " aaa " , str2 = " aa " ; cout << Find_Index ( str1 , str2 ) ; return 0 ; }
Expand the string according to the given conditions | C ++ implementation of the approach ; Function to expand and print the given string ; Subtract '0' to convert char to int ; Characters within brackets ; Expanding ; Reset the variables ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void expandString ( string strin ) { string temp = " " ; int j ; for ( int i = 0 ; i < strin . length ( ) ; i ++ ) { if ( strin [ i ] >= 0 ) { int num = strin [ i ] - '0' ; if ( strin [ i + 1 ] == ' ( ' ) { for ( j = i + 1 ; strin [ j ] != ' ) ' ; j ++ ) { if ( ( strin [ j ] >= ' a ' && strin [ j ] <= ' z ' ) || ( strin [ j ] >= ' A ' && strin [ j ] <= ' Z ' ) ) { temp += strin [ j ] ; } } for ( int k = 1 ; k <= num ; k ++ ) { cout << ( temp ) ; } num = 0 ; temp = " " ; if ( j < strin . length ( ) ) { i = j ; } } } } } int main ( ) { string strin = "3 ( ab ) 4 ( cd ) " ; expandString ( strin ) ; }
Largest substring of str2 which is a prefix of str1 | C ++ implementation of the approach ; Function to return the largest substring in str2 which is a prefix of str1 ; To store the index in str2 which matches the prefix in str1 ; While there are characters left in str1 ; If the prefix is not found in str2 ; Remove the last character ; Prefix found ; No substring found in str2 that matches the prefix of str1 ; Driver code
#include <iostream> NEW_LINE using namespace std ; string findPrefix ( string str1 , string str2 ) { int pos = -1 ; while ( ! str1 . empty ( ) ) { if ( str2 . find ( str1 ) == string :: npos ) str1 . pop_back ( ) ; else { pos = str2 . find ( str1 ) ; break ; } } if ( pos == -1 ) return " - 1" ; return str1 ; } int main ( ) { string str1 = " geeksfor " ; string str2 = " forgeeks " ; cout << findPrefix ( str1 , str2 ) ; return 0 ; }
Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2 | C ++ implementation of the approach ; Function to return the lexicographically smallest required string ; Initially empty string ; min heap of characters ; Length of the string ; K cannot be greater than the size of the string ; First push the first K characters into the priority_queue ; While there are characters to append ; Append the top of priority_queue to X ; Remove the top element ; Push only if i is less than the size of string ; Return the generated string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getSmallestStr ( string S , int K ) { string X = " " ; priority_queue < char , vector < char > , greater < char > > pq ; int i , n = S . length ( ) ; K = min ( K , n ) ; for ( i = 0 ; i < K ; i ++ ) pq . push ( S [ i ] ) ; while ( ! pq . empty ( ) ) { X += pq . top ( ) ; pq . pop ( ) ; if ( i < S . length ( ) ) pq . push ( S [ i ] ) ; i ++ ; } return X ; } int main ( ) { string S = " geeksforgeeks " ; int K = 5 ; cout << getSmallestStr ( S , K ) ; return 0 ; }
Round the given number to nearest multiple of 10 | Set | C ++ implementation of the approach ; Function to round the given number to the nearest multiple of 10 ; If string is empty ; If the last digit is less then or equal to 5 then it can be rounded to the nearest ( previous ) multiple of 10 by just replacing the last digit with 0 ; Set the last digit to 0 ; Print the updated number ; The number hast to be rounded to the next multiple of 10 ; To store the carry ; Replace the last digit with 0 ; Starting from the second last digit , add 1 to digits while there is carry ; While there are digits to consider and there is carry to add ; Get the current digit ; Add the carry ; If the digit exceeds 9 then the carry will be generated ; Else there will be no carry ; Update the current digit ; Get to the previous digit ; If the carry is still 1 then it must be inserted at the beginning of the string ; Prin the rest of the number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void roundToNearest ( string str , int n ) { if ( str == " " ) return ; if ( str [ n - 1 ] - '0' <= 5 ) { str [ n - 1 ] = '0' ; cout << str . substr ( 0 , n ) ; } else { int carry = 0 ; str [ n - 1 ] = '0' ; int i = n - 2 ; carry = 1 ; while ( i >= 0 && carry == 1 ) { int currentDigit = str [ i ] - '0' ; currentDigit += carry ; if ( currentDigit > 9 ) { carry = 1 ; currentDigit = 0 ; } else carry = 0 ; str [ i ] = ( char ) ( currentDigit + '0' ) ; i -- ; } if ( carry == 1 ) cout << carry ; cout << str . substr ( 0 , n ) ; } } int main ( ) { string str = "99999999999999993" ; int n = str . length ( ) ; roundToNearest ( str , n ) ; return 0 ; }
Repeat substrings of the given String required number of times | C ++ implementation of the approach ; Function that returns true if the passed character is a digit ; Function to return the next index of a non - digit character in the string starting at the index i ( returns - 1 ifno such index is found ) ; If the character at index i is a digit then skip to the next character ; If no such index was found ; Function to append str the given number of times to the StringBuilder ; Function to return the string after performing the given operations ; To build the resultant string ; Index of the first non - digit character in the string ; While there are substrings that do not consist of digits ; Find the ending of the substring ; Starting index of the number ; If no digit appears after the current substring ; Find the index at which the current number ends ; Parse the number from the substring ; Repeat the current substring required number of times ; Find the next non - digit character index ; Return the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } int nextNonDigit ( string str , int i ) { while ( i < str . length ( ) && isDigit ( str [ i ] ) ) { i ++ ; } if ( i >= str . length ( ) ) return -1 ; return i ; } void appendRepeated ( string & sb , string str , int times ) { for ( int i = 0 ; i < times ; i ++ ) sb . append ( str ) ; } string findString ( string str , int n ) { string sb = " " ; int startStr = nextNonDigit ( str , 0 ) ; while ( startStr != -1 ) { int endStr = startStr ; while ( ( endStr + 1 ) < n && ! isDigit ( str [ endStr + 1 ] ) ) { endStr ++ ; } int startNum = endStr + 1 ; if ( startNum == -1 ) break ; int endNum = startNum ; while ( ( endNum + 1 ) < n && isDigit ( str [ endNum + 1 ] ) ) { endNum ++ ; } int num = str [ startNum ] - '0' ; appendRepeated ( sb , str . substr ( startStr , endStr + 1 - startStr ) , num ) ; startStr = nextNonDigit ( str , endStr + 1 ) ; } return sb ; } int main ( ) { string str = " g1ee1ks1for1g1e2ks1" ; int n = str . length ( ) ; cout << findString ( str , n ) << endl ; return 0 ; }