codeparrot/xlcost-text-to-code huggingface dataset:
Boyer Moore Algorithm | Good Suffix heuristic | C program for Boyer Moore Algorithm with Good Suffix heuristic to find pattern in given text string ; preprocessing for strong good suffix rule ; m is the length of pattern ; if character at position i - 1 is not equivalent to character at j - 1 , then continue searching to right of the pattern for border ; the character preceding the occurrence of t in pattern P is different than the mismatching character in P , we stop skipping the occurrences and shift the pattern from i to j ; Update the position of next border ; p [ i - 1 ] matched with p [ j - 1 ] , border is found . store the beginning position of border ; Preprocessing for case 2 ; set the border position of the first character of the pattern to all indices in array shift having shift [ i ] = 0 ; suffix becomes shorter than bpos [ 0 ] , use the position of next widest border as value of j ; Search for a pattern in given text using Boyer Moore algorithm with Good suffix rule ; s is shift of the pattern with respect to text ; initialize all occurrence of shift to 0 ; do preprocessing ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at the current shift , then index j will become - 1 after the above loop ; pat [ i ] != pat [ s + j ] so shift the pattern shift [ j + 1 ] times ; Driver
#include <stdio.h> NEW_LINE #include <string.h> NEW_LINE void preprocess_strong_suffix ( int * shift , int * bpos , char * pat , int m ) { int i = m , j = m + 1 ; bpos [ i ] = j ; while ( i > 0 ) { while ( j <= m && pat [ i - 1 ] != pat [ j - 1 ] ) { if ( shift [ j ] == 0 ) shift [ j ] = j - i ; j = bpos [ j ] ; } i -- ; j -- ; bpos [ i ] = j ; } } void preprocess_case2 ( int * shift , int * bpos , char * pat , int m ) { int i , j ; j = bpos [ 0 ] ; for ( i = 0 ; i <= m ; i ++ ) { if ( shift [ i ] == 0 ) shift [ i ] = j ; if ( i == j ) j = bpos [ j ] ; } } void search ( char * text , char * pat ) { int s = 0 , j ; int m = strlen ( pat ) ; int n = strlen ( text ) ; int bpos [ m + 1 ] , shift [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) shift [ i ] = 0 ; preprocess_strong_suffix ( shift , bpos , pat , m ) ; preprocess_case2 ( shift , bpos , pat , m ) ; while ( s <= n - m ) { j = m - 1 ; while ( j >= 0 && pat [ j ] == text [ s + j ] ) j -- ; if ( j < 0 ) { printf ( " pattern ▁ occurs ▁ at ▁ shift ▁ = ▁ % d STRNEWLINE " , s ) ; s += shift [ 0 ] ; } else s += shift [ j + 1 ] ; } } int main ( ) { char text [ ] = " ABAAAABAACD " ; char pat [ ] = " ABA " ; search ( text , pat ) ; return 0 ; }
Maximum length prefix of one string that occurs as subsequence in another | C ++ program to find maximum length prefix of one string occur as subsequence in another string . ; Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPrefix ( char s [ ] , char t [ ] ) { int count = 0 ; for ( int i = 0 ; i < strlen ( t ) ; i ++ ) { if ( count == strlen ( s ) ) break ; if ( t [ i ] == s [ count ] ) count ++ ; } return count ; } int main ( ) { char S [ ] = " digger " ; char T [ ] = " biggerdiagram " ; cout << maxPrefix ( S , T ) << endl ; return 0 ; }
Replace all occurrences of string AB with C without using extra space | C ++ program to replace all occurrences of " AB " with " C " ; Start traversing from second character ; If previous character is ' A ' and current character is 'B" ; Replace previous character with ' C ' and move all subsequent characters one position back ; Driver code
#include <bits/stdc++.h> NEW_LINE void translate ( char * str ) { if ( str [ 0 ] == ' ' ) return ; for ( int i = 1 ; str [ i ] != ' ' ; i ++ ) { if ( str [ i - 1 ] == ' A ' && str [ i ] == ' B ' ) { str [ i - 1 ] = ' C ' ; for ( int j = i ; str [ j ] != ' ' ; j ++ ) str [ j ] = str [ j + 1 ] ; } } return ; } int main ( ) { char str [ ] = " helloABworldABGfG " ; translate ( str ) ; printf ( " The ▁ modified ▁ string ▁ is ▁ : STRNEWLINE " ) ; printf ( " % s " , str ) ; }
Find all occurrences of a given word in a matrix | Program to find all occurrences of the word in a matrix ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 8 neighboursof a given cell ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; return if current character doesn 't match with  the next character in the word ; append current character position to path ; current character matches with the last character in the word ; Recur for all connected neighbours ; The main function to find all occurrences of the word in a matrix ; traverse through the all cells of given matrix ; occurrence of first character in matrix ; check and print if path exists ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ROW  3 NEW_LINE #define COL  5 NEW_LINE bool isvalid ( int row , int col , int prevRow , int prevCol ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ! ( row == prevRow && col == prevCol ) ; } int rowNum [ ] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 } ; int colNum [ ] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 } ; void DFS ( char mat [ ] [ COL ] , int row , int col , int prevRow , int prevCol , char * word , string path , int index , int n ) { if ( index > n mat [ row ] [ col ] != word [ index ] ) return ; path += string ( 1 , word [ index ] ) + " ( " + to_string ( row ) + " , ▁ " + to_string ( col ) + " ) ▁ " ; if ( index == n ) { cout << path << endl ; return ; } for ( int k = 0 ; k < 8 ; ++ k ) if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) ; } void findWords ( char mat [ ] [ COL ] , char * word , int n ) { for ( int i = 0 ; i < ROW ; ++ i ) for ( int j = 0 ; j < COL ; ++ j ) if ( mat [ i ] [ j ] == word [ 0 ] ) DFS ( mat , i , j , -1 , -1 , word , " " , 0 , n ) ; } int main ( ) { char mat [ ROW ] [ COL ] = { { ' B ' , ' N ' , ' E ' , ' Y ' , ' S ' } , { ' H ' , ' E ' , ' D ' , ' E ' , ' S ' } , { ' S ' , ' G ' , ' N ' , ' D ' , ' E ' } } ; char word [ ] = " DES " ; findWords ( mat , word , strlen ( word ) - 1 ) ; return 0 ; }
Generate N Random Hexadecimal Numbers | C ++ program for the above approach ; Maximum length of the random integer ; Function to generate N Hexadecimal integers ; Stores all the possible charcters in the Hexadecimal notation ; Loop to print N integers ; Randomly select length of the int in the range [ 1 , maxSize ] ; Print len charcters ; Print a randomly selected character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int maxSize = 10 ; void randomHexInt ( int N ) { srand ( time ( 0 ) ) ; char hexChar [ ] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' } ; for ( int i = 0 ; i < N ; i ++ ) { int len = rand ( ) % maxSize + 1 ; for ( int j = 0 ; j < len ; j ++ ) { cout << hexChar [ rand ( ) % 16 ] ; } cout << ' ' ; } } int main ( ) { int N = 3 ; randomHexInt ( N ) ; return 0 ; }
Check if two array of string are equal by performing swapping operations | C ++ program for above approach ; Function to check whether brr [ ] can be made equal to arr [ ] by doing swapping operations ; siz variable to store siz of string ; iterate till N to sort strings ; sort each string in arr [ ] ; sort each string in brr [ ] ; Sort both the vectors so that all the comparable strings will be arranged ; iterate till N to compare string in arr [ ] and brr [ ] ; Compare each string ; Return false becaues if atleast one string is not equal then brr [ ] cannot be converted to arr [ ] ; All the strings are compared so at last return true . ; Driver code ; Store the answer in variable
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEqualArrays ( vector < string > arr , vector < string > brr , int N ) { int siz = arr [ 0 ] . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { sort ( arr [ i ] . begin ( ) , arr [ i ] . end ( ) ) ; sort ( brr [ i ] . begin ( ) , brr [ i ] . end ( ) ) ; } sort ( arr . begin ( ) , arr . end ( ) ) ; sort ( brr . begin ( ) , brr . end ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != brr [ i ] ) { return false ; } } return true ; } int main ( ) { int N = 2 ; vector < string > arr = { " bcd " , " aac " } ; vector < string > brr = { " aca " , " dbc " } ; bool ans = checkEqualArrays ( arr , brr , N ) ; if ( ans ) cout << " true " ; else cout << " false " ; return 0 ; }
Maximum length of string formed by concatenation having even frequency of each character | C ++ implementation of the above approach ; Function to check the string ; Count the frequency of the string ; Check the frequency of the string ; Store the length of the new String ; Function to find the longest concatenated string having every character of even frequency ; Checking the string ; Dont Include the string ; Include the string ; Driver code ; Call the function ; Print the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxi = 0 ; string ans1 = " " ; void calculate ( string ans ) { int dp [ 26 ] = { 0 } ; for ( int i = 0 ; i < ans . length ( ) ; ++ i ) { dp [ ans [ i ] - ' A ' ] ++ ; } for ( int i = 0 ; i < 26 ; ++ i ) { if ( dp [ i ] % 2 == 1 ) { return ; } } if ( maxi < ans . length ( ) ) { maxi = ans . length ( ) ; ans1 = ans ; } } void longestString ( vector < string > arr , int index , string str ) { if ( index == arr . size ( ) ) { return ; } longestString ( arr , index + 1 , str ) ; str += arr [ index ] ; calculate ( str ) ; longestString ( arr , index + 1 , str ) ; } int main ( ) { vector < string > A = { " ABAB " , " ABF " , " CDA " , " AD " , " CCC " } ; longestString ( A , 0 , " " ) ; cout << ans1 << " ▁ " << ans1 . length ( ) ; return 0 ; }
Find the next number by adding natural numbers in order on alternating indices from last | C ++ program for the above approach ; Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string generateNumber ( string number ) { int temp = 0 , adding_number = 0 ; string result = " " ; int len = number . size ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { int digit = number [ i ] - '0' ; if ( temp % 2 == 0 ) { adding_number += 1 ; digit += adding_number ; if ( digit >= 10 ) { digit %= 9 ; if ( digit == 0 ) digit = 9 ; } } result = to_string ( digit ) + result ; temp += 1 ; } return result ; } int main ( ) { string S = "1345" ; cout << generateNumber ( S ) ; return 0 ; }
Find the single digit sum of alphabetical values of a string | C ++ program for the above approach ; Traverse the given string ; If character is an alphabet ; Stores the sum of order of values ; Find the score ; Find the single digit sum ; Return the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findTheSum ( string str ) { string alpha ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) alpha . push_back ( str [ i ] ) ; } int score = 0 , n = 0 ; for ( int i = 0 ; i < alpha . length ( ) ; i ++ ) { if ( alpha [ i ] >= ' A ' && alpha [ i ] <= ' Z ' ) score += alpha [ i ] - ' A ' + 1 ; else score += alpha [ i ] - ' a ' + 1 ; } while ( score > 0 n > 9 ) { if ( score == 0 ) { score = n ; n = 0 ; } n += score % 10 ; score /= 10 ; } return n ; } int main ( ) { string S = " GeeksforGeeks " ; cout << findTheSum ( S ) ; return 0 ; }
Minimum value of K such that each substring of size K has the given character | C ++ program for the above approach ; Function to find the minimum value of K such that char c occurs in all K sized substrings of string S ; Store the string length ; Store difference of lengths of segments of every two consecutive occurrences of c ; Stores the maximum difference ; Store the previous occurence of char c ; Check if the current character is c or not ; Stores the difference of consecutive occurrences of c ; Update previous occurrence of c with current occurence ; Comparing diff with max ; If string doesn 't contain c ; Return max ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findK ( string s , char c ) { int n = s . length ( ) ; int diff ; int max = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == c ) { diff = i - prev ; prev = i ; if ( diff > max ) { max = diff ; } } } if ( max == 0 ) return -1 ; return max ; } int main ( ) { string S = " abdegb " ; char ch = ' b ' ; cout << findK ( S , ch ) ; return 0 ; }
Count subsequences 01 in string generated by concatenation of given numeric string K times | C ++ program for the above approach ; Function to calculate the number of subsequences of "01" ; Store count of 0 ' s ▁ and ▁ 1' s ; Count of subsequences without concatenation ; Case 1 ; Case 2 ; Return the total count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubsequence ( string S , int N , int K ) { int C = 0 , C1 = 0 , C0 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) C1 ++ ; else if ( S [ i ] == '0' ) C0 ++ ; } int B1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) B1 ++ ; else if ( S [ i ] == '0' ) C = C + ( C1 - B1 ) ; } int ans = C * K ; ans += ( C1 * C0 * ( ( ( K ) * ( K - 1 ) ) / 2 ) ) ; return ans ; } int main ( ) { string S = "230013110087" ; int K = 2 ; int N = S . length ( ) ; cout << countSubsequence ( S , N , K ) ; return 0 ; }
Make Palindrome binary string with exactly a 0 s and b 1 s by replacing wild card ? | C ++ program for the above approach ; Function to convert the given string to a palindrome with a 0 ' s ▁ and ▁ b ▁ 1' s ; Stores the size of the string ; Loop to iterate over the string ; If one of S [ i ] or S [ N - i - 1 ] is equal to ' ? ' , replace it with corresponding character ; Subtract the count of 0 from the required number of zeroes ; Subtract the count of 1 from required number of ones ; Traverse the string ; If both S [ i ] and S [ N - i - 1 ] are ' ? ' ; If a is greater than b ; Update S [ i ] and S [ N - i - 1 ] to '0' ; Update the value of a ; Update S [ i ] and S [ N - i - 1 ] to '1' ; Update the value of b ; Case with middle character ' ? ' in case of odd length string ; If a is greater than b ; Update middle character with '0' ; Update middle character by '1' ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string convertString ( string S , int a , int b ) { int N = S . size ( ) ; for ( int i = 0 ; i < N / 2 ; ++ i ) { if ( S [ i ] == ' ? ' && S [ N - i - 1 ] != ' ? ' ) { S [ i ] = S [ N - i - 1 ] ; } else if ( S [ i ] != ' ? ' && S [ N - i - 1 ] == ' ? ' ) { S [ N - i - 1 ] = S [ i ] ; } } a = a - count ( S . begin ( ) , S . end ( ) , '0' ) ; b = b - count ( S . begin ( ) , S . end ( ) , '1' ) ; for ( int i = 0 ; i < N / 2 ; ++ i ) { if ( S [ i ] == ' ? ' && S [ N - i - 1 ] == ' ? ' ) { if ( a > b ) { S [ i ] = S [ N - i - 1 ] = '0' ; a -= 2 ; } else { S [ i ] = S [ N - i - 1 ] = '1' ; b -= 2 ; } } } if ( S [ N / 2 ] == ' ? ' ) { if ( a > b ) { S [ N / 2 ] = '0' ; a -- ; } else { S [ N / 2 ] = '1' ; b -- ; } } if ( a == 0 && b == 0 ) { return S ; } else { return " - 1" ; } } int main ( ) { string S = "10 ? ? ? ? ?1" ; int a = 4 , b = 4 ; cout << convertString ( S , a , b ) ; return 0 ; }
Count of distinct groups of strings formed after performing equivalent operation | C ++ program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; Function to perform union operation of disjoint set union ; Find the parent of node a and b ; Update the rank ; Function to find the number of distinct strings after performing the given operations ; Stores the parent elements of the sets ; Stores the rank of the sets ; Update parent [ i ] to i ; Stores the total characters traversed through the strings ; Stores the current characters traversed through a string ; Update current [ i ] to false ; Update current [ ch - ' a ' ] to true ; Check if current [ j ] is true ; Update total [ j ] to true ; Add arr [ i ] [ 0 ] - ' a ' and j elements to same set ; Stores the count of distinct strings ; Check total [ i ] is true and parent of i is i only ; Increment the value of distCount by 1 ; Print the value of distCount ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Find ( vector < int > & parent , int a ) { return parent [ a ] = ( parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } void Union ( vector < int > & parent , vector < int > & rank , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( rank [ a ] == rank [ b ] ) rank [ a ] ++ ; if ( rank [ a ] > rank [ b ] ) parent [ b ] = a ; else parent [ a ] = b ; } void numOfDistinctStrings ( string arr [ ] , int N ) { vector < int > parent ( 27 ) ; vector < int > rank ( 27 , 0 ) ; for ( int j = 0 ; j < 27 ; j ++ ) { parent [ j ] = j ; } vector < bool > total ( 26 , false ) ; vector < bool > current ( 26 , false ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { current [ j ] = false ; } for ( char ch : arr [ i ] ) { current [ ch - ' a ' ] = true ; } for ( int j = 0 ; j < 26 ; j ++ ) { if ( current [ j ] ) { total [ j ] = true ; Union ( parent , rank , arr [ i ] [ 0 ] - ' a ' , j ) ; } } } int distCount = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( total [ i ] && Find ( parent , i ) == i ) { distCount ++ ; } } cout << distCount << endl ; } int main ( ) { string arr [ ] = { " a " , " ab " , " b " , " d " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; numOfDistinctStrings ( arr , N ) ; return 0 ; }
Minimum adjacent swaps required to make a binary string alternating | C ++ program for the above approach ; Function to find the minimum number of adjacent swaps to make the string alternating ; Count the no of zeros and ones ; Base Case ; Store no of min swaps when string starts with "1" ; Keep track of the odd positions ; Checking for when the string starts with "1" ; Adding the no of swaps to fix "1" at odd positions ; Store no of min swaps when string starts with "0" ; Keep track of the odd positions ; Checking for when the string starts with "0" ; Adding the no of swaps to fix "1" at odd positions ; Returning the answer based on the conditions when string length is even ; When string length is odd ; When no of ones is greater than no of zeros ; When no of ones is greater than no of zeros ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSwaps ( string s ) { int ones = 0 , zeros = 0 ; int N = s . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) ones ++ ; else zeros ++ ; } if ( ( N % 2 == 0 && ones != zeros ) || ( N % 2 == 1 && abs ( ones - zeros ) != 1 ) ) { return -1 ; } int ans_1 = 0 ; int j = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) { ans_1 += abs ( j - i ) ; j += 2 ; } } int ans_0 = 0 ; int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '0' ) { ans_0 += abs ( k - i ) ; k += 2 ; } } if ( N % 2 == 0 ) return min ( ans_1 , ans_0 ) ; else { if ( ones > zeros ) return ans_1 ; else return ans_0 ; } } int main ( ) { string S = "110100" ; cout << minSwaps ( S ) ; return 0 ; }
Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time | C ++ program for the above approach ; Function to find all the ways to reach Nth stair using one or two jumps ; Base Cases ; Recur for jump1 and jump2 ; Stores the total possible jumps ; Add "1" with every element present in jump1 ; Add "2" with every element present in jump2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > TotalPossibleJumps ( int N ) { if ( ( N - 1 ) == 0 ) { vector < string > newvec ; newvec . push_back ( " " ) ; return newvec ; } else { if ( N < 0 ) { vector < string > newvec ; return newvec ; } } vector < string > jump1 = TotalPossibleJumps ( N - 1 ) ; vector < string > jump2 = TotalPossibleJumps ( N - 2 ) ; vector < string > totaljumps ; for ( string s : jump1 ) { totaljumps . push_back ( "1" + s ) ; } for ( string s : jump2 ) { totaljumps . push_back ( "2" + s ) ; } return totaljumps ; } int main ( ) { int N = 3 ; vector < string > Ans = TotalPossibleJumps ( N ) ; for ( auto & it : Ans ) cout << it << ' ' ; return 0 ; }
Find any permutation of Binary String of given size not present in Array | C ++ Program for the above approach ; Function to find a binary string of N bits that does not occur in the given array arr [ ] ; Stores the resultant string ; Loop to iterate over all the given strings in a diagonal order ; Append the complement of element at current index into ans ; Return Answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findString ( vector < string > & arr , int N ) { string ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { ans += arr [ i ] [ i ] == '0' ? '1' : '0' ; } return ans ; } int main ( ) { vector < string > arr { "111" , "011" , "001" } ; int N = arr . size ( ) ; cout << findString ( arr , N ) ; return 0 ; }
Check if the string has a reversible equal substring at the ends | C ++ program for the above approach ; Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void commonSubstring ( string s ) { int n = s . size ( ) ; int i = 0 ; int j = n - 1 ; string ans = " " ; while ( j >= 0 ) { if ( s [ i ] == s [ j ] ) { ans += s [ i ] ; i ++ ; j -- ; } else { break ; } } if ( ans . size ( ) == 0 ) cout << " False " ; else { cout << " True ▁ STRNEWLINE " << ans ; } } int main ( ) { string S = " abca " ; commonSubstring ( S ) ; return 0 ; }
Length of Smallest Non Prime Subsequence in given numeric String | C ++ program for the above approach ; Function to find the smallest length of resultant subsequence ; Check for a subsequence of size 1 ; Check for a subsequence of size 2 ; If none of the above check is successful then subsequence must be of size 3 ; Never executed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumSubsequence ( string S ) { bool flag = false ; string dummy ; for ( int j = 0 ; j < S . length ( ) ; j ++ ) { if ( S [ j ] != '2' && S [ j ] != '3' && S [ j ] != '5' && S [ j ] != '7' ) { cout << 1 ; flag = true ; break ; } } if ( ! flag ) { for ( int j = 0 ; j < S . length ( ) - 1 ; j ++ ) { for ( int j1 = j + 1 ; j1 < S . length ( ) ; j1 ++ ) { dummy = S [ j ] + S [ j1 ] ; if ( dummy != "23" && dummy != "37" && dummy != "53" && dummy != "73" ) { cout << 2 ; } if ( flag = true ) break ; } if ( flag = true ) break ; } } if ( ! flag ) { if ( S . length ( ) >= 3 ) { cout << 3 ; } else { cout << -1 ; } } } int main ( ) { string S = "237" ; findMinimumSubsequence ( S ) ; return 0 ; }