codeparrot/xlcost-text-to-code huggingface dataset:
Longest substring with count of 1 s more than 0 s | CPP program to find largest substring having count of 1 s more than count count of 0 s . ; Function to find longest substring having count of 1 s more than count of 0 s . ; To store sum . ; To store first occurrence of each sum value . ; To store maximum length . ; To store current substring length . ; Add 1 if current character is 1 else subtract 1. ; If sum is positive , then maximum length substring is bin [ 0. . i ] ; If sum is negative , then maximum length substring is bin [ j + 1. . i ] , where sum of substring bin [ 0. . j ] is sum - 1. ; Make entry for this sum value in hash table if this value is not present . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLongestSub ( string bin ) { int n = bin . length ( ) , i ; int sum = 0 ; unordered_map < int , int > prevSum ; int maxlen = 0 ; int currlen ; for ( i = 0 ; i < n ; i ++ ) { if ( bin [ i ] == '1' ) sum ++ ; else sum -- ; if ( sum > 0 ) { maxlen = i + 1 ; } else if ( sum <= 0 ) { if ( prevSum . find ( sum - 1 ) != prevSum . end ( ) ) { currlen = i - prevSum [ sum - 1 ] ; maxlen = max ( maxlen , currlen ) ; } } if ( prevSum . find ( sum ) == prevSum . end ( ) ) prevSum [ sum ] = i ; } return maxlen ; } int main ( ) { string bin = "1010" ; cout << findLongestSub ( bin ) ; return 0 ; }
Average of ASCII values of characters of a given string | C ++ code to find average of ASCII characters ; Function to find average of ASCII value of chars ; loop to sum the ascii value of chars ; Returning average of chars ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int averageValue ( string s ) { int sum_char = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum_char += ( int ) s [ i ] ; } return sum_char / s . length ( ) ; } int main ( ) { string s = " GeeksforGeeks " ; cout << averageValue ( s ) ; return 0 ; }
Check if two same sub | C ++ program to Check if similar subsequences exist or not ; Function to check if similar subsequences occur in a string or not ; iterate and count the frequency ; freq [ s [ i ] - ' a ' ] ++ ; counting frequency of the letters ; check if frequency is more than once of any character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s , int l ) { int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < l ; i ++ ) { } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] >= 2 ) return true ; } return false ; } int main ( ) { string s = " geeksforgeeks " ; int l = s . length ( ) ; if ( check ( s , l ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Find the winner of a game where scores are given as a binary string | Cpp program for predicting winner ; function for winner prediction ; increase count ; check losing condition ; check winning condition ; check tie on n - 1 point ; increase count ; check for 2 point lead ; condition of lost ; condition of win ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void predictWinner ( string score , int n ) { int count [ 2 ] = { 0 } , i ; for ( i = 0 ; i < score . size ( ) ; i ++ ) { count [ score [ i ] - '0' ] ++ ; if ( count [ 0 ] == n && count [ 1 ] < n - 1 ) { cout << " GEEKS ▁ lost " ; return ; } if ( count [ 1 ] == n && count [ 0 ] < n - 1 ) { cout << " GEEKS ▁ won " ; return ; } if ( count [ 0 ] == n - 1 && count [ 1 ] == n - 1 ) { count [ 0 ] = 0 ; count [ 1 ] = 0 ; break ; } } for ( i ++ ; i < score . size ( ) ; i ++ ) { count [ score [ i ] - '0' ] ++ ; if ( abs ( count [ 0 ] - count [ 1 ] ) == 2 ) { if ( count [ 0 ] > count [ 1 ] ) cout << " GEEKS ▁ lost " ; else cout << " GEEKS ▁ won " ; return ; } } } int main ( ) { string score = "1001010101111011101111" ; int n = 15 ; predictWinner ( score , n ) ; return 0 ; }
Number of Counterclockwise shifts to make a string palindrome | C ++ program to find counter clockwise shifts to make string palindrome . ; Function to check if given string is palindrome or not . ; Function to find counter clockwise shifts to make string palindrome . ; Pointer to starting of current shifted string . ; Pointer to ending of current shifted string . ; Concatenate string with itself ; To store counterclockwise shifts ; Move left and right pointers one step at a time . ; Check if current shifted string is palindrome or not ; If string is not palindrome then increase count of number of shifts by 1. ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string str , int l , int r ) { while ( l < r ) { if ( str [ l ] != str [ r ] ) return false ; l ++ ; r -- ; } return true ; } int CyclicShifts ( string str ) { int n = str . length ( ) ; int left = 0 ; int right = n - 1 ; str = str + str ; int cnt = 0 ; while ( right < 2 * n - 1 ) { if ( isPalindrome ( str , left , right ) ) break ; cnt ++ ; left ++ ; right ++ ; } return cnt ; } int main ( ) { string str = " bccbbaab " ; cout << CyclicShifts ( str ) ; return 0 ; }
Find a string such that every character is lexicographically greater than its immediate next character | C ++ program to print a string in reverse alphabetical order upto given number ; Function that prints the required string ; Find modulus with 26 ; Print extra characters required ; Print the given reverse string countOfStr times ; Driver Code ; Initialize a string in reverse order
#include <bits/stdc++.h> NEW_LINE using namespace std ; string printString ( int n , string str ) { string str2 = " " ; int extraChar = n % 26 ; if ( extraChar >= 1 ) { for ( int i = 26 - ( extraChar + 1 ) ; i <= 25 ; i ++ ) str2 += str [ i ] ; } int countOfStr = n / 26 ; for ( int i = 1 ; i <= countOfStr ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) str2 += str [ j ] ; } return str2 ; } int main ( ) { int n = 30 ; string str = " zyxwvutsrqponmlkjihgfedcba " ; cout << printString ( n , str ) ; return 0 ; }
Longest Common Prefix Matching | Set | A C ++ Program to find the longest common prefix ; A Utility Function to find the common prefix between first and last strings ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; sorts the N set of strings ; prints the common prefix of the first and the last string of the set of strings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string commonPrefixUtil ( string str1 , string str2 ) { string result ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) break ; result . push_back ( str1 [ i ] ) ; } return ( result ) ; } void commonPrefix ( string arr [ ] , int n ) { sort ( arr , arr + n ) ; cout << commonPrefixUtil ( arr [ 0 ] , arr [ n - 1 ] ) ; } int main ( ) { string arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; commonPrefix ( arr , n ) ; return 0 ; }
Square of large number represented as String | C ++ program to multiply two numbers represented as strings . ; Multiplies str1 and str2 , and prints result . ; will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multiplication of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result at current position . ; Carry for next iteration ; Store result ; store carry in next cell ; To shift position to left after every multiplication of a digit in num1 . ; ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0' ; generate the result string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string multiply ( string num1 , string num2 ) { int n1 = num1 . size ( ) ; int n2 = num2 . size ( ) ; if ( n1 == 0 n2 == 0 ) return "0" ; vector < int > result ( n1 + n2 , 0 ) ; int i_n1 = 0 ; int i_n2 = 0 ; for ( int i = n1 - 1 ; i >= 0 ; i -- ) { int carry = 0 ; int n1 = num1 [ i ] - '0' ; i_n2 = 0 ; for ( int j = n2 - 1 ; j >= 0 ; j -- ) { int n2 = num2 [ j ] - '0' ; int sum = n1 * n2 + result [ i_n1 + i_n2 ] + carry ; carry = sum / 10 ; result [ i_n1 + i_n2 ] = sum % 10 ; i_n2 ++ ; } if ( carry > 0 ) result [ i_n1 + i_n2 ] += carry ; i_n1 ++ ; } int i = result . size ( ) - 1 ; while ( i >= 0 && result [ i ] == 0 ) i -- ; if ( i == -1 ) return "0" ; string s = " " ; while ( i >= 0 ) s += std :: to_string ( result [ i -- ] ) ; return s ; } int main ( ) { string str1 = "454545454545454545" ; cout << multiply ( str1 , str1 ) ; return 0 ; }
Perform n steps to convert every digit of a number in the format [ count ] [ digit ] | C ++ program to convert number to the format [ count ] [ digit ] at every step ; Function to perform every step ; perform N steps ; Traverse in the string ; for last digit ; recur for current string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countDigits ( string st , int n ) { if ( n > 0 ) { int cnt = 1 , i ; string st2 = " " ; for ( i = 1 ; i < st . length ( ) ; i ++ ) { if ( st [ i ] == st [ i - 1 ] ) cnt ++ ; else { st2 += ( '0' + cnt ) ; st2 += st [ i - 1 ] ; cnt = 1 ; } } st2 += ( '0' + cnt ) ; st2 += st [ i - 1 ] ; countDigits ( st2 , -- n ) ; } else cout << st ; } int main ( ) { string num = "123" ; int n = 3 ; countDigits ( num , n ) ; return 0 ; }
Sudo Placement | Special Subsequences | C ++ Program to find all special subsequences of the given type ; Function to generate the required subsequences ; If the index pointer has reached the end of input string ; Skip empty ( " ▁ " ) subsequence ; Exclude current character in output string ; Include current character in output string ; Remove the current included character and and include it in its uppercase form ; Function to print the required subsequences ; Output String to store every subsequence ; Set of strings that will store all special subsequences in lexicographical sorted order ; Sort the strings to print in sorted order ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateSubsequences ( string input , string output , int idx , vector < string > & ans ) { if ( input [ idx ] == ' \0' ) { if ( output . size ( ) ) ans . push_back ( output ) ; return ; } generateSubsequences ( input , output , idx + 1 , ans ) ; output += input [ idx ] ; generateSubsequences ( input , output , idx + 1 , ans ) ; output . pop_back ( ) ; char upper = input [ idx ] ; upper = toupper ( upper ) ; output += upper ; generateSubsequences ( input , output , idx + 1 , ans ) ; } void printSubsequences ( string S ) { string output ; vector < string > ans ; generateSubsequences ( S , output , 0 , ans ) ; sort ( ans . begin ( ) , ans . end ( ) ) ; for ( auto str : ans ) cout << str << " ▁ " ; } int main ( ) { string S = " ab " ; printSubsequences ( S ) ; return 0 ; }
Check if the given string of words can be formed from words present in the dictionary | C ++ program to check if a sentence can be formed from a given set of words . ; Function to check if the word is in the dictionary or not ; map to store all words in dictionary with their count ; adding all words in map ; search in map for all words in the sentence ; all words of sentence are present ; Driver Code ; Calling function to check if words are present in the dictionary or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool match_words ( string dictionary [ ] , string sentence [ ] , int n , int m ) { unordered_map < string , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ dictionary [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { if ( mp [ sentence [ i ] ] ) mp [ sentence [ i ] ] -= 1 ; else return false ; } return true ; } int main ( ) { string dictionary [ ] = { " find " , " a " , " geeks " , " all " , " for " , " on " , " geeks " , " answers " , " inter " } ; int n = sizeof ( dictionary ) / sizeof ( dictionary [ 0 ] ) ; string sentence [ ] = { " find " , " all " , " answers " , " on " , " geeks " , " for " , " geeks " } ; int m = sizeof ( sentence ) / sizeof ( sentence [ 0 ] ) ; if ( match_words ( dictionary , sentence , n , m ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Print all 3 digit repeating numbers in a very large number | CPP program to print 3 digit repeating numbers ; function to print 3 digit repeating numbers ; Hashmap to store the frequency of a 3 digit number ; first three digit number ; if key already exists increase value by 1 ; Output the three digit numbers with frequency > 1 ; Driver Code ; Input string ; Calling Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNum ( string s ) { int i = 0 , j = 0 , val = 0 ; map < int , int > mp ; val = ( s [ 0 ] - '0' ) * 100 + ( s [ 1 ] - '0' ) * 10 + ( s [ 2 ] - '0' ) ; mp [ val ] = 1 ; for ( i = 3 ; i < s . length ( ) ; i ++ ) { val = ( val % 100 ) * 10 + s [ i ] - '0' ; if ( mp . find ( val ) != mp . end ( ) ) { mp [ val ] = mp [ val ] + 1 ; } else { mp [ val ] = 1 ; } } for ( auto m : mp ) { int key = m . first ; int value = m . second ; if ( value > 1 ) cout << key << " ▁ - ▁ " << value << " ▁ times " << endl ; } } int main ( ) { string input = "123412345123456" ; printNum ( input ) ; }
Count occurrences of a substring recursively | Recursive C ++ program for counting number of substrings ; Recursive function to count the number of occurrences of " hi " in str . ; Base Case ; Recursive Case Checking if the first substring matches ; Otherwise , return the count from the remaining index ; Driver function
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int countSubstrig ( string str1 , string str2 ) { int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; if ( n1 == 0 n1 < n2 ) return 0 ; if ( str1 . substr ( 0 , n2 ) . compare ( str2 ) == 0 ) return countSubstrig ( str1 . substr ( n2 - 1 ) , str2 ) + 1 ; return countSubstrig ( str1 . substr ( n2 - 1 ) , str2 ) ; } int main ( ) { string str1 = " geeksforgeeks " , str2 = " geeks " ; cout << countSubstrig ( str1 , str2 ) << endl ; str1 = " hikakashi " , str2 = " hi " ; cout << countSubstrig ( str1 , str2 ) << endl ; return 0 ; }
Check if a binary string contains all permutations of length k | C ++ Program to Check If a String Contains All Binary Codes of Size K ; Unordered map of type string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long NEW_LINE bool hasAllcodes ( string s , int k ) { unordered_set < string > us ; for ( int i = 0 ; i + k <= s . size ( ) ; i ++ ) { us . insert ( s . substr ( i , k ) ) ; } return us . size ( ) == 1 << k ; } signed main ( ) { string s = "00110110" ; int k = 2 ; if ( hasAllcodes ) { cout << " YES STRNEWLINE " ; } else { cout << " NO STRNEWLINE " ; } }
Add n binary strings | C ++ program to add n binary strings ; This function adds two binary strings and return result as a third string ; Initialize result ; Initialize digit sum ; Traverse both strings starting from last characters ; Compute sum of last digits and carry ; If current digit sum is 1 or 3 , add 1 to result ; Compute carry ; Move to next digits ; function to add n binary strings ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string addBinaryUtil ( string a , string b ) { string result = " " ; int s = 0 ; int i = a . size ( ) - 1 , j = b . size ( ) - 1 ; while ( i >= 0 j >= 0 s == 1 ) { s += ( ( i >= 0 ) ? a [ i ] - '0' : 0 ) ; s += ( ( j >= 0 ) ? b [ j ] - '0' : 0 ) ; result = char ( s % 2 + '0' ) + result ; s /= 2 ; i -- ; j -- ; } return result ; } string addBinary ( string arr [ ] , int n ) { string result = " " ; for ( int i = 0 ; i < n ; i ++ ) result = addBinaryUtil ( result , arr [ i ] ) ; return result ; } int main ( ) { string arr [ ] = { "1" , "10" , "11" } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << addBinary ( arr , n ) << endl ; return 0 ; }
Reverse String according to the number of words | C ++ program to reverse string according to the number of words ; Reverse the letters of the word ; Temporary variable to store character ; Swapping the first and last character ; This function forms the required string ; Checking the number of words present in string to reverse ; Reverse the letter of the words ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverse ( char str [ ] , int start , int end ) { char temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } } void reverseletter ( char str [ ] , int start , int end ) { int wstart , wend ; for ( wstart = wend = start ; wend < end ; wend ++ ) { if ( str [ wend ] == ' ▁ ' ) continue ; while ( str [ wend ] != ' ▁ ' && wend <= end ) wend ++ ; wend -- ; reverse ( str , wstart , wend ) ; } } int main ( ) { char str [ 1000 ] = " Ashish ▁ Yadav ▁ Abhishek ▁ Rajput ▁ Sunil ▁ Pundir " ; reverseletter ( str , 0 , strlen ( str ) - 1 ) ; cout << str ; return 0 ; }
Minimum bit changes in Binary Circular array to reach a index | CPP program to find direction with minimum flips ; finding which path have minimum flip bit and the minimum flip bits ; concatenating given string to itself , to make it circular ; check x is greater than y . marking if output need to be opposite . ; iterate Clockwise ; if current bit is not equal to next index bit . ; iterate Anti - Clockwise ; if current bit is not equal to next index bit . ; Finding whether Clockwise or Anti - clockwise path take minimum flip . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumFlip ( string s , int x , int y ) { s = s + s ; bool isOpposite = false ; if ( x > y ) { swap ( x , y ) ; isOpposite = true ; } int valClockwise = 0 ; char cur = s [ x ] ; for ( int i = x ; i <= y ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valClockwise ++ ; } } int valAnticlockwise = 0 ; cur = s [ y ] ; x += s . length ( ) ; for ( int i = y ; i <= x ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valAnticlockwise ++ ; } } if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) cout << " Clockwise ▁ " << valClockwise << endl ; else cout << " Anti - clockwise ▁ " << valAnticlockwise << endl ; } else { if ( ! isOpposite ) cout << " Anti - clockwise ▁ " << valAnticlockwise << endl ; else cout << " Clockwise ▁ " << valClockwise << endl ; } } int main ( ) { int x = 0 , y = 8 ; string s = "000110" ; minimumFlip ( s , x , y ) ; return 0 ; }
Prefixes with more a than b | CPP code to count the prefixes with more a than b ; Function to count prefixes ; calculating for string S ; count == 0 or when N == 1 ; when all characters are a or a - b == 0 ; checking for saturation of string after repetitive addition ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prefix ( string k , int n ) { int a = 0 , b = 0 , count = 0 ; int i = 0 ; int len = k . size ( ) ; for ( i = 0 ; i < len ; i ++ ) { if ( k [ i ] == ' a ' ) a ++ ; if ( k [ i ] == ' b ' ) b ++ ; if ( a > b ) { count ++ ; } } if ( count == 0 n == 1 ) { cout << count << endl ; return 0 ; } if ( count == len a - b == 0 ) { cout << count * n << endl ; return 0 ; } int n2 = n - 1 , count2 = 0 ; while ( n2 != 0 ) { for ( i = 0 ; i < len ; i ++ ) { if ( k [ i ] == ' a ' ) a ++ ; if ( k [ i ] == ' b ' ) b ++ ; if ( a > b ) { count2 ++ ; } } count += count2 ; n2 -- ; if ( count2 == 0 ) break ; if ( count2 == len ) { count += ( n2 * count2 ) ; break ; } count2 = 0 ; } return count ; } int main ( ) { string S = " aba " ; int N = 2 ; cout << prefix ( S , N ) << endl ; S = " baa " ; N = 3 ; cout << prefix ( S , N ) << endl ; return 0 ; }
Pairs whose concatenation contain all digits | C ++ Program to find number of pairs whose concatenation contains all digits from 0 to 9. ; Function to return number of pairs whose concatenation contain all digits from 0 to 9 ; making the mask for each of the number . ; for each of the possible pair which can make OR value equal to 1023 ; finding the count of pair from the given numbers . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  20 NEW_LINE int countPair ( char str [ N ] [ N ] , int n ) { int cnt [ 1 << 10 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { int mask = 0 ; for ( int j = 0 ; str [ i ] [ j ] != ' \0' ; ++ j ) mask |= ( 1 << ( str [ i ] [ j ] - '0' ) ) ; cnt [ mask ] ++ ; } int ans = 0 ; for ( int m1 = 0 ; m1 <= 1023 ; m1 ++ ) for ( int m2 = 0 ; m2 <= 1023 ; m2 ++ ) if ( ( m1 m2 ) == 1023 ) { ans += ( ( m1 == m2 ) ? ( cnt [ m1 ] * ( cnt [ m1 ] - 1 ) ) : ( cnt [ m1 ] * cnt [ m2 ] ) ) ; } return ans / 2 ; } int main ( ) { int n = 5 ; char str [ ] [ N ] = { "129300455" , "5559948277" , "012334556" , "56789" , "123456879" } ; cout << countPair ( str , n ) << endl ; return 0 ; }
Count binary strings with twice zeros in first half | CPP for finding number of binary strings number of '0' in first half is double the number of '0' in second half of string ; pre define some constant ; global values for pre computation ; function to print number of required string ; calculate answer using proposed algorithm ; Driver code
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE #define max  1001 NEW_LINE using namespace std ; long long int nCr [ 1003 ] [ 1003 ] ; void preComputeCoeff ( ) { for ( int i = 0 ; i < max ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = ( nCr [ i - 1 ] [ j - 1 ] + nCr [ i - 1 ] [ j ] ) % mod ; } } } long long int computeStringCount ( int N ) { int n = N / 2 ; long long int ans = 0 ; for ( int i = 2 ; i <= n ; i += 2 ) ans = ( ans + ( ( nCr [ n ] [ i ] * nCr [ n ] [ i / 2 ] ) % mod ) ) % mod ; return ans ; } int main ( ) { preComputeCoeff ( ) ; int N = 3 ; cout << computeStringCount ( N ) << endl ; return 0 ; }
Count of ' GFG ' Subsequences in the given string | CPP Program to find the " GFG " subsequence in the given string ; Print the count of " GFG " subsequence in the string ; Traversing the given string ; If the character is ' G ' , increment the count of ' G ' , increase the result and update the array . ; If the character is ' F ' , increment the count of ' F ' and update the array . ; Ignore other character . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE void countSubsequence ( char s [ ] , int n ) { int cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( int i = 0 ; i < n ; i ++ ) { switch ( s [ i ] ) { case ' G ' : cntG ++ ; result += C ; break ; case ' F ' : cntF ++ ; C += cntG ; break ; default : continue ; } } cout << result << endl ; } int main ( ) { char s [ ] = " GFGFG " ; int n = strlen ( s ) ; countSubsequence ( s , n ) ; return 0 ; }
Lexicographical Maximum substring of string | CPP program to find the lexicographically maximum substring . ; loop to find the max leicographic substring in the substring array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string LexicographicalMaxString ( string str ) { string mx = " " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) mx = max ( mx , str . substr ( i ) ) ; return mx ; } int main ( ) { string str = " ababaa " ; cout << LexicographicalMaxString ( str ) ; return 0 ; }
Lexicographical Maximum substring of string | C ++ program to find the lexicographically maximum substring . ; We store all the indexes of maximum characters we have in the string ; We form a substring from that maximum character index till end and check if its greater that maxstring ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string LexicographicalMaxString ( string str ) { char maxchar = ' a ' ; vector < int > index ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= maxchar ) { maxchar = str [ i ] ; index . push_back ( i ) ; } } string maxstring = " " ; for ( int i = 0 ; i < index . size ( ) ; i ++ ) { if ( str . substr ( index [ i ] , str . length ( ) ) > maxstring ) { maxstring = str . substr ( index [ i ] , str . length ( ) ) ; } } return maxstring ; } int main ( ) { string str = " acbacbc " ; cout << LexicographicalMaxString ( str ) ; return 0 ; }