codeparrot/xlcost-text-to-code huggingface dataset:
Print longest palindrome word in a sentence | C ++ program to print longest palindrome word in a sentence and its length ; Function to check if a word is palindrome ; making the check case case insensitive word = word . toLowerCase ( ) ; ; loop to check palindrome ; Function to find longest palindrome word ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE #include <string> NEW_LINE using namespace std ; bool checkPalin ( string word ) { int n = word . length ( ) ; transform ( word . begin ( ) , word . end ( ) , word . begin ( ) , :: tolower ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word [ i ] != word [ n - 1 ] ) return false ; return true ; } string longestPalin ( string str ) { str = str + " ▁ " ; string longestword = " " , word = " " ; int length , length1 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str [ i ] ; if ( ch != ' ▁ ' ) word = word + ch ; else { length = word . length ( ) ; if ( checkPalin ( word ) && length > length1 ) { length1 = length ; longestword = word ; } word = " " ; } } return longestword ; } int main ( ) { string s = " My ▁ name ▁ is ▁ ava ▁ and ▁ i ▁ love " " ▁ Geeksforgeeks " ; if ( longestPalin ( s ) == " " ) cout << " No ▁ Palindrome " << " ▁ Word " ; else cout << longestPalin ( s ) ; return 0 ; }
Number of common base strings for two strings | CPP program to count common base strings of s1 and s2 . ; function for finding common divisor . ; Checking if ' base ' is base string of ' s1' ; Checking if ' base ' is base string of ' s2' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCommonBase ( string base , string s1 , string s2 ) { for ( int j = 0 ; j < s1 . length ( ) ; ++ j ) if ( base [ j % base . length ( ) ] != s1 [ j ] ) return false ; for ( int j = 0 ; j < s2 . length ( ) ; ++ j ) if ( base [ j % base . length ( ) ] != s2 [ j ] ) return false ; return true ; } int countCommonBases ( string s1 , string s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; int count = 0 ; for ( int i = 1 ; i <= min ( n1 , n2 ) ; i ++ ) { string base = s1 . substr ( 0 , i ) ; if ( isCommonBase ( base , s1 , s2 ) ) count ++ ; } return count ; } int main ( ) { string s1 = " pqrspqrs " ; string s2 = " pqrspqrspqrspqrs " ; cout << countCommonBases ( s1 , s2 ) << endl ; return 0 ; }
Removing elements between the two zeros | C ++ program to delete elements between zeros ; Function to find the string after operation ; Travesing through string ; Checking for character Between two zeros ; deleting the character At specific position ; updating the length of the string ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findstring ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( s . at ( i - 1 ) == '0' && s . at ( i + 1 ) == '0' ) ) { s . erase ( i , 1 ) ; i -- ; if ( i > 0 && s . at ( i - 1 ) == '0' ) i -- ; n = s . length ( ) ; } } return s ; } int main ( ) { cout << findstring ( "100100" ) ; return 0 ; }
Perfect Square String | C ++ program to find if string is a perfect square or not . ; calculating the length of the string ; calculating the ASCII value of the string ; Find floating point value of square root of x . ; If square root is an integer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquareString ( string str ) { int sum = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) sum += ( int ) str [ i ] ; long double squareRoot = sqrt ( sum ) ; return ( ( squareRoot - floor ( squareRoot ) ) == 0 ) ; } int main ( ) { string str = " d " ; if ( isPerfectSquareString ( str ) ) cout << " Yes " ; else cout << " No " ; }
Count substrings with each character occurring at most k times | CPP program to count number of substrings in which each character has count less than or equal to k . ; function to find number of substring in which each character has count less than or equal to k . ; initialize left and right pointer to 0 ; an array to keep track of count of each alphabet ; decrement the count ; increment left pointer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_sub ( string s , int k ) { int len = s . length ( ) ; int lp = 0 , rp = 0 ; int ans = 0 ; int hash_char [ 26 ] = { 0 } ; for ( ; rp < len ; rp ++ ) { hash_char [ s [ rp ] - ' a ' ] ++ ; while ( hash_char [ s [ rp ] - ' a ' ] > k ) { hash_char [ s [ lp ] - ' a ' ] -- ; lp ++ ; } ans += rp - lp + 1 ; } return ans ; } int main ( ) { string s = " aaabb " ; int k = 2 ; cout << find_sub ( s , k ) << endl ; }
Remove consecutive vowels from string | C ++ program for printing sentence without repetitive vowels ; function which returns True or False for occurrence of a vowel ; this compares vowel with character ' c ' ; function to print resultant string ; print 1 st character ; loop to check for each character ; comparison of consecutive characters ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_vow ( char c ) { return ( c == ' a ' ) || ( c == ' e ' ) || ( c == ' i ' ) || ( c == ' o ' ) || ( c == ' u ' ) ; } void removeVowels ( string str ) { printf ( " % c " , str [ 0 ] ) ; for ( int i = 1 ; str [ i ] ; i ++ ) if ( ( ! is_vow ( str [ i - 1 ] ) ) || ( ! is_vow ( str [ i ] ) ) ) printf ( " % c " , str [ i ] ) ; } int main ( ) { char str [ ] = " ▁ geeks ▁ for ▁ geeks " ; removeVowels ( str ) ; }
Find the Number which contain the digit d | CPP program to print the number which contain the digit d from 0 to n ; function to display the values ; Converting d to character ; Loop to check each digit one by one . ; initialize the string ; checking for digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNumbers ( int n , int d ) { string st = " " ; st += to_string ( d ) ; char ch = st [ 0 ] ; string p = " " ; p += ch ; for ( int i = 0 ; i <= n ; i ++ ) { st = " " ; st = st + to_string ( i ) ; int idx = st . find ( p ) ; if ( i == d idx != -1 ) cout << ( i ) << " ▁ " ; } } int main ( ) { int n = 100 , d = 5 ; printNumbers ( n , d ) ; }
Transform a string such that it has abcd . . z as a subsequence | C Plus Plus Program to transform the given string to contain the required subsequence ; function to transform string with string passed as reference ; initializing the variable ch to ' a ' ; if the length of string is less than 26 , we can 't obtain the required  subsequence ; if ch has reached ' z ' , it means we have transformed our string such that required subsequence can be obtained ; current character is not greater than ch , then replace it with ch and increment ch ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool transformString ( string & s ) { char ch = ' a ' ; if ( s . size ( ) < 26 ) return false ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( int ( ch ) > int ( ' z ' ) ) break ; if ( s [ i ] <= ch ) { s [ i ] = ch ; ch = char ( int ( ch ) + 1 ) ; } } if ( ch <= ' z ' ) return false ; return true ; } int main ( ) { string str = " aaaaaaaaaaaaaaaaaaaaaaaaaa " ; if ( transformString ( str ) ) cout << str << endl ; else cout << " Not ▁ Possible " << endl ; return 0 ; }
Number of pairs with Pandigital Concatenation | C ++ program to find all Pandigital concatenations of two strings . ; Checks if a given string is Pandigital ; digit i is not present thus not pandigital ; Returns number of pairs of strings resulting in Pandigital Concatenations ; iterate over all pair of strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPanDigital ( string s ) { bool digits [ 10 ] = { false } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) digits [ s [ i ] - '0' ] = true ; for ( int i = 0 ; i <= 9 ; i ++ ) if ( digits [ i ] == false ) return false ; return true ; } int countPandigitalPairs ( vector < string > & v ) { int pairs = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) if ( isPanDigital ( v [ i ] + v [ j ] ) ) pairs ++ ; return pairs ; } int main ( ) { vector < string > v = { "123567" , "098234" , "14765" , "19804" } ; cout << countPandigitalPairs ( v ) << endl ; return 0 ; }
Number of pairs with Pandigital Concatenation | C ++ program to count PanDigital pairs ; Stores digits present in string v [ i ] atleast once . We use a set as we only need digits which exist only once ( irrespective of reputation ) ; Calculate the mask by considering all digits existing atleast once ; Increment the frequency of this mask ; Returns number of pairs of strings resulting in Pandigital Concatenations ; All possible strings lie between 1 and 1023 so we iterate over every possible mask ; if the concatenation results in mask of Pandigital Concatenation , calculate all pairs formed with Masks i and j ; since every pair is considers twice , we get rid of half of these ; Find frequencies of all masks in given vector of strings ; Return all possiblg concatenations . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; void computeMaskFrequencies ( vector < string > v , map < int , int > & freq ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mask = 0 ; unordered_set < int > digits ; for ( int j = 0 ; j < v [ i ] . size ( ) ; j ++ ) digits . insert ( v [ i ] [ j ] - '0' ) ; for ( auto it = digits . begin ( ) ; it != digits . end ( ) ; it ++ ) { int digit = ( * it ) ; mask += ( 1 << digit ) ; } freq [ mask ] ++ ; } } int pandigitalConcatenations ( map < int , int > freq ) { int ans = 0 ; for ( int i = 1 ; i <= 1023 ; i ++ ) { for ( int j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask ) { if ( i == j ) ans += ( freq [ i ] * ( freq [ i ] - 1 ) ) ; else ans += ( freq [ i ] * freq [ j ] ) ; } } } return ans / 2 ; } int countPandigitalPairs ( vector < string > v ) { map < int , int > freq ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } int main ( ) { vector < string > v = { "123567" , "098234" , "14765" , "19804" } ; cout << countPandigitalPairs ( v ) << endl ; return 0 ; }
Print the arranged positions of characters to make palindrome | CPP program to print original positions of characters in a string after rearranging and forming a palindrome ; Maximum number of characters ; Insert all positions of every character in the given string . ; find the number of odd elements . Takes O ( n ) ; A palindrome cannot contain more than 1 odd characters ; Print positions in first half of palindrome ; Consider one instance odd character ; Print positions in second half of palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 256 ; void printPalindromePos ( string & str ) { vector < int > pos [ MAX ] ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) pos [ str [ i ] ] . push_back ( i + 1 ) ; int oddCount = 0 ; char oddChar ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pos [ i ] . size ( ) % 2 != 0 ) { oddCount ++ ; oddChar = i ; } } if ( oddCount > 1 ) cout << " NO ▁ PALINDROME " ; for ( int i = 0 ; i < MAX ; i ++ ) { int mid = pos [ i ] . size ( ) / 2 ; for ( int j = 0 ; j < mid ; j ++ ) cout << pos [ i ] [ j ] << " ▁ " ; } if ( oddCount > 0 ) { int last = pos [ oddChar ] . size ( ) - 1 ; cout << pos [ oddChar ] [ last ] << " ▁ " ; pos [ oddChar ] . pop_back ( ) ; } for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int count = pos [ i ] . size ( ) ; for ( int j = count / 2 ; j < count ; j ++ ) cout << pos [ i ] [ j ] << " ▁ " ; } } int main ( ) { string s = " geeksgk " ; printPalindromePos ( s ) ; return 0 ; }
Minimum changes to a string to make all substrings distinct | CPP program to count number of changes to make all substrings distinct . ; Returns minimum changes to str so that no substring is repeated . ; If length is more than maximum allowed characters , we cannot get the required string . ; Variable to store count of distinct characters ; To store counts of different characters ; Answer is , n - number of distinct char ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; int minChanges ( string & str ) { int n = str . length ( ) ; if ( n > MAX_CHAR ) return -1 ; int dist_count = 0 ; int count [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ str [ i ] - ' a ' ] == 0 ) dist_count ++ ; count [ ( str [ i ] - ' a ' ) ] ++ ; } return ( n - dist_count ) ; } int main ( ) { string str = " aebaecedabbee " ; cout << minChanges ( str ) ; return 0 ; }
Reverse each word in a linked list node | C ++ program to reverse each word in a linked list ; Linked list Node structure ; Function to create newNode in a linked list ; reverse each node data ; iterate each node and call reverse_word for each node data ; printing linked list ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { string c ; struct Node * next ; } ; struct Node * newNode ( string c ) { Node * temp = new Node ; temp -> c = c ; temp -> next = NULL ; return temp ; } ; void reverse_word ( string & str ) { reverse ( str . begin ( ) , str . end ( ) ) ; } void reverse ( struct Node * head ) { struct Node * ptr = head ; while ( ptr != NULL ) { reverse_word ( ptr -> c ) ; ptr = ptr -> next ; } } void printList ( struct Node * head ) { while ( head != NULL ) { cout << head -> c << " ▁ " ; head = head -> next ; } } int main ( ) { Node * head = newNode ( " Geeksforgeeks " ) ; head -> next = newNode ( " a " ) ; head -> next -> next = newNode ( " computer " ) ; head -> next -> next -> next = newNode ( " science " ) ; head -> next -> next -> next -> next = newNode ( " portal " ) ; head -> next -> next -> next -> next -> next = newNode ( " for " ) ; head -> next -> next -> next -> next -> next -> next = newNode ( " geeks " ) ; cout << " List ▁ before ▁ reverse : ▁ STRNEWLINE " ; printList ( head ) ; reverse ( head ) ; cout << " List after reverse : " ; printList ( head ) ; return 0 ; }
Number of strings of length N with no palindromic sub string | CPP program to count number of strings of size m such that no substring is palindrome . ; Return the count of strings with no palindromic substring . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numofstring ( int n , int m ) { if ( n == 1 ) return m ; if ( n == 2 ) return m * ( m - 1 ) ; return m * ( m - 1 ) * pow ( m - 2 , n - 2 ) ; } int main ( ) { int n = 2 , m = 3 ; cout << numofstring ( n , m ) << endl ; return 0 ; }
Count special palindromes in a String | C ++ program to count special Palindromic substring ; Function to count special Palindromic susbstring ; store count of special Palindromic substring ; it will store the count of continues same char ; traverse string character from left to right ; store same character count ; count smiler character ; Case : 1 so total number of substring that we can generate are : K * ( K + 1 ) / 2 here K is sameCharCount ; store current same char count in sameChar [ ] array ; increment i ; Case 2 : Count all odd length Special Palindromic substring ; if current character is equal to previous one then we assign Previous same character count to current one ; case 2 : odd length ; subtract all single length substring ; driver program to test above fun
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountSpecialPalindrome ( string str ) { int n = str . length ( ) ; int result = 0 ; int sameChar [ n ] = { 0 } ; int i = 0 ; while ( i < n ) { int sameCharCount = 1 ; int j = i + 1 ; while ( str [ i ] == str [ j ] && j < n ) sameCharCount ++ , j ++ ; result += ( sameCharCount * ( sameCharCount + 1 ) / 2 ) ; sameChar [ i ] = sameCharCount ; i = j ; } for ( int j = 1 ; j < n ; j ++ ) { if ( str [ j ] == str [ j - 1 ] ) sameChar [ j ] = sameChar [ j - 1 ] ; if ( j > 0 && j < ( n - 1 ) && ( str [ j - 1 ] == str [ j + 1 ] && str [ j ] != str [ j - 1 ] ) ) result += min ( sameChar [ j - 1 ] , sameChar [ j + 1 ] ) ; } return result - n ; } int main ( ) { string str = " abccba " ; cout << CountSpecialPalindrome ( str ) << endl ; return 0 ; }
Print Bracket Number | C ++ implementation to print the bracket number ; function to print the bracket number ; used to print the bracket number for the left bracket ; used to obtain the bracket number for the right bracket ; traverse the given expression ' exp ' ; if current character is a left bracket ; print ' left _ bnum ' , ; push ' left _ bum ' on to the stack ' right _ bnum ' ; increment ' left _ bnum ' by 1 ; else if current character is a right bracket ; print the top element of stack ' right _ bnum ' it will be the right bracket number ; pop the top element from the stack ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printBracketNumber ( string exp , int n ) { int left_bnum = 1 ; stack < int > right_bnum ; for ( int i = 0 ; i < n ; i ++ ) { if ( exp [ i ] == ' ( ' ) { cout << left_bnum << " ▁ " ; right_bnum . push ( left_bnum ) ; left_bnum ++ ; } else if ( exp [ i ] == ' ) ' ) { cout << right_bnum . top ( ) << " ▁ " ; right_bnum . pop ( ) ; } } } int main ( ) { string exp = " ( a + ( b * c ) ) + ( d / e ) " ; int n = exp . size ( ) ; printBracketNumber ( exp , n ) ; return 0 ; }
Find if a string starts and ends with another given string | CPP program to find if a given corner string is present at corners . ; If length of corner string is more , it cannot be present at corners . ; Return true if corner string is present at both corners of given string . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCornerPresent ( string str , string corner ) { int n = str . length ( ) ; int cl = corner . length ( ) ; if ( n < cl ) return false ; return ( str . substr ( 0 , cl ) . compare ( corner ) == 0 && str . substr ( n - cl , cl ) . compare ( corner ) == 0 ) ; } int main ( ) { string str = " geeksforgeeks " ; string corner = " geeks " ; if ( isCornerPresent ( str , corner ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find i 'th Index character in a binary string obtained after n iterations | C ++ Program to find ith character in a binary string . ; Function to store binary Representation ; Function to find ith character ; Function to change decimal to binary ; Assign s1 string in s string ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void binary_conversion ( string & s , int m ) { while ( m ) { int tmp = m % 2 ; s += tmp + '0' ; m = m / 2 ; } reverse ( s . begin ( ) , s . end ( ) ) ; } int find_character ( int n , int m , int i ) { string s ; binary_conversion ( s , m ) ; string s1 = " " ; for ( int x = 0 ; x < n ; x ++ ) { for ( int y = 0 ; y < s . length ( ) ; y ++ ) { if ( s [ y ] == '1' ) s1 += "10" ; else s1 += "01" ; } s = s1 ; s1 = " " ; } return s [ i ] - '0' ; } int main ( ) { int m = 5 , n = 2 , i = 8 ; cout << find_character ( n , m , i ) ; return 0 ; }
Maximum segment value after putting k breakpoints in a number | CPP program to find the maximum segment value after putting k breaks . ; Function to Find Maximum Number ; Maximum segment length ; Find value of first segment of seg_len ; Find value of remaining segments using sliding window ; To find value of current segment , first remove leading digit from previous value ; Then add trailing digit ; Driver 's Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSegment ( string & s , int k ) { int seg_len = s . length ( ) - k ; int res = 0 ; for ( int i = 0 ; i < seg_len ; i ++ ) res = res * 10 + ( s [ i ] - '0' ) ; int seg_len_pow = pow ( 10 , seg_len - 1 ) ; int curr_val = res ; for ( int i = 1 ; i <= ( s . length ( ) - seg_len ) ; i ++ ) { curr_val = curr_val - ( s [ i - 1 ] - '0' ) * seg_len_pow ; curr_val = curr_val * 10 + ( s [ i + seg_len - 1 ] - '0' ) ; res = max ( res , curr_val ) ; } return res ; } int main ( ) { string s = "8754" ; int k = 2 ; cout << " Maximum ▁ number ▁ = ▁ " << findMaxSegment ( s , k ) ; return 0 ; }
Consecutive sequenced numbers in a string | CPP Program to check whether a string contains consecutive sequential numbers or not ; function to check consecutive sequential number ; variable to store starting number ; length of the input string ; find the number till half of the string ; new string containing the starting substring of input string ; converting starting substring into number ; backing up the starting number in start ; while loop until the new_string is smaller than input string ; next number ; concatenate the next number ; check if new string becomes equal to input string ; if string doesn 't contains consecutive numbers ; Driver 's Code
#include <iostream> NEW_LINE using namespace std ; int isConsecutive ( string str ) { int start ; int length = str . size ( ) ; for ( int i = 0 ; i < length / 2 ; i ++ ) { string new_str = str . substr ( 0 , i + 1 ) ; int num = atoi ( new_str . c_str ( ) ) ; start = num ; while ( new_str . size ( ) < length ) { num ++ ; new_str = new_str + to_string ( num ) ; } if ( new_str == str ) return start ; } return -1 ; } int main ( ) { string str = "99100" ; cout << " String : ▁ " << str << endl ; int start = isConsecutive ( str ) ; if ( start != -1 ) cout << " Yes ▁ STRNEWLINE " << start << endl ; else cout << " No " << endl ; string str1 = "121315" ; cout << " String : " start = isConsecutive ( str1 ) ; if ( start != -1 ) cout << " Yes ▁ STRNEWLINE " << start << endl ; else cout << " No " << endl ; return 0 ; }
Converting one string to other using append and delete last operations | CPP Program to convert str1 to str2 in exactly k operations ; Returns true if it is possible to convert str1 to str2 using k operations . ; Case A ( i ) ; finding common length of both string ; Case A ( ii ) - ; Case B - ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isConvertible ( string str1 , string str2 , int k ) { if ( ( str1 . length ( ) + str2 . length ( ) ) < k ) return true ; int commonLength = 0 ; for ( int i = 0 ; i < min ( str1 . length ( ) , str2 . length ( ) ) ; i ++ ) { if ( str1 [ i ] == str2 [ i ] ) commonLength ++ ; else break ; } if ( ( k - str1 . length ( ) - str2 . length ( ) + 2 * commonLength ) % 2 == 0 ) return true ; return false ; } int main ( ) { string str1 = " geek " , str2 = " geek " ; int k = 7 ; if ( isConvertible ( str1 , str2 , k ) ) cout << " Yes " ; else cout << " No " ; str1 = " geeks " , str2 = " geek " ; k = 5 ; cout << endl ; if ( isConvertible ( str1 , str2 , k ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum distinct lowercase alphabets between two uppercase | CPP Program to find maximum lowercase alphabets present between two uppercase alphabets ; Function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; Ignoring lowercase characters in the beginning . ; We start from next of first capital letter and traverse through remaining character . ; If character is in uppercase , ; Count all distinct lower case characters ; Update maximum count ; Reset count array ; If character is in lowercase ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_CHAR  26 NEW_LINE int maxLower ( string str ) { int n = str . length ( ) ; int i = 0 ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { i ++ ; break ; } } int maxCount = 0 ; int count [ MAX_CHAR ] = { 0 } ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { int currCount = 0 ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) if ( count [ j ] > 0 ) currCount ++ ; maxCount = max ( maxCount , currCount ) ; memset ( count , 0 , sizeof ( count ) ) ; } if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) count [ str [ i ] - ' a ' ] ++ ; } return maxCount ; } int main ( ) { string str = " zACaAbbaazzC " ; cout << maxLower ( str ) ; return 0 ; }