codeparrot/xlcost-text-to-code huggingface dataset:
Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | C ++ program for the above approach ; Function to count minimum number of removal of consecutive 0 s required to make binary string consists only of 1 s ; Stores the count of removals ; Traverse the string S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary string only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRemovals ( string str , int N ) { int ans = 0 ; bool X = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { ans ++ ; while ( str [ i ] == '0' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str [ 0 ] == '0' and str [ N - 1 ] == '0' ) { ans -- ; } return ans ; } int main ( ) { string S = "11010001" ; int N = S . size ( ) ; cout << minRemovals ( S , N ) ; return 0 ; }
Minimum removals required to place all 0 s before 1 s in a Binary String | C ++ program for the above approach ; Function to count minimum removals required to arrange all 0 s before 1 s ; Count the occurences of 0 in s ; Size of the string ; Stores the minimum number of removals required ; Iterate over each of the characters in the string s ; If the i - th character is found to be '0' ; Store the minimum of res and right_0 + left_1 in res ; Return the final result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumDeletions ( string s ) { int right_0 = count ( s . begin ( ) , s . end ( ) , '0' ) ; int left_1 = 0 ; int n = s . size ( ) ; int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { right_0 -= 1 ; } else { left_1 += 1 ; } res = min ( res , right_0 + left_1 ) ; } return res ; } int main ( ) { string s = "001101" ; int count = minimumDeletions ( s ) ; cout << count ; return 0 ; }
Minimum substring flips required to convert a Binary String to another | C ++ program for the above approach ; Function to count the minimum number of reversals required to make the given binary strings s1 and s2 same ; Stores the minimum count of reversal of substrings required ; If the length of the strings are not the same then return - 1 ; Iterate over each character ; If s1 [ i ] is not equal to s2 [ i ] ; Iterate until s1 [ i ] != s2 [ i ] ; Increment answer by 1 ; Return the resultant count of reversal of substring required ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int canMakeSame ( string s1 , string s2 ) { int ans = 0 ; if ( s1 . size ( ) != s2 . size ( ) ) { return -1 ; } int N = s1 . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { while ( i < s1 . length ( ) && s1 [ i ] != s2 [ i ] ) { i ++ ; } ans ++ ; } } return ans ; } int main ( ) { string S1 = "100001" ; string S2 = "110111" ; cout << canMakeSame ( S1 , S2 ) ; return 0 ; }
Count intervals that intersects with a given meeting time | C ++ implementation of the above approach ; Function to convert a time in 24 hour format to an equivalent integer ; Removes " : " at 3 rd position ; Calculate hours ; Stores the time in 24 hours format ; If time is in " AM " ; If hh is equal to 12 ; If time is in " PM " ; If hh is equal to 12 ; Return time ; Function to count number of intervals in which p lies ; Stores the count ; Stores the integer value of 24 hours time format of P ; Traverse the array ; Stores the integer value of 24 hours time format of arr [ i ] [ 0 ] ; Stores the integer value of 24 hours time format of arr [ i ] [ 1 ] ; If M lies within the [ L , R ] ; Increment ans by 1 ; Return ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int convert ( string str ) { str . replace ( 2 , 1 , " " ) ; int h1 = ( int ) str [ 1 ] - '0' ; int h2 = ( int ) str [ 0 ] - '0' ; int hh = ( h2 * 10 + h1 % 10 ) ; int time = 0 ; if ( str [ 5 ] == ' A ' ) { if ( hh == 12 ) time += stoi ( str . substr ( 2 , 2 ) ) ; else { time += stoi ( str . substr ( 0 , 2 ) ) ; } } else { if ( hh == 12 ) { time += stoi ( str . substr ( 0 , 4 ) ) ; } else { time += stoi ( str . substr ( 0 , 4 ) ) ; time += 1200 ; } } return time ; } int countOverlap ( string arr [ ] [ 2 ] , int n , string p ) { int ans = 0 ; int M = convert ( p ) ; for ( int i = 0 ; i < n ; i ++ ) { int L = convert ( arr [ i ] [ 0 ] ) ; int R = convert ( arr [ i ] [ 1 ] ) ; if ( ( L <= M && M <= R ) || ( M >= R && M <= L ) ) ans ++ ; } return ans ; } int main ( ) { string arr [ ] [ 2 ] = { { "12:00 : AM " , "11:55 : PM " } , { "12:01 : AM " , "11:50 : AM " } , { "12:30 : AM " , "12:00 : PM " } , { "11:57 : AM " , "11:59 : PM " } } ; string P = "12:01 : PM " ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countOverlap ( arr , N , P ) << endl ; }
Count points which are revisited while following the path specified by a given string | C ++ program for the above approach ; Function to find the number of times already visited position is revisited after starting traversal from { X , Y } ; Stores the x and y temporarily ; Stores the number of times an already visited position is revisited ; Initialize hashset ; Insert the starting coordinates ; Traverse over the string ; Update the coordinates according to the current directions ; If the new { X , Y } has been visited before , then increment the count by 1 ; Otherwise ; Insert new { x , y } ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( string S , int X , int Y ) { int N = S . length ( ) ; int temp_x = 0 , temp_y = 0 ; int count = 0 ; set < pair < int , int > > s ; s . insert ( { X , Y } ) ; for ( int i = 0 ; i < N ; i ++ ) { temp_x = X ; temp_y = Y ; if ( S [ i ] == ' U ' ) { X ++ ; } else if ( S [ i ] == ' D ' ) { X -- ; } else if ( S [ i ] == ' R ' ) { Y ++ ; } else { Y -- ; } if ( s . find ( { temp_x + X , temp_y + Y } ) != s . end ( ) ) { count ++ ; } else { s . insert ( { temp_x + X , temp_y + Y } ) ; } } return count ; } int main ( ) { string S = " RDDUDL " ; int X = 0 , Y = 0 ; cout << count ( S , X , Y ) ; return 0 ; }
Sum of frequencies of characters of a string present in another string | C ++ program for the above approach ; Function to find sum of frequencies of characters of S1 present in S2 ; Insert all characters of string S1 in the set ; Traverse the string S2 ; Check if X is present in bset or not ; Increment count by 1 ; Finally , print the count ; Driver Code ; Given strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countTotalFrequencies ( string S1 , string S2 ) { set < char > bset ; for ( auto x : S1 ) bset . insert ( x ) ; int count = 0 ; for ( auto x : S2 ) { if ( bset . find ( x ) != bset . end ( ) ) count += 1 ; } cout << count << endl ; } int main ( ) { string S1 = " geEksFOR " ; string S2 = " GeEksforgeEKS " ; countTotalFrequencies ( S1 , S2 ) ; }
Check if characters of a string can be made non | C ++ program for the above approach ; Function to check if it 's possible to make the string s non decreasing ; Stores length of string ; Stores the last character that is not ' ? ' ; Traverse the string S ; If current character of the string is ' ? ' ; If s [ i ] is not ' ? ' and is less than C ; Otherwise ; Update C ; Return 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nonDecreasing ( string s ) { int size = s . length ( ) ; char c = ' a ' ; for ( int i = 0 ; i < size ; i ++ ) { if ( s [ i ] == ' ? ' ) { continue ; } else if ( ( s [ i ] != ' ? ' ) && ( s [ i ] < c ) ) { return 0 ; } else { c = s [ i ] ; } } return 1 ; } int main ( ) { string S = " abb ? xy ? " ; if ( nonDecreasing ( S ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Make a given Binary String non | C ++ program for the above approach ; Function to return the length of smallest subsequence required to be removed to make the given string non - decreasing ; Length of the string ; Count of zeros and ones ; Traverse the string ; Count minimum removals to obtain strings of the form "00000 . . . . " or "11111 . . . " ; Increment count ; Remove 1 s and remaining 0 s ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_length ( string str ) { int n = str . length ( ) ; int total_zeros = 0 ; int total_ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) total_zeros ++ ; else total_ones ++ ; } int ans = min ( total_zeros , total_ones ) ; int cur_zeros = 0 , cur_ones = 0 ; for ( char x : str ) { if ( x == '0' ) cur_zeros ++ ; else cur_ones ++ ; ans = min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } cout << ans ; } int main ( ) { string str = "10011" ; min_length ( str ) ; return 0 ; }
Print all words occurring in a sentence exactly K times | CPP program for the above approach ; Function to print all the words occurring k times in a string ; Stores the words ; Traverse the list ; Check for count ; Print the word ; Remove from list ; Driver Code ; Given string ; Given value of K ; Function call to find all words occurring K times
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kFreqWords ( string S , int K ) { string temp = " " ; vector < string > l ; for ( auto x : S ) { if ( x == ' ▁ ' ) { l . push_back ( temp ) ; temp = " " ; } else temp += x ; } for ( auto x : l ) { if ( count ( l . begin ( ) , l . end ( ) , x ) == K ) { cout << x << endl ; remove ( l . begin ( ) , l . end ( ) , x ) ; } } } int main ( ) { string S = " banana ▁ is ▁ in ▁ yellow ▁ and ▁ sun ▁ flower ▁ is ▁ also ▁ in ▁ yellow ▁ " ; int K = 2 ; kFreqWords ( S , K ) ; }
Check if substrings from three given strings can be concatenated to form a palindrome | C ++ program for the above approach ; Function to check if substrings from three given strings can be concatenated to form a palindrome ; Mask for S1 and S2 ; Set ( i - ' a ' ) th bit in maskA ; Set ( i - ' a ' ) th bit in maskC ; If the bitwise AND is > 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string make_palindrome ( string S1 , string S2 , string S3 ) { int maskA = 0 , maskC = 0 ; for ( char i : S1 ) maskA |= ( 1 << ( i - ' a ' ) ) ; for ( char i : S3 ) maskC |= ( 1 << ( i - ' a ' ) ) ; if ( ( maskA & maskC ) > 0 ) return " YES " ; return " NO " ; } int main ( ) { string S1 = " adcb " , S2 = " bcdb " , S3 = " abe " ; cout << make_palindrome ( S1 , S2 , S3 ) ; }
Length of longest prefix anagram which are common in given two strings | C ++ program for the above approach ; Function to check if two arrays are identical or not ; Iterate over the range [ 0 , SIZE ] ; If frequency any character is not same in both the strings ; Otherwise ; Function to find the maximum length of the required string ; Store the count of characters in string str1 ; Store the count of characters in string str2 ; Stores the maximum length ; Minimum length of str1 and str2 ; Increment the count of characters of str1 [ i ] in freq1 [ ] by one ; Increment the count of characters of str2 [ i ] in freq2 [ ] by one ; Checks if prefixes are anagram or not ; Finally print the ans ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE bool longHelper ( int freq1 [ ] , int freq2 [ ] ) { for ( int i = 0 ; i < SIZE ; ++ i ) { if ( freq1 [ i ] != freq2 [ i ] ) { return false ; } } return true ; } int longCommomPrefixAnagram ( string s1 , string s2 , int n1 , int n2 ) { int freq1 [ 26 ] = { 0 } ; int freq2 [ 26 ] = { 0 } ; int ans = 0 ; int mini_len = min ( n1 , n2 ) ; for ( int i = 0 ; i < mini_len ; ++ i ) { freq1 [ s1 [ i ] - ' a ' ] ++ ; freq2 [ s2 [ i ] - ' a ' ] ++ ; if ( longHelper ( freq1 , freq2 ) ) { ans = i + 1 ; } } cout << ans ; } int main ( ) { string str1 = " abaabcdezzwer " ; string str2 = " caaabbttyh " ; int N = str1 . length ( ) ; int M = str2 . length ( ) ; longCommomPrefixAnagram ( str1 , str2 , N , M ) ; return 0 ; }
Lexicographic rank of a Binary String | C ++ program for the above approach ; Function to find the rank of a string ; Store the length of the string ; Stores its equivalent decimal value ; Traverse the string ; Store the number of strings of length less than N occurring before the given string ; Store the decimal equivalent number of string bin ; Store the rank in answer ; Print the answer ; Driver program
#include <iostream> NEW_LINE using namespace std ; void findRank ( string s ) { int N = s . size ( ) ; string bin ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '0' ) bin += "0" ; else bin += "1" ; } long long X = 1ll << N ; long long res = 0 , val = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) res += ( val ) ; val *= 2ll ; } long long Y = res ; long ans = X + Y - 1 ; cout << ans ; } int main ( ) { string S = "001" ; findRank ( S ) ; return 0 ; }
Find the GCD of an array made up of numeric strings | CPP program for the above approach ; Recursive function to return gcd of A and B ; Base case ; Length of A is greater ; Calculate GCD ; Store the GCD of the length of the strings ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GCD ( int lena , int lenb ) { if ( lena == 0 ) return lenb ; if ( lenb == 0 ) return lena ; if ( lena == lenb ) return lena ; if ( lena > lenb ) return GCD ( lena - lenb , lenb ) ; return GCD ( lena , lenb - lena ) ; } string StringGCD ( string a , string b ) { int gcd = GCD ( a . size ( ) , b . size ( ) ) ; if ( a . substr ( 0 , gcd ) == b . substr ( 0 , gcd ) ) { int x = ( ( int ) b . size ( ) / gcd ) ; int y = ( ( int ) a . size ( ) / gcd ) ; string r = " " , s = " " ; while ( x -- ) s += a ; while ( y -- ) r += b ; if ( s == r ) return a . substr ( 0 , gcd ) ; } return " - 1" ; } int main ( ) { string a = " geeksgeeks " ; string b = " geeks " ; cout << ( StringGCD ( a , b ) ) ; }
Calculate score of a string consisting of balanced parentheses | C ++ program to implement the above approach ; Function to calculate score of parentheses ; Stores index of character of string ; Stores total scores obtained from the string ; Iterate over characters of the string ; If s [ i ] is ' ( ' ; If top element of stack is ' ( ' ; Stores score of inner parentheses ; Calculate score of inner parentheses ; Update count ; Pop from stack ; Insert score of inner parentheses ; Update i ; Calculate score of the string ; Update ans ; Driver Code
#include <iostream> NEW_LINE #include <stack> NEW_LINE using namespace std ; long long scoreOfParentheses ( string S ) { stack < string > s ; int i = 0 ; long long ans = 0 ; while ( i < S . length ( ) ) { if ( S [ i ] == ' ( ' ) s . push ( " ( " ) ; else { if ( s . top ( ) == " ( " ) { s . pop ( ) ; s . push ( "1" ) ; } else { long long count = 0 ; while ( s . top ( ) != " ( " ) { count += stoi ( s . top ( ) ) ; s . pop ( ) ; } s . pop ( ) ; s . push ( to_string ( 2 * count ) ) ; } } i ++ ; } while ( ! s . empty ( ) ) { ans += stoi ( s . top ( ) ) ; s . pop ( ) ; } return ans ; } int main ( ) { string S1 = " ( ( ) ( ( ) ) ) " ; cout << scoreOfParentheses ( S1 ) << endl ; return 0 ; }
Minimize cost to rearrange substrings to convert a string to a Balanced Bracket Sequence | C ++ program for the above approach ; Function to count minimum number of operations to convert the string to a balanced bracket sequence ; Initialize the integer array ; Traverse the string ; Decrement a [ i ] ; Increment a [ i ] ; Update the sum as current value of arr [ i ] ; If answer exists ; Traverse from i ; Find all substrings with 0 sum ; Print the sum of sizes ; No answer exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countMinMoves ( string str ) { int n = str . size ( ) ; int a [ n ] = { 0 } ; int j , ans = 0 , i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ) ' ) { a [ i ] += sum - 1 ; } else { a [ i ] += sum + 1 ; } sum = a [ i ] ; } if ( sum == 0 ) { i = 1 ; while ( i < n ) { j = i - 1 ; while ( i < n && a [ i ] != 0 ) i ++ ; if ( i < n && a [ i - 1 ] < 0 ) { ans += i - j ; if ( j == 0 ) ans ++ ; } i ++ ; } cout << ans << endl ; } else cout << " - 1 STRNEWLINE " ; } int main ( ) { string str = " ) ( ( ) " ; countMinMoves ( str ) ; return 0 ; }
Check if a given string can be converted to a Balanced Bracket Sequence | C ++ program for the above approach ; Function to check if the string can be balanced by replacing the ' $ ' with opening or closing brackets ; If string can never be balanced ; Declare 2 stacks to check if all ) can be balanced with ( or $ and vice - versa ; Store the count the occurence of ( , ) and $ ; Traverse the string ; Increment closed bracket count by 1 ; If there are no opening bracket to match it then return false ; Otherwise , pop the character from the stack ; If current character is an opening bracket or $ , push it to the stack ; Increment symbol count by 1 ; Increment open bracket count by 1 ; Traverse the string from end and repeat the same process ; If there are no closing brackets to match it ; Otherwise , pop character from stack ; Store the extra ( or ) which are not balanced yet ; Check if $ is available to balance the extra brackets ; Count ramaining $ after balancing extra ( and ) ; Check if each pair of $ is convertable in ( ) ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeBalanced ( string sequence ) { if ( sequence . size ( ) % 2 ) return false ; stack < char > stack_ , stack2_ ; int countOpen = 0 , countClosed = 0 ; int countSymbol = 0 ; for ( int i = 0 ; i < sequence . size ( ) ; i ++ ) { if ( sequence [ i ] == ' ) ' ) { countClosed ++ ; if ( stack_ . empty ( ) ) { return false ; } else { stack_ . pop ( ) ; } } else { if ( sequence [ i ] == ' $ ' ) { countSymbol ++ ; } else { countOpen ++ ; } stack_ . push ( sequence [ i ] ) ; } } for ( int i = sequence . size ( ) - 1 ; i >= 0 ; i -- ) { if ( sequence [ i ] == ' ( ' ) { if ( stack2_ . empty ( ) ) { return false ; } else { stack2_ . pop ( ) ; } } else { stack2_ . push ( sequence [ i ] ) ; } } int extra = abs ( countClosed - countOpen ) ; if ( countSymbol < extra ) { return false ; } else { countSymbol -= extra ; if ( countSymbol % 2 == 0 ) { return true ; } } return false ; } int main ( ) { string S = " ( ) ( $ " ; if ( canBeBalanced ( S ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | C ++ program for the above approach ; Function to find difference between maximum and minimum frequency of a character in given range ; Stores length of string ; Stores count of queries ; Iterate over the characters of the string ; Stores l - value of a query ; Stores r - value of a query ; Store count of every character laying in range [ l , r ] ; Update frequency of current character ; Stores maximum frequency of characters in given range ; Stores minimum frequency of characters in given range ; Iterate over all possible characters of the given string ; Update mx ; If ( j + ' a ' ) is present ; difference between max and min ; Driver Code ; Given string ; Given queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxDiffFreq ( vector < pair < int , int > > queries , string S ) { int N = S . size ( ) ; int Q = queries . size ( ) ; for ( int i = 0 ; i < Q ; ++ i ) { int l = queries [ i ] . first - 1 ; int r = queries [ i ] . second - 1 ; int freq [ 26 ] = { 0 } ; for ( int j = l ; j <= r ; j ++ ) { freq [ S [ j ] - ' a ' ] ++ ; } int mx = 0 ; int mn = 99999999 ; for ( int j = 0 ; j < 26 ; j ++ ) { mx = max ( mx , freq [ j ] ) ; if ( freq [ j ] ) mn = min ( mn , freq [ j ] ) ; } cout << mx - mn << endl ; } } int main ( ) { string S = " abaabac " ; vector < pair < int , int > > queries { { 2 , 6 } , { 1 , 7 } } ; maxDiffFreq ( queries , S ) ; }
Count substrings consisting of equal number of a , b , c and d | C ++ program to implement the above approach ; Function to count the substring with equal number of a , b , c and d ; Stores relative frequency of the characters { ' a ' , ' b ' , ' c ' , ' d ' } ; Initially , frequencies of ' a ' , ' b ' , ' c ' and ' d ' are 0. ; Stores relative frequency of ' a ' ; Stores relative frequency of ' b ' ; Stores relative frequency of ' c ' ; Stores relative frequency of ' d ' ; Stores count of substring with equal number of ' a ' , ' b ' , ' c ' and ' d ' ; Iterate over the characters of the string ; If current character is ' a ' ; Update p ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; If current character is b ; Update q ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; Update r ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; Update s ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; Update relative frequency of { p , q , r , s } ; Traverse the map ; Stores count of relative frequency ; Update cntSub ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstrings ( string str ) { map < pair < pair < int , int > , pair < int , int > > , int > mp ; mp [ { { 0 , 0 } , { 0 , 0 } } ] ++ ; int p = 0 ; int q = 0 ; int r = 0 ; int s = 0 ; int cntSub = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' a ' ) { p ++ ; int Y = min ( min ( s , r ) , min ( p , q ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } else if ( str [ i ] == ' b ' ) { q ++ ; int Y = min ( min ( p , q ) , min ( r , s ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } else if ( str [ i ] == ' c ' ) { r ++ ; int Y = min ( min ( p , q ) , min ( r , s ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } else if ( str [ i ] == ' d ' ) { s ++ ; int Y = min ( min ( p , q ) , min ( r , s ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } mp [ { { p , q } , { r , s } } ] ++ ; } for ( auto & e : mp ) { int freq = e . second ; cntSub += ( freq ) * ( freq - 1 ) / 2 ; } return cntSub ; } int main ( ) { string str = " abcdefg " ; cout << countSubstrings ( str ) ; return 0 ; }
Spell Checker using Trie | C ++ program to implement the above approach ; Structure of a Trie node ; Store address of a character ; Check if the character is last character of a string or not ; Constructor function ; Function to insert a string into Trie ; Traverse the string , s ; Initialize a node ; Update temp ; Mark the last character of the string to true ; Function to print suggestions of the string ; If current character is the last character of a string ; Iterate over all possible characters of the string ; If current character present in the Trie ; Insert current character into Trie ; Function to check if the string is present in Trie or not ; Traverse the string ; If current character not present in the Trie ; Update root ; Driver Code ; Given array of strings ; Initialize a Trie ; Insert strings to trie
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TrieNode { TrieNode * Trie [ 256 ] ; bool isEnd ; TrieNode ( ) { for ( int i = 0 ; i < 256 ; i ++ ) { Trie [ i ] = NULL ; } isEnd = false ; } } ; void InsertTrie ( TrieNode * root , string s ) { TrieNode * temp = root ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( temp -> Trie [ s [ i ] ] == NULL ) { temp -> Trie [ s [ i ] ] = new TrieNode ( ) ; } temp = temp -> Trie [ s [ i ] ] ; } temp -> isEnd = true ; } void printSuggestions ( TrieNode * root , string res ) { if ( root -> isEnd == true ) { cout << res << " ▁ " ; } for ( int i = 0 ; i < 256 ; i ++ ) { if ( root -> Trie [ i ] != NULL ) { res . push_back ( i ) ; printSuggestions ( root -> Trie [ i ] , res ) ; res . pop_back ( ) ; } } } bool checkPresent ( TrieNode * root , string key ) { for ( int i = 0 ; i < key . length ( ) ; i ++ ) { if ( root -> Trie [ key [ i ] ] == NULL ) { printSuggestions ( root , key . substr ( 0 , i ) ) ; return false ; } root = root -> Trie [ key [ i ] ] ; } if ( root -> isEnd == true ) { return true ; } printSuggestions ( root , key ) ; return false ; } int main ( ) { vector < string > str = { " gee " , " geeks " , " ape " , " apple " , " geeksforgeeks " } ; string key = " geek " ; TrieNode * root = new TrieNode ( ) ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { InsertTrie ( root , str [ i ] ) ; } if ( checkPresent ( root , key ) ) { cout << " YES " ; } return 0 ; }