codeparrot/xlcost-text-to-code huggingface dataset:
Largest divisible pairs subset | CPP program to find the largest subset which where each pair is divisible . ; function to find the longest Subsequence ; dp [ i ] is going to store size of largest divisible subset beginning with a [ i ] . ; Since last element is largest , d [ n - 1 ] is 1 ; Fill values for smaller elements . ; Find all multiples of a [ i ] and consider the multiple that has largest subset beginning with it . ; Return maximum value from dp [ ] ; driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestSubset ( int a [ ] , int n ) { int dp [ n ] ; dp [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int mxm = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] == 0 a [ i ] % a [ j ] == 0 ) mxm = max ( mxm , dp [ j ] ) ; dp [ i ] = 1 + mxm ; } return * max_element ( dp , dp + n ) ; } int main ( ) { int a [ ] = { 1 , 3 , 6 , 13 , 17 , 18 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << largestSubset ( a , n ) << endl ; return 0 ; }
How to solve a Dynamic Programming Problem ? | initialize to - 1 ; this function returns the number of arrangements to form ' n ' ; base case ; checking if already calculated ; storing the result and returning
int dp [ MAXN ] ; int solve ( int n ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != -1 ) return dp [ n ] ; return dp [ n ] = solve ( n - 1 ) + solve ( n - 3 ) + solve ( n - 5 ) ; }
Friends Pairing Problem | C ++ program for solution of friends pairing problem ; Returns count of ways n people can remain single or paired up . ; Filling dp [ ] in bottom - up manner using recursive formula explained above . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFriendsPairings ( int n ) { int dp [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i <= 2 ) dp [ i ] = i ; else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ n ] ; } int main ( ) { int n = 4 ; cout << countFriendsPairings ( n ) << endl ; return 0 ; }
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFriendsPairings ( int n ) { int a = 1 , b = 2 , c = 0 ; if ( n <= 2 ) { return n ; } for ( int i = 3 ; i <= n ; i ++ ) { c = b + ( i - 1 ) * a ; a = b ; b = c ; } return c ; } int main ( ) { int n = 4 ; cout << countFriendsPairings ( n ) ; return 0 ; }
Check whether row or column swaps produce maximum size binary sub | C ++ program to find maximum binary sub - matrix with row swaps and column swaps . ; Precompute the number of consecutive 1 below the ( i , j ) in j - th column and the number of consecutive 1 s on right side of ( i , j ) in i - th row . ; Travesing the 2d matrix from top - right . ; If ( i , j ) contain 0 , do nothing ; Counting consecutive 1 on right side ; Travesing the 2d matrix from bottom - left . ; If ( i , j ) contain 0 , do nothing ; Counting consecutive 1 down to ( i , j ) . ; Return maximum size submatrix with row swap allowed . ; Copying the column ; Sort the copied array ; Find maximum submatrix size . ; Return maximum size submatrix with column swap allowed . ; Copying the row . ; Sort the copied array ; Find maximum submatrix size . ; Solving for row swap and column swap ; Comparing both . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define R  5 NEW_LINE #define C  3 NEW_LINE using namespace std ; void precompute ( int mat [ R ] [ C ] , int ryt [ ] [ C + 2 ] , int dwn [ R + 2 ] [ C + 2 ] ) { for ( int j = C - 1 ; j >= 0 ; j -- ) { for ( int i = 0 ; i < R ; ++ i ) { if ( mat [ i ] [ j ] == 0 ) ryt [ i ] [ j ] = 0 ; else ryt [ i ] [ j ] = ryt [ i ] [ j + 1 ] + 1 ; } } for ( int i = R - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < C ; ++ j ) { if ( mat [ i ] [ j ] == 0 ) dwn [ i ] [ j ] = 0 ; else dwn [ i ] [ j ] = dwn [ i + 1 ] [ j ] + 1 ; } } } int solveRowSwap ( int ryt [ R + 2 ] [ C + 2 ] ) { int b [ R ] = { 0 } , ans = 0 ; for ( int j = 0 ; j < C ; j ++ ) { for ( int i = 0 ; i < R ; i ++ ) b [ i ] = ryt [ i ] [ j ] ; sort ( b , b + R ) ; for ( int i = 0 ; i < R ; ++ i ) ans = max ( ans , b [ i ] * ( R - i ) ) ; } return ans ; } int solveColumnSwap ( int dwn [ R + 2 ] [ C + 2 ] ) { int b [ C ] = { 0 } , ans = 0 ; for ( int i = 0 ; i < R ; ++ i ) { for ( int j = 0 ; j < C ; ++ j ) b [ j ] = dwn [ i ] [ j ] ; sort ( b , b + C ) ; for ( int i = 0 ; i < C ; ++ i ) ans = max ( ans , b [ i ] * ( C - i ) ) ; } return ans ; } void findMax1s ( int mat [ R ] [ C ] ) { int ryt [ R + 2 ] [ C + 2 ] , dwn [ R + 2 ] [ C + 2 ] ; memset ( ryt , 0 , sizeof ryt ) ; memset ( dwn , 0 , sizeof dwn ) ; precompute ( mat , ryt , dwn ) ; int rswap = solveRowSwap ( ryt ) ; int cswap = solveColumnSwap ( dwn ) ; ( rswap > cswap ) ? ( cout << " Row ▁ Swap STRNEWLINE " << rswap << endl ) : ( cout << " Column ▁ Swap STRNEWLINE " << cswap << endl ) ; } int main ( ) { int mat [ R ] [ C ] = { { 0 , 0 , 0 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } , { 0 , 0 , 0 } , { 1 , 1 , 0 } } ; findMax1s ( mat ) ; return 0 ; }
LCS ( Longest Common Subsequence ) of three strings | C ++ program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] [ o + 1 ] in bottom up fashion . Note that L [ i ] [ j ] [ k ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] and Z [ 0. . ... k - 1 ] ; L [ m ] [ n ] [ o ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] and Z [ 0. . o - 1 ] ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lcsOf3 ( string X , string Y , string Z , int m , int n , int o ) { int L [ m + 1 ] [ n + 1 ] [ o + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { for ( int k = 0 ; k <= o ; k ++ ) { if ( i == 0 j == 0 k == 0 ) L [ i ] [ j ] [ k ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] && X [ i - 1 ] == Z [ k - 1 ] ) L [ i ] [ j ] [ k ] = L [ i - 1 ] [ j - 1 ] [ k - 1 ] + 1 ; else L [ i ] [ j ] [ k ] = max ( max ( L [ i - 1 ] [ j ] [ k ] , L [ i ] [ j - 1 ] [ k ] ) , L [ i ] [ j ] [ k - 1 ] ) ; } } } return L [ m ] [ n ] [ o ] ; } int main ( ) { string X = " AGGT12" ; string Y = "12TXAYB " ; string Z = "12XBA " ; int m = X . length ( ) ; int n = Y . length ( ) ; int o = Z . length ( ) ; cout << " Length ▁ of ▁ LCS ▁ is ▁ " << lcsOf3 ( X , Y , Z , m , n , o ) ; return 0 ; }
Longest subsequence such that difference between adjacents is one | Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubsequence ( int n , int arr [ ] ) { if ( n == 1 ) return 1 ; int dp [ n ] ; unordered_map < int , int > mapp ; dp [ 0 ] = 1 ; mapp [ arr [ 0 ] ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( abs ( arr [ i ] - arr [ i - 1 ] ) == 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; else { if ( mapp . count ( arr [ i ] + 1 ) > 0 || mapp . count ( arr [ i ] - 1 ) ) { dp [ i ] = 1 + max ( mapp [ arr [ i ] + 1 ] , mapp [ arr [ i ] - 1 ] ) ; } else dp [ i ] = 1 ; } mapp [ arr [ i ] ] = dp [ i ] ; } return ( * max_element ( dp , dp + n ) ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestSubsequence ( n , arr ) ; return 0 ; }
Printing Shortest Common Supersequence | A dynamic programming based C ++ program print shortest supersequence of two strings ; returns shortest supersequence of X and Y ; dp [ i ] [ j ] contains length of shortest supersequence for X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Fill table in bottom up manner ; Below steps follow recurrence relation ; string to store the shortest supersequence ; Start from the bottom right corner and one by one push characters in output string ; If current character in X and Y are same , then current character is part of shortest supersequence ; Put current character in result ; reduce values of i , j and index ; If current character in X and Y are different ; Put current character of Y in result ; reduce values of j and index ; Put current character of X in result ; reduce values of i and index ; If Y reaches its end , put remaining characters of X in the result string ; If X reaches its end , put remaining characters of Y in the result string ; reverse the string and return it ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string printShortestSuperSeq ( string X , string Y ) { int m = X . length ( ) ; int n = Y . length ( ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } string str ; int i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { str . push_back ( X [ i - 1 ] ) ; i -- , j -- ; } else if ( dp [ i - 1 ] [ j ] > dp [ i ] [ j - 1 ] ) { str . push_back ( Y [ j - 1 ] ) ; j -- ; } else { str . push_back ( X [ i - 1 ] ) ; i -- ; } } while ( i > 0 ) { str . push_back ( X [ i - 1 ] ) ; i -- ; } while ( j > 0 ) { str . push_back ( Y [ j - 1 ] ) ; j -- ; } reverse ( str . begin ( ) , str . end ( ) ) ; return str ; } int main ( ) { string X = " AGGTAB " ; string Y = " GXTXAYB " ; cout << printShortestSuperSeq ( X , Y ) ; return 0 ; }
Longest Repeating Subsequence | C ++ program to find the longest repeating subsequence ; This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match ; Driver Program
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int findLongestRepeatingSubSeq ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ n ] ; } int main ( ) { string str = " aabb " ; cout << " The ▁ length ▁ of ▁ the ▁ largest ▁ subsequence ▁ that " " ▁ repeats ▁ itself ▁ is ▁ : ▁ " << findLongestRepeatingSubSeq ( str ) ; return 0 ; }
Count total number of N digit numbers such that the difference between sum of even and odd digits is 1 | A memoization based recursive program to count numbers with difference between odd and even digit sums as 1 ; A lookup table used for memoization . ; Memoization based recursive function to count numbers with even and odd digit sum difference as 1. This function considers leading zero as a digit ; Base Case ; If current subproblem is already computed ; Initialize result ; If the current digit is odd , then add it to odd sum and recur ; else Add to even sum and recur ; Store current result in lookup table and return the same ; This is mainly a wrapper over countRec . It explicitly handles leading digit and calls countRec ( ) for remaining digits . ; Initialize number digits considered so far ; Initialize all entries of lookup table ; Initialize final answer ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function countRec for remaining digits . Note that the first digit is considered as even digit . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long int lookup [ 50 ] [ 1000 ] [ 1000 ] [ 2 ] ; unsigned long long int countRec ( int digits , int esum , int osum , bool isOdd , int n ) { if ( digits == n ) return ( esum - osum == 1 ) ; if ( lookup [ digits ] [ esum ] [ osum ] [ isOdd ] != -1 ) return lookup [ digits ] [ esum ] [ osum ] [ isOdd ] ; unsigned long long int ans = 0 ; if ( isOdd ) for ( int i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum , osum + i , false , n ) ; for ( int i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , true , n ) ; return lookup [ digits ] [ esum ] [ osum ] [ isOdd ] = ans ; } unsigned long long int finalCount ( int n ) { int digits = 0 ; memset ( lookup , -1 , sizeof lookup ) ; unsigned long long int ans = 0 ; int esum = 0 , osum = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , true , n ) ; return ans ; } int main ( ) { int n = 3 ; cout << " Count ▁ of ▁ " << n << " ▁ digit ▁ numbers ▁ is ▁ " << finalCount ( n ) ; return 0 ; }
Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 1D array to store results of subproblems ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfPaths ( int m , int n ) { int dp [ n ] = { 1 } ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; } int main ( ) { cout << numberOfPaths ( 3 , 3 ) ; }
Longest Arithmetic Progression | DP | C ++ program to find Length of the Longest AP ( llap ) in a given sorted set . ; Returns length of the longest AP subset in a given set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Solution ( vector < int > A ) { int ans = 2 ; int n = A . size ( ) ; if ( n <= 2 ) return n ; vector < int > llap ( n , 2 ) ; sort ( A . begin ( ) , A . end ( ) ) ; for ( int j = n - 2 ; j >= 0 ; j -- ) { int i = j - 1 ; int k = j + 1 ; while ( i >= 0 && k < n ) { if ( A [ i ] + A [ k ] == 2 * A [ j ] ) { llap [ j ] = max ( llap [ k ] + 1 , llap [ j ] ) ; ans = max ( ans , llap [ j ] ) ; i -= 1 ; k += 1 ; } else if ( A [ i ] + A [ k ] < 2 * A [ j ] ) k += 1 ; else i -= 1 ; } } return ans ; } int main ( ) { vector < int > a ( { 9 , 4 , 7 , 2 , 10 } ) ; cout << Solution ( a ) << endl ; return 0 ; }
Minimum numbers to be appended such that mean of Array is equal to 1 | C ++ program for above approach ; Function to calculate minimum Number of operations ; Storing sum of array arr [ ] ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minumumOperation ( int N , int arr [ ] ) { int sum_arr = 0 ; sum_arr = accumulate ( arr , arr + N , sum_arr ) ; if ( sum_arr >= N ) cout << sum_arr - N << endl ; else cout << 1 << endl ; } int main ( ) { int N = 4 ; int arr [ ] = { 8 , 4 , 6 , 2 } ; minumumOperation ( N , arr ) ; }
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | CPP program to find N - th term of the series : 1 , 4 , 15 , 72 , 420 Aa Using recursion ; Function to find factorial of N with recursion ; base condition ; use recursion ; calculate Nth term of series ; Driver Function
#include <iostream> NEW_LINE using namespace std ; int factorial ( int N ) { if ( N == 0 N == 1 ) return 1 ; return N * factorial ( N - 1 ) ; } int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } int main ( ) { int N = 6 ; cout << nthTerm ( N ) ; return 0 ; }
Check if a string is substring of another | int counter = 0 ; pointing s2 ; Special case where character preceding the i 'th character is duplicate ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Substr ( string s2 , string s1 ) { int i = 0 ; for ( ; i < s1 . length ( ) ; i ++ ) { if ( counter == s2 . length ( ) ) break ; if ( s2 [ counter ] == s1 [ i ] ) { counter ++ ; } else { if ( counter > 0 ) { i -= counter ; } counter = 0 ; } } return counter < s2 . length ( ) ? -1 : i - counter ; } int main ( ) { string s1 = " geeksfffffoorrfoorforgeeks " ; cout << Substr ( " for " , s1 ) ; return 0 ; }
Count anagrams having first character as a consonant and no pair of consonants or vowels placed adjacently | C ++ program for the above approach ; Function to compute factorials till N ; Iterate in the range [ 1 , N ] ; Update ans to ans * i ; Store the value of ans in fac [ i ] ; Function to check whether the current character is a vowel or not ; Function to count the number of anagrams of S satisfying the given condition ; Store the factorials upto N ; Function Call to generate all factorials upto n ; Create a hashmap to store frequencies of all characters ; Store the count of vowels and consonants ; Iterate through all characters in the string ; Update the frequency of current character ; Check if the character is vowel or consonant ; Check if C == V + 1 or C == V ; Store the denominator ; Calculate the denominator of the expression ; Multiply denominator by factorial of counts of all letters ; Store the numerator ; Store the answer by dividing numerator by denominator ; Print the answer ; Otherwise , print 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE #define mod  1000000007 NEW_LINE #define N  1000001 NEW_LINE using namespace std ; void Precomputefact ( unordered_map < ll , ll > & fac ) { ll ans = 1 ; for ( ll i = 1 ; i <= N ; i ++ ) { ans = ( ans * i ) % mod ; fac [ i ] = ans ; } return ; } bool isVowel ( char a ) { if ( a == ' A ' a == ' E ' a == ' I ' a == ' O ' a == ' U ' ) return true ; else return false ; } void countAnagrams ( string s , int n ) { unordered_map < ll , ll > fac ; Precomputefact ( fac ) ; unordered_map < char , ll > count ; int vo = 0 , co = 0 ; for ( int i = 0 ; i < n ; i ++ ) { count [ s [ i ] ] ++ ; if ( isVowel ( s [ i ] ) ) vo ++ ; else co ++ ; } if ( ( co == vo + 1 ) || ( co == vo ) ) { ll deno = 1 ; for ( auto c : count ) { deno = ( deno * fac [ c . second ] ) % mod ; } ll nume = fac [ co ] % mod ; nume = ( nume * fac [ vo ] ) % mod ; ll ans = nume / deno ; cout << ans ; } else { cout << 0 ; } } int main ( ) { string S = " GADO " ; int l = S . size ( ) ; countAnagrams ( S , l ) ; return 0 ; }
Hamming code Implementation in C / C ++ | C ++ program for the above approach ; Function to generate hamming code ; Stores the Hamming Code ; Find positions of redundant bits ; Placing - 1 at redundant bits place to identify it later ; Iterate to update the code ; Placing msgBits where - 1 is absent i . e . , except redundant bits all positions are msgBits ; If current bit is not redundant bit then continue ; Find msg bits containing set bit at x 'th position ; Generating hamming code for even parity ; Return the generated code ; Function to find the hamming code of the given message bit msgBit [ ] ; Message bit size ; r is the number of redundant bits ; Find no . of redundant bits ; Generating Code ; Print the code ; Driver Code ; Given message bits ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > generateHammingCode ( vector < int > msgBits , int m , int r ) { vector < int > hammingCode ( r + m ) ; for ( int i = 0 ; i < r ; ++ i ) { hammingCode [ pow ( 2 , i ) - 1 ] = -1 ; } int j = 0 ; for ( int i = 0 ; i < ( r + m ) ; i ++ ) { if ( hammingCode [ i ] != -1 ) { hammingCode [ i ] = msgBits [ j ] ; j ++ ; } } for ( int i = 0 ; i < ( r + m ) ; i ++ ) { if ( hammingCode [ i ] != -1 ) continue ; int x = log2 ( i + 1 ) ; int one_count = 0 ; for ( int j = i + 2 ; j <= ( r + m ) ; ++ j ) { if ( j & ( 1 << x ) ) { if ( hammingCode [ j - 1 ] == 1 ) { one_count ++ ; } } } if ( one_count % 2 == 0 ) { hammingCode [ i ] = 0 ; } else { hammingCode [ i ] = 1 ; } } return hammingCode ; } void findHammingCode ( vector < int > & msgBit ) { int m = msgBit . size ( ) ; int r = 1 ; while ( pow ( 2 , r ) < ( m + r + 1 ) ) { r ++ ; } vector < int > ans = generateHammingCode ( msgBit , m , r ) ; cout << " Message ▁ bits ▁ are : ▁ " ; for ( int i = 0 ; i < msgBit . size ( ) ; i ++ ) cout << msgBit [ i ] << " ▁ " ; cout << " Hamming code is : " ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] << " ▁ " ; } int main ( ) { vector < int > msgBit = { 0 , 1 , 0 , 1 } ; findHammingCode ( msgBit ) ; return 0 ; }
Modify string by increasing each character by its distance from the end of the word | C ++ implementation of the above approach ; Function to transform and return the transformed word ; Stores resulting word ; Iterate over the word ; Add the position value to the letter ; Convert it back to character ; Add it to the string ; Function to transform the given string ; Size of string ; Stores resultant string ; Iterate over given string ; End of word is reached ; Append the word ; For the last word ; Driver code ; Given string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string util ( string sub ) { int n = sub . length ( ) ; int i = 0 ; string ret = " " ; while ( i < n ) { int t = ( sub [ i ] - ' a ' ) + n - 1 - i ; char ch = ( char ) ( t % 26 + 97 ) ; ret = ret + ch ; i ++ ; } return ret ; } void manipulate ( string s ) { int n = s . length ( ) ; int i = 0 , j = 0 ; string res = " " ; while ( i < n ) { if ( s [ i ] == ' ▁ ' ) { res += util ( s . substr ( j , i ) ) ; res = res + " ▁ " ; j = i + 1 ; i = j + 1 ; } else { i ++ ; } } res = res + util ( s . substr ( j , i ) ) ; cout << res << endl ; } int main ( ) { string s = " acm ▁ fkz " ; manipulate ( s ) ; return 0 ; }
Check if given words are present in a string | Find the small string at that index in the array of small strings is contained in the big string ; Function to the multiStringSearch ; iterate in the smallString ; calling the isInBigString Function ; Function to the bigString ; iterate in the bigString ; Check if length of smallString + i is greater than the length of bigString ; call the function isInBigStringHelper ; Helper Function to the Finding bigString ; Initialize leftBigIdx and rightBigIdx and leftSmallIdx variables ; Iterate until leftBigIdx variable reaches less than or equal to rightBigIdx ; Check if bigString [ leftBigIdx ] is not equal to smallString [ leftSmallIdx ] or Check if bigString [ rightBigIdx ] is not equal to smallString [ rightSmallIdx ] than return false otherwise increment leftBigIdx and leftSmallIdx decrement rightBigIdx and rightSmallIdx ; Driver code ; initialize string ; initialize vector string ; Function call ; Print answers ; Check if ans [ i ] is equal to 1 then Print true otherwise print false
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isInBigString ( string bigString , string smallString ) ; bool isInBigStringHelper ( string bigString , string smallString , int startIdx ) ; vector < bool > multiStringSearch ( string bigString , vector < string > smallStrings ) { vector < bool > solution ; for ( string smallString : smallStrings ) { solution . push_back ( isInBigString ( bigString , smallString ) ) ; } return solution ; } bool isInBigString ( string bigString , string smallString ) { for ( int i = 0 ; i < bigString . length ( ) ; i ++ ) { if ( i + smallString . length ( ) > bigString . length ( ) ) { break ; } if ( isInBigStringHelper ( bigString , smallString , i ) ) { return true ; } } return false ; } bool isInBigStringHelper ( string bigString , string smallString , int startIdx ) { int leftBigIdx = startIdx ; int rightBigIdx = startIdx + smallString . length ( ) - 1 ; int leftSmallIdx = 0 ; int rightSmallIdx = smallString . length ( ) - 1 ; while ( leftBigIdx <= rightBigIdx ) { if ( bigString [ leftBigIdx ] != smallString [ leftSmallIdx ] bigString [ rightBigIdx ] != smallString [ rightSmallIdx ] ) { return false ; } leftBigIdx ++ ; rightBigIdx -- ; leftSmallIdx ++ ; rightSmallIdx -- ; } return true ; } int main ( int argc , char * argv [ ] ) { string str = " this ▁ is ▁ a ▁ big ▁ string " ; vector < string > substr = { " this " , " yo " , " is " , " a " , " bigger " , " string " , " kappa " } ; vector < bool > ans = multiStringSearch ( str , substr ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans [ i ] == 1 ) { cout << " true " << " ▁ " ; } else { cout << " false " << " ▁ " ; } } return 0 ; }