codeparrot/xlcost-text-to-code huggingface dataset:
Reduce the string to minimum length with the given operation | C ++ implementation of the approach ; Function to return the minimum possible length str can be reduced to with the given operation ; Stack to store the characters of the given string ; For every character of the string ; If the stack is empty then push the current character in the stack ; Get the top character ; If the top element is not equal to the current element and it only differs in the case ; Pop the top element from stack ; Else push the current element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( string str , int len ) { stack < char > s ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . empty ( ) ) { s . push ( str [ i ] ) ; } else { char c = s . top ( ) ; if ( c != str [ i ] && toupper ( c ) == toupper ( str [ i ] ) ) { s . pop ( ) ; } else { s . push ( str [ i ] ) ; } } } return s . size ( ) ; } int main ( ) { string str = " ASbBsd " ; int len = str . length ( ) ; cout << minLength ( str , len ) ; return 0 ; }
Check whether two strings can be made equal by copying their characters with the adjacent ones | C ++ implementation of the approach ; Function that returns true if both the strings can be made equal with the given operation ; Lengths of both the strings have to be equal ; To store the frequency of the characters of str1 ; For every character of str2 ; If current character of str2 also appears in str1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  26 NEW_LINE bool canBeMadeEqual ( string str1 , string str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; if ( len1 == len2 ) { int freq [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) { freq [ str1 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 [ i ] - ' a ' ] > 0 ) return true ; } } return false ; } int main ( ) { string str1 = " abc " , str2 = " defa " ; if ( canBeMadeEqual ( str1 , str2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum characters that are to be inserted such that no three consecutive characters are same | C ++ implementation of the approach ; Function to return the count of characters that are to be inserted in str such that no three consecutive characters are same ; To store the count of operations required ; A character needs to be inserted after str [ i + 1 ] ; Current three consecutive characters are not same ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( string str , int n ) { int cnt = 0 ; int i = 0 ; while ( i < n - 2 ) { if ( str [ i ] == str [ i + 1 ] && str [ i ] == str [ i + 2 ] ) { cnt ++ ; i = i + 2 ; } else i ++ ; } return cnt ; } int main ( ) { string str = " aabbbcc " ; int n = str . length ( ) ; cout << getCount ( str , n ) ; return 0 ; }
Find the number of strings formed using distinct characters of a given string | C ++ implementation of the approach ; Function to return the factorial of n ; Function to return the count of all possible strings that can be formed with the characters of the given string without repeating characters ; To store the distinct characters of the string str ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact *= i ; return fact ; } int countStrings ( string str , int n ) { set < char > distinct_char ; for ( int i = 0 ; i < n ; i ++ ) { distinct_char . insert ( str [ i ] ) ; } return fact ( distinct_char . size ( ) ) ; } int main ( ) { string str = " geeksforgeeks " ; int n = str . length ( ) ; cout << countStrings ( str , n ) ; return 0 ; }
Find the character made by adding all the characters of the given string | C ++ implementation of the approach ; Function to return the required character ; To store the sum of the characters of the given string ; Add the current character to the sum ; Return the required character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char getChar ( string str ) { int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { sum += ( str [ i ] - ' a ' + 1 ) ; } if ( sum % 26 == 0 ) return ' z ' ; else { sum = sum % 26 ; return ( char ) ( ' a ' + sum - 1 ) ; } } int main ( ) { string str = " gfg " ; cout << getChar ( str ) ; return 0 ; }
Reverse the given string in the range [ L , R ] | C ++ implementation of the approach ; Function to return the string after reversing characters in the range [ L , R ] ; Invalid range ; While there are characters to swap ; Swap ( str [ l ] , str [ r ] ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string reverse ( string str , int len , int l , int r ) { if ( l < 0 r > = len l > r ) return str ; while ( l < r ) { char c = str [ l ] ; str [ l ] = str [ r ] ; str [ r ] = c ; l ++ ; r -- ; } return str ; } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; int l = 5 , r = 7 ; cout << reverse ( str , len , l , r ) ; return 0 ; }
Program to Encrypt a String using ! and @ | C ++ program to Encrypt the String using ! and @ ; Function to encrypt the string ; evenPos is for storing encrypting char at evenPosition oddPos is for storing encrypting char at oddPosition ; Get the number of times the character is to be repeated ; if i is odd , print ' ! ' else print ' @ ' ; Driver code ; Encrypt the String
#include <bits/stdc++.h> NEW_LINE using namespace std ; void encrypt ( char input [ 100 ] ) { char evenPos = ' @ ' , oddPos = ' ! ' ; int repeat , ascii ; for ( int i = 0 ; i <= strlen ( input ) ; i ++ ) { ascii = input [ i ] ; repeat = ascii >= 97 ? ascii - 96 : ascii - 64 ; for ( int j = 0 ; j < repeat ; j ++ ) { if ( i % 2 == 0 ) cout << oddPos ; else cout << evenPos ; } } } int main ( ) { char input [ 100 ] = { ' A ' , ' b ' , ' C ' , ' d ' } ; encrypt ( input ) ; return 0 ; }
Check if expression contains redundant bracket or not | Set 2 | C ++ program to check for / redundant braces in the string ; Function to check for redundant braces ; count of no of signs ; Driver function
#include <iostream> NEW_LINE using namespace std ; bool IsRedundantBraces ( string A ) { int a = 0 , b = 0 ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { if ( A [ i ] == ' ( ' && A [ i + 2 ] == ' ) ' ) return 1 ; if ( A [ i ] == ' * ' A [ i ] == ' + ' A [ i ] == ' - ' A [ i ] == ' / ' ) a ++ ; if ( A [ i ] == ' ( ' ) b ++ ; } if ( b > a ) return 1 ; return 0 ; } int main ( ) { string A = " ( ( ( a + b ) ▁ + ▁ c ) ▁ + ▁ d ) " ; if ( IsRedundantBraces ( A ) ) { cout << " YES STRNEWLINE " ; } else { cout << " NO " ; } }
Convert an unbalanced bracket sequence to a balanced sequence | C ++ implementation of the approach ; Function to return balancedBrackets string ; Initializing dep to 0 ; Stores maximum negative depth ; if dep is less than minDep ; if minDep is less than 0 then there is need to add ' ( ' at the front ; Reinitializing to check the updated string ; if dep is not 0 then there is need to add ' ) ' at the back ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string balancedBrackets ( string str ) { int dep = 0 ; int minDep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' ( ' ) dep ++ ; else dep -- ; if ( minDep > dep ) minDep = dep ; } if ( minDep < 0 ) { for ( int i = 0 ; i < abs ( minDep ) ; i ++ ) str = ' ( ' + str ; } dep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' ( ' ) dep ++ ; else dep -- ; } if ( dep != 0 ) { for ( int i = 0 ; i < dep ; i ++ ) str = str + ' ) ' ; } return str ; } int main ( ) { string str = " ) ) ) ( ) " ; cout << balancedBrackets ( str ) ; }
Minimum operations required to convert a binary string to all 0 s or all 1 s | C ++ implementation of the approach ; Function to return the count of minimum operations required ; Increment count when consecutive characters are different ; Answer is rounding off the ( count / 2 ) to lower ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string str , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] != str [ i + 1 ] ) count ++ ; } return ( count + 1 ) / 2 ; } int main ( ) { string str = "000111" ; int n = str . length ( ) ; cout << minOperations ( str , n ) ; return 0 ; }
Append a digit in the end to make the number equal to the length of the remaining string | C ++ implementation of the approach ; Function to return the required digit ; To store the position of the first numeric digit in the string ; To store the length of the string without the numeric digits in the end ; pw stores the current power of 10 and num is to store the number which is appended in the end ; If current character is a numeric digit ; Get the current digit ; Build the number ; If number exceeds the length ; Next power of 10 ; Append 0 in the end ; Required number that must be added ; If number is not a single digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_digit ( string s , int n ) { int first_digit = -1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] < '0' s [ i ] > '9' ) { first_digit = i ; break ; } } first_digit ++ ; int s_len = first_digit ; int num = 0 , pw = 1 ; int i = n - 1 ; while ( i >= 0 ) { if ( s [ i ] >= '0' && s [ i ] <= '9' ) { int digit = s [ i ] - '0' ; num = num + ( pw * digit ) ; if ( num >= s_len ) return -1 ; pw = pw * 10 ; } i -- ; } num = num * 10 ; int req = s_len - num ; if ( req > 9 req < 0 ) return -1 ; return req ; } int main ( ) { string s = " abcd0" ; int n = s . length ( ) ; cout << find_digit ( s , n ) ; return 0 ; }
Check whether str1 can be converted to str2 with the given operations | C ++ implementation of the approach ; Function that returns true if str1 can be converted to str2 with the given operations ; Traverse from left to right ; If the two characters do not match ; If possible to combine ; If not possible to combine ; If the two characters match ; If possible to convert one string to another ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canConvert ( string str1 , string str2 ) { int i = 0 , j = 0 ; while ( i < str1 . size ( ) && j < str2 . size ( ) ) { if ( str1 [ i ] != str2 [ j ] ) { if ( str1 [ i ] == '0' && str2 [ j ] == '1' && i + 1 < str1 . size ( ) && str1 [ i + 1 ] == '0' ) { i += 2 ; j ++ ; } else { return false ; } } else { i ++ ; j ++ ; } } if ( i == str1 . size ( ) && j == str2 . size ( ) ) return true ; return false ; } int main ( ) { string str1 = "00100" , str2 = "111" ; if ( canConvert ( str1 , str2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Reverse the Words of a String using Stack | C ++ implementation of the above approach ; function to reverse the words of the given string without using strtok ( ) . ; create an empty string stack ; create an empty temporary string ; traversing the entire string ; push the temporary variable into the stack ; assigning temporary variable as empty ; for the last word of the string ; Get the words in reverse order ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverse ( string s ) { stack < string > stc ; string temp = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == ' ▁ ' ) { stc . push ( temp ) ; temp = " " ; } else { temp = temp + s [ i ] ; } } stc . push ( temp ) ; while ( ! stc . empty ( ) ) { temp = stc . top ( ) ; cout << temp << " ▁ " ; stc . pop ( ) ; } cout << endl ; } int main ( ) { string s = " I ▁ Love ▁ To ▁ Code " ; reverse ( s ) ; return 0 ; }
Print an N x M matrix such that each row and column has all the vowels in it | C ++ implementation of the approach ; Function to print the required matrix ; Impossible to generate the required matrix ; Store all the vowels ; Print the matrix ; Print vowels for every index ; Shift the vowels by one ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMatrix ( int n , int m ) { if ( n < 5 m < 5 ) { cout << -1 ; return ; } string s = " aeiou " ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { cout << s [ j % 5 ] << " ▁ " ; } cout << endl ; char c = s [ 0 ] ; for ( int i = 0 ; i < 4 ; i ++ ) { s [ i ] = s [ i + 1 ] ; } s [ 4 ] = c ; } } int main ( ) { int n = 5 , m = 5 ; printMatrix ( n , m ) ; return 0 ; }
Check if a given string is made up of two alternating characters | C ++ implementation of the approach ; Function that returns true if the string is made up of two alternating characters ; Check if ith character matches with the character at index ( i + 2 ) ; If string consists of a single character repeating itself ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isTwoAlter ( string s ) { for ( int i = 0 ; i < s . length ( ) - 2 ; i ++ ) { if ( s [ i ] != s [ i + 2 ] ) { return false ; } } if ( s [ 0 ] == s [ 1 ] ) return false ; return true ; } int main ( ) { string str = " ABAB " ; if ( isTwoAlter ( str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number of character corrections in the given strings to make them equal | C ++ implementation of the approach ; Function to return the count of operations required ; To store the count of operations ; No operation required ; One operation is required when any two characters are equal ; Two operations are required when none of the characters are equal ; Return the minimum count of operations required ; Driver code
#include <iostream> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; const int minOperations ( int n , string a , string b , string c ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char x = a [ i ] ; char y = b [ i ] ; char z = c [ i ] ; if ( x == y && y == z ) ; else if ( x == y y == z x == z ) { ans ++ ; } else { ans += 2 ; } } return ans ; } int main ( ) { string a = " place " ; string b = " abcde " ; string c = " plybe " ; int n = a . size ( ) ; cout << minOperations ( n , a , b , c ) ; return 0 ; }
Check if string can be made lexicographically smaller by reversing any substring | C ++ implementation of the approach ; Function that returns true if s can be made lexicographically smaller by reversing a sub - string in s ; Traverse in the string ; Check if s [ i + 1 ] < s [ i ] ; Not possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s ) { int n = s . size ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) return true ; } return false ; } int main ( ) { string s = " geeksforgeeks " ; if ( check ( s ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of sub | C ++ implementation of the approach ; Function to return the count of required sub - strings ; Number of sub - strings from position of current x to the end of str ; To store the number of characters before x ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubStr ( string str , int n , char x ) { int res = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == x ) { res += ( ( count + 1 ) * ( n - i ) ) ; count = 0 ; } else count ++ ; } return res ; } int main ( ) { string str = " abcabc " ; int n = str . length ( ) ; char x = ' c ' ; cout << countSubStr ( str , n , x ) ; return 0 ; }
Count of sub | C ++ implementation of the approach ; Function to return the count of possible sub - strings of length n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubStr ( string str , int n ) { int len = str . length ( ) ; return ( len - n + 1 ) ; } int main ( ) { string str = " geeksforgeeks " ; int n = 5 ; cout << countSubStr ( str , n ) ; return 0 ; }
Count of sub | C ++ implementation of the approach ; Function to return the number of sub - strings that do not contain the given character c ; Length of the string ; Traverse in the string ; If current character is different from the given character ; Update the number of sub - strings ; Reset count to 0 ; For the characters appearing after the last occurrence of c ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstrings ( string s , char c ) { int n = s . length ( ) ; int cnt = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] != c ) cnt ++ ; else { sum += ( cnt * ( cnt + 1 ) ) / 2 ; cnt = 0 ; } } sum += ( cnt * ( cnt + 1 ) ) / 2 ; return sum ; } int main ( ) { string s = " baa " ; char c = ' b ' ; cout << countSubstrings ( s , c ) ; return 0 ; }
Minimize ASCII values sum after removing all occurrences of one character | C ++ implementation of the approach ; Function to return the minimized sum ; To store the occurrences of each character of the string ; Update the occurrence ; Calculate the sum ; Get the character which is contributing the maximum value to the sum ; Count of occurrence of the character multiplied by its ASCII value ; Return the minimized sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinimizedSum ( string str , int len ) { int i , maxVal = INT_MIN , sum = 0 ; int occurrences [ 26 ] = { 0 } ; for ( i = 0 ; i < len ; i ++ ) { occurrences [ str [ i ] - ' a ' ] ++ ; sum += ( int ) str [ i ] ; } for ( i = 0 ; i < 26 ; i ++ ) maxVal = max ( maxVal , occurrences [ i ] * ( i + ' a ' ) ) ; return ( sum - maxVal ) ; } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; cout << getMinimizedSum ( str , len ) ; return 0 ; }
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | C ++ implementation of the approach ; Function that returns true if s is palindrome ; Function to return the required index ; Copy the ith character in S ; Copy all the character of string s2 in Temp ; Check whether the string is palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string s ) { int i = 0 ; int j = s . length ( ) - 1 ; while ( i < j ) { if ( s [ i ] != s [ j ] ) return false ; i ++ ; j -- ; } return true ; } int getIndex ( string S1 , string S2 , int n ) { string S = " " ; for ( int i = 0 ; i < n ; i ++ ) { S = S + S1 [ i ] ; string Temp = " " ; for ( int j = i + 1 ; j < n ; j ++ ) Temp += S2 [ j ] ; if ( isPalindrome ( S + Temp ) ) { return i ; } } return -1 ; } int main ( ) { string S1 = " abcdf " , S2 = " sfgba " ; int n = S1 . length ( ) ; cout << getIndex ( S1 , S2 , n ) ; return 0 ; }
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | C ++ program to implement the above approach ; Function that returns true if the sub - string starting from index i and ending at index j is a palindrome ; Function to get the required index ; Start comparing the two strings from both ends . ; Break from the loop at first mismatch ; If it is possible to concatenate the strings to form palindrome , return index ; If remaining part for s2 is palindrome ; If remaining part for s1 is palindrome ; If not possible , return - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string s , int i , int j ) { while ( i < j ) { if ( s [ i ] != s [ j ] ) return false ; i ++ ; j -- ; } return true ; } int getIndex ( string s1 , string s2 , int len ) { int i = 0 , j = len - 1 ; while ( i < j ) { if ( s1 [ i ] != s2 [ j ] ) { break ; } i ++ ; j -- ; } if ( i == j ) { return i - 1 ; } else if ( isPalindrome ( s2 , i , j ) ) return i - 1 ; else if ( isPalindrome ( s1 , i , j ) ) return j ; return -1 ; } int main ( ) { string s1 = " abcdf " , s2 = " sfgba " ; int len = s1 . length ( ) ; cout << getIndex ( s1 , s2 , len ) ; return 0 ; }
Acronym words | C ++ implementation of the approach ; Function to return the number of strings that can be an acronym for other strings ; Frequency array to store the frequency of the first character of every string in the array ; To store the count of required strings ; Current word ; Frequency array to store the frequency of each of the character of the current string ; Check if the frequency of every character in the current string is <= its value in freq [ ] ; First character of the current string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_acronym ( int n , string arr [ ] ) { int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] [ 0 ] - ' a ' ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { string st = arr [ i ] ; int num [ 26 ] = { 0 } ; for ( int j = 0 ; j < st . length ( ) ; j ++ ) num [ st [ j ] - ' a ' ] ++ ; bool flag = true ; for ( int j = 1 ; j < 26 ; j ++ ) { if ( num [ j ] > freq [ j ] ) { flag = false ; break ; } } int x = st [ 0 ] - ' a ' ; if ( freq [ x ] - 1 < num [ x ] ) flag = false ; if ( flag ) cnt ++ ; } return cnt ; } int main ( ) { string arr [ ] = { " abc " , " bcad " , " cabd " , " cba " , " dzzz " } ; int n = 5 ; cout << count_acronym ( n , arr ) ; }
Sub | C ++ implementation of the approach ; Function that returns true if every lowercase character appears atmost once ; every character frequency must be not greater than one ; Function that returns the modified good string if possible ; If the length of the string is less than n ; Sub - strings of length 26 ; To store frequency of each character ; Get the frequency of each character in the current sub - string ; Check if we can get sub - string containing all the 26 characters ; Find which character is missing ; Fill with missing characters ; Find the next missing character ; Return the modified good string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool valid ( int cnt [ ] ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( cnt [ i ] >= 2 ) return false ; } return true ; } string getGoodString ( string s , int n ) { if ( n < 26 ) return " - 1" ; for ( int i = 25 ; i < n ; i ++ ) { int cnt [ 26 ] = { 0 } ; for ( int j = i ; j >= i - 25 ; j -- ) { cnt [ s [ j ] - ' a ' ] ++ ; } if ( valid ( cnt ) ) { int cur = 0 ; while ( cnt [ cur ] > 0 ) cur ++ ; for ( int j = i - 25 ; j <= i ; j ++ ) { if ( s [ j ] == ' ? ' ) { s [ j ] = cur + ' a ' ; cur ++ ; while ( cnt [ cur ] > 0 ) cur ++ ; } } return s ; } } return " - 1" ; } int main ( ) { string s = " abcdefghijkl ? nopqrstuvwxy ? " ; int n = s . length ( ) ; cout << getGoodString ( s , n ) ; return 0 ; }
Modify the string by swapping continuous vowels or consonants | C ++ implementation of the above approach ; Function to check if a character is a vowel ; Function to swap two consecutively repeated vowels or consonants ; Traverse through the length of the string ; Check if the two consecutive characters are vowels or consonants ; swap the two characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { c = tolower ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } string swapRepeated ( string str ) { for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) { if ( ( isVowel ( str [ i ] ) && isVowel ( str [ i + 1 ] ) ) || ( ! isVowel ( str [ i ] ) && ! isVowel ( str [ i + 1 ] ) ) ) swap ( str [ i ] , str [ i + 1 ] ) ; } return str ; } int main ( ) { string str = " geeksforgeeks " ; cout << swapRepeated ( str ) ; return 0 ; }
Find the lexicographically largest palindromic Subsequence of a String | CPP program to find the largest palindromic subsequence ; Function to find the largest palindromic subsequence ; Find the largest character ; Append all occurrences of largest character to the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestPalinSub ( string s ) { string res ; char mx = s [ 0 ] ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) mx = max ( mx , s [ i ] ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s [ i ] == mx ) res += s [ i ] ; return res ; } int main ( ) { string s = " geeksforgeeks " ; cout << largestPalinSub ( s ) ; }