codeparrot/xlcost-text-to-code huggingface dataset:
Convert given Float value to equivalent Fraction | C ++ program for the above approach ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating string ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence re sequence ; Traverse the string ; Convert string to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . size ( ) is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Driver Code ; Given string str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findFraction ( string s ) { string be_deci = " " , af_deci = " " , reccu = " " ; bool x = true , y = false , z = false ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { if ( s [ i ] == ' . ' ) { x = false ; y = true ; continue ; } if ( s [ i ] == ' ( ' ) { z = true ; y = false ; continue ; } if ( x ) be_deci += s [ i ] ; if ( y ) af_deci += s [ i ] ; if ( z ) { for ( ; i < s . size ( ) && s [ i ] != ' ) ' ; ++ i ) reccu += s [ i ] ; break ; } } int num_be_deci = stoi ( be_deci ) ; int num_af_deci = 0 ; if ( af_deci . size ( ) != 0 ) num_af_deci = stoi ( af_deci ) ; int numr = num_be_deci * pow ( 10 , af_deci . size ( ) ) + num_af_deci ; int deno = pow ( 10 , af_deci . size ( ) ) ; if ( reccu . size ( ) == 0 ) { int gd = __gcd ( numr , deno ) ; cout << numr / gd << " ▁ / ▁ " << deno / gd ; } else { int reccu_num = stoi ( reccu ) ; int numr1 = numr * pow ( 10 , reccu . size ( ) ) + reccu_num ; int deno1 = deno * pow ( 10 , reccu . size ( ) ) ; int res_numr = numr1 - numr , res_deno = deno1 - deno ; int gd = __gcd ( res_numr , res_deno ) ; cout << res_numr / gd << " ▁ / ▁ " << res_deno / gd ; } } int main ( ) { string str = "23.98(231 ) " ; findFraction ( str ) ; return 0 ; }
Lexicographically largest string possible for a given cost of appending characters | C ++ Program to implement the above approach ; Function to find the lexicographically largest String possible ; If sum is less than 0 ; If sum is equal to 0 ; If sum is less than 0 ; Add current character ; Check if selecting current character generates lexicographically largest String ; Backtrack if solution not found ; Find the lexicographically largest String excluding the current character ; Function to print the lexicographically largest String generated ; Function call ; Stores the String ; Print the lexicographically largest String formed ; Driver code ; Cost of adding each alphabet ; Cost of generating the String
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool lexi_largest_String ( int a [ ] , int i , int sum , vector < int > & v ) { if ( sum < 0 ) return false ; if ( sum == 0 ) return true ; if ( i < 0 ) return false ; v . push_back ( i ) ; if ( lexi_largest_String ( a , i , sum - a [ i ] , v ) ) return true ; auto it = v . end ( ) ; it -- ; v . erase ( it ) ; return lexi_largest_String ( a , i - 1 , sum , v ) ; } void generateString ( int a [ ] , int sum ) { vector < int > v ; lexi_largest_String ( a , 25 , sum , v ) ; string s = " " ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) s += ( char ) ( v [ j ] + ' a ' ) ; cout << s << endl ; } int main ( ) { int a [ ] = { 1 , 1 , 2 , 33 , 4 , 6 , 9 , 7 , 36 , 32 , 58 , 32 , 28 , 904 , 22 , 255 , 47 , 69 , 558 , 544 , 21 , 36 , 48 , 85 , 48 , 58 } ; int sum = 236 ; generateString ( a , sum ) ; return 0 ; }
Count of distinct permutations of every possible length of given string | C ++ implementation of the above approach ; Function to find the factorial of a number ; Loop to find the factorial of the given number ; Function to find the number of permutations possible for a given string ; Function to find the total number of combinations possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int fact ( int a ) { int i , f = 1 ; for ( i = 2 ; i <= a ; i ++ ) f = f * i ; return f ; } int permute ( int n , int r ) { int ans = 0 ; ans = ( fact ( n ) / fact ( n - r ) ) ; return ans ; } int findPermutations ( int n ) { int sum = 0 , P ; for ( int r = 1 ; r <= n ; r ++ ) { P = permute ( n , r ) ; sum = sum + P ; } return sum ; } int main ( ) { string str = " xz " ; int result , n ; n = str . length ( ) ; cout << findPermutations ( n ) ; return 0 ; }
Cycles of length n in an undirected and connected graph | CPP Program to count cycles of length n in a given graph . ; Number of vertices ; mark the vertex vert as visited ; if the path of length ( n - 1 ) is found ; mark vert as un - visited to make it usable again . ; Check if vertex vert can end with vertex start ; For searching every possible path of length ( n - 1 ) ; DFS for searching path by decreasing length by 1 ; marking vert as unvisited to make it usable again . ; Counts cycles of length N in an undirected and connected graph . ; all vertex are marked un - visited initially . ; Searching for cycle by using v - n + 1 vertices ; ith vertex is marked as visited and will not be visited again . ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int V = 5 ; void DFS ( bool graph [ ] [ V ] , bool marked [ ] , int n , int vert , int start , int & count ) { marked [ vert ] = true ; if ( n == 0 ) { marked [ vert ] = false ; if ( graph [ vert ] [ start ] ) { count ++ ; return ; } else return ; } for ( int i = 0 ; i < V ; i ++ ) if ( ! marked [ i ] && graph [ vert ] [ i ] ) DFS ( graph , marked , n - 1 , i , start , count ) ; marked [ vert ] = false ; } int countCycles ( bool graph [ ] [ V ] , int n ) { bool marked [ V ] ; memset ( marked , 0 , sizeof ( marked ) ) ; int count = 0 ; for ( int i = 0 ; i < V - ( n - 1 ) ; i ++ ) { DFS ( graph , marked , n - 1 , i , i , count ) ; marked [ i ] = true ; } return count / 2 ; } int main ( ) { bool graph [ ] [ V ] = { { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } } ; int n = 4 ; cout << " Total ▁ cycles ▁ of ▁ length ▁ " << n << " ▁ are ▁ " << countCycles ( graph , n ) ; return 0 ; }
String hashing using Polynomial rolling hash function | C ++ implementation of the Polynomial Rolling Hash Function ; Function to calculate the hash of a string ; P and M ; Loop to calculate the hash value by iterating over the elements of string ; / Driver Code ; Given strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long polynomialRollingHash ( string const & str ) { int p = 31 ; int m = 1e9 + 9 ; long long power_of_p = 1 ; long long hash_val = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { hash_val = ( hash_val + ( str [ i ] - ' a ' + 1 ) * power_of_p ) % m ; power_of_p = ( power_of_p * p ) % m ; } return ( hash_val % m + m ) % m ; } int main ( ) { string str1 = " geeksforgeeks " ; string str2 = " geeks " ; cout << " Hash ▁ of ▁ ' " << str1 << " ' ▁ = ▁ " << polynomialRollingHash ( str1 ) ; cout << endl ; }
Find first non | C ++ implementation to find the first non - repeating element of the string using Linked List ; Function to find the first non - repeating element of the given string using Linked List ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void firstNonRepElement ( string str ) { map < char , int > mpp ; for ( auto i : str ) { mpp [ i ] ++ ; } for ( auto i : str ) { if ( mpp [ i ] == 1 ) { cout << i << endl ; return ; } } return ; } int main ( ) { string str = " geeksforgeeks " ; firstNonRepElement ( str ) ; }
Count of substrings of length K with exactly K distinct characters | C ++ program to find the count of k length substrings with k distinct characters using sliding window ; Function to return the required count of substrings ; Store the count ; Store the count of distinct characters in every window ; Store the frequency of the first K length substring ; Increase frequency of i - th character ; If K distinct characters exist ; Traverse the rest of the substring ; Increase the frequency of the last character of the current substring ; Decrease the frequency of the first character of the previous substring ; If the character is not present in the current substring ; If the count of distinct characters is 0 ; Return the count ; Driver code ; string str ; integer K ; Print the count of K length substrings with k distinct characters
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstrings ( string str , int K ) { int N = str . size ( ) ; int answer = 0 ; unordered_map < char , int > map ; for ( int i = 0 ; i < K ; i ++ ) { map [ str [ i ] ] ++ ; } if ( map . size ( ) == K ) answer ++ ; for ( int i = K ; i < N ; i ++ ) { map [ str [ i ] ] ++ ; map [ str [ i - K ] ] -- ; if ( map [ str [ i - K ] ] == 0 ) { map . erase ( str [ i - K ] ) ; } if ( map . size ( ) == K ) { answer ++ ; } } return answer ; } int main ( ) { string str = " aabcdabbcdc " ; int K = 3 ; cout << countSubstrings ( str , K ) << endl ; return 0 ; }
Check if a string is a scrambled form of another string | C ++ Program to check if a given string is a scrambled form of another string ; map declaration for storing key value pair means for storing subproblem result ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Check for the condition of anagram ; make key of type string for search in map ; checking if both string are before calculated or not if calculated means find in map then return it 's  value ; declaring flag variable to store result ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; add key & flag value to map ( store for future use ) so next time no required to calculate it again ; If none of the above conditions are satisfied ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_map < string , bool > mp ; bool isScramble ( string S1 , string S2 ) { if ( S1 . length ( ) != S2 . length ( ) ) { return false ; } int n = S1 . length ( ) ; if ( n == 0 ) { return true ; } if ( S1 == S2 ) { return true ; } string copy_S1 = S1 , copy_S2 = S2 ; sort ( copy_S1 . begin ( ) , copy_S1 . end ( ) ) ; sort ( copy_S2 . begin ( ) , copy_S2 . end ( ) ) ; if ( copy_S1 != copy_S2 ) { return false ; } string key = ( S1 + " ▁ " + S2 ) ; if ( mp . find ( key ) != mp . end ( ) ) { return mp [ key ] ; } bool flag = false ; for ( int i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substr ( 0 , i ) , S2 . substr ( 0 , i ) ) && isScramble ( S1 . substr ( i , n - i ) , S2 . substr ( i , n - i ) ) ) { flag = true ; return true ; } if ( isScramble ( S1 . substr ( 0 , i ) , S2 . substr ( n - i , i ) ) && isScramble ( S1 . substr ( i , n - i ) , S2 . substr ( 0 , n - i ) ) ) { flag = true ; return true ; } } mp [ key ] = flag ; return false ; } int main ( ) { string S1 = " coder " ; string S2 = " ocred " ; if ( isScramble ( S1 , S2 ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Print the longest palindromic prefix of a given string | C ++ program for the above approach ; Function to find the longest prefix which is palindromic ; Find the length of the given string ; For storing the length of longest prefix palindrome ; Loop to check the substring of all length from 1 to N which is palindrome ; String of length i ; To store the reversed of temp ; Reversing string temp2 ; If string temp is palindromic then update the length ; Print the palindromic string of max_len ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LongestPalindromicPrefix ( string s ) { int n = s . length ( ) ; int max_len = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { string temp = s . substr ( 0 , len ) ; string temp2 = temp ; reverse ( temp2 . begin ( ) , temp2 . end ( ) ) ; if ( temp == temp2 ) { max_len = len ; } } cout << s . substr ( 0 , max_len ) ; } int main ( ) { string str = " abaab " ; LongestPalindromicPrefix ( str ) ; }
K | C ++ program to find the K - th lexicographical string of length N ; Initialize the array to store the base 26 representation of K with all zeroes , that is , the initial String consists of N a 's ; Start filling all the N slots for the base 26 representation of K ; Store the remainder ; Reduce K ; If K is greater than the possible number of strings that can be represented by a string of length N ; Store the Kth lexicographical string ; Driver Code ; Reducing k value by 1 because our stored value starts from 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; string find_kth_String_of_n ( int n , int k ) { int d [ n ] = { 0 } ; for ( int i = n - 1 ; i > -1 ; i -- ) { d [ i ] = k % 26 ; k /= 26 ; } if ( k > 0 ) return " - 1" ; string s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( d [ i ] + ( ' a ' ) ) ; return s ; } int main ( ) { int n = 3 ; int k = 10 ; k -= 1 ; cout << find_kth_String_of_n ( n , k ) ; return 0 ; }
Length of the smallest substring which contains all vowels | C ++ Program to find the length of the smallest substring of which contains all vowels ; Map to store the frequency of vowels ; Store the indices which contains the vowels ; If all vowels are not present in the string ; If the frequency of the vowel at i - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the left ; Otherwise set flag1 ; If the frequency of the vowel at j - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the right ; Otherwise set flag2 ; If both flag1 and flag2 are set , break out of the loop as the substring length cannot be minimized ; Return the length of the substring ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinLength ( string s ) { int n = s . size ( ) ; map < char , int > counts ; vector < int > indices ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' o ' s [ i ] == ' i ' s [ i ] == ' u ' ) { counts [ s [ i ] ] ++ ; indices . push_back ( i ) ; } } if ( counts . size ( ) < 5 ) return -1 ; int flag1 = 0 , flag2 = 0 ; int i = 0 ; int j = indices . size ( ) - 1 ; while ( ( j - i ) >= 4 ) { if ( ! flag1 && counts [ s [ indices [ i ] ] ] > 1 ) { counts [ s [ indices [ i ] ] ] -- ; i ++ ; } else flag1 = 1 ; if ( ! flag2 && counts [ s [ indices [ j ] ] ] > 1 ) { counts [ s [ indices [ j ] ] ] -- ; j -- ; } else flag2 = 1 ; if ( flag1 && flag2 ) break ; } return ( indices [ j ] - indices [ i ] + 1 ) ; } int main ( ) { string s = " aaeebbeaccaaoiuooooooooiuu " ; cout << findMinLength ( s ) ; return 0 ; }
Longest Subsequence of a String containing only Consonants | C ++ program to find the longest subsequence which contain all consonants ; Returns true if x is consonants . ; Function to check whether a character is consonants or not ; Function to find the longest subsequence which contain all consonants ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isConsonants ( char x ) { x = tolower ( x ) ; return ! ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } string longestConsonantsSubsequence ( string str ) { string answer = " " ; int n = str . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isConsonants ( str [ i ] ) ) { answer += str [ i ] ; } } return answer ; } int main ( ) { string str = " geeksforgeeks " ; cout << longestConsonantsSubsequence ( str ) << endl ; return 0 ; }
Find the length of the longest subsequence with first K alphabets having same frequency | C ++ program to find the longest subsequence with first K alphabets having same frequency ; Function to return the length of the longest subsequence with first K alphabets having same frequency ; Map to store frequency of all characters in the string ; Variable to store the frequency of the least frequent of first K alphabets ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lengthOfSubsequence ( string str , int K ) { map < char , int > mp ; for ( char ch : str ) { mp [ ch ] ++ ; } int minimum = mp [ ' A ' ] ; for ( int i = 1 ; i < K ; i ++ ) { minimum = min ( minimum , mp [ ( char ) ( i + ' A ' ) ] ) ; } return minimum * K ; } int main ( ) { string str = " ACAABCCAB " ; int K = 3 ; cout << lengthOfSubsequence ( str , K ) ; return 0 ; }
Detecting negative cycle using Floyd Warshall | C ++ Program to check if there is a negative weight cycle using Floyd Warshall Algorithm ; Number of vertices in the graph ;  ; Define Infinite as a large enough value . This value will be used for vertices not connected to each other ; Returns true if graph has negative weight cycle else false . ; dist [ ] [ ] will be the output matrix that will finally have the shortest distances between every pair of vertices ; Initialize the solution matrix same as input graph matrix . Or we can say the initial values of shortest distances are based on shortest paths considering no intermediate vertex . ; Add all vertices one by one to the set of intermediate vertices . -- -> Before start of a iteration , we have shortest distances between all pairs of vertices such that the shortest distances consider only the vertices in set { 0 , 1 , 2 , . . k - 1 } as intermediate vertices . -- -- > After the end of a iteration , vertex no . k is added to the set of intermediate vertices and the set becomes { 0 , 1 , 2 , . . k } ; Pick all vertices as source one by one ; Pick all vertices as destination for the above picked source ; If vertex k is on the shortest path from i to j , then update the value of dist [ i ] [ j ] ; If distance of any verex from itself becomes negative , then there is a negative weight cycle . ; driver program ; Let us create the following weighted graph 1 ( 0 ) -- -- -- -- -- -> ( 1 ) / | \ | | | - 1 | | - 1 | \ | / ( 3 ) < -- -- -- -- -- - ( 2 ) - 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define V  4 NEW_LINE void printSolution ( int dist [ ] [ V ] ) ; #define INF  99999 NEW_LINE bool negCyclefloydWarshall ( int graph [ ] [ V ] ) { int dist [ V ] [ V ] , i , j , k ; for ( i = 0 ; i < V ; i ++ ) for ( j = 0 ; j < V ; j ++ ) dist [ i ] [ j ] = graph [ i ] [ j ] ; for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } for ( int i = 0 ; i < V ; i ++ ) if ( dist [ i ] [ i ] < 0 ) return true ; return false ; } int main ( ) { int graph [ V ] [ V ] = { { 0 , 1 , INF , INF } , { INF , 0 , -1 , INF } , { INF , INF , 0 , -1 } , { -1 , INF , INF , 0 } } ; if ( negCyclefloydWarshall ( graph ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Digital Root of a given large integer using Recursion | C ++ program to print the digital root of a given very large number ; Function to convert given sum into string ; Loop to extract digit one by one from the given sum and concatenate into the string ; Type casting for concatenation ; Return converted string ; Function to get individual digit sum from string ; Loop to get individual digit sum ; Function call to convert sum into string ; Function to calculate the digital root of a very large number ; Base condition ; Function call to get individual digit sum ; Recursive function to get digital root of a very large number ; Driver code ; Function to print final digit
#include <iostream> NEW_LINE using namespace std ; string convertToString ( int sum ) { string str = " " ; while ( sum ) { str = str + ( char ) ( ( sum % 10 ) + '0' ) ; sum = sum / 10 ; } return str ; } string GetIndividulaDigitSum ( string str , int len ) { int sum = 0 ; for ( int i = 0 ; i < len ; i ++ ) { sum = sum + str [ i ] - '0' ; } return convertToString ( sum ) ; } int GetDigitalRoot ( string str ) { if ( str . length ( ) == 1 ) { return str [ 0 ] - '0' ; } str = GetIndividulaDigitSum ( str , str . length ( ) ) ; return GetDigitalRoot ( str ) ; } int main ( ) { string str = "675987890789756545689070986776987" ; cout << GetDigitalRoot ( str ) ; }
Count the Number of matching characters in a pair of strings | C ++ code to count number of matching characters in a pair of strings ; Function to count the matching characters ; Traverse the string 1 char by char ; This will check if str1 [ i ] is present in str2 or not str2 . find ( str1 [ i ] ) returns - 1 if not found otherwise it returns the starting occurrence index of that character in str2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count ( string str1 , string str2 ) { int c = 0 , j = 0 ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str2 . find ( str1 [ i ] ) >= 0 and j == str1 . find ( str1 [ i ] ) ) c += 1 ; j += 1 ; } cout << " No . ▁ of ▁ matching ▁ characters ▁ are : ▁ " << c / 2 ; } int main ( ) { string str1 = " aabcddekll12 @ " ; string str2 = " bb2211@55k " ; count ( str1 , str2 ) ; }
Check if count of Alphabets and count of Numbers are equal in the given String | C ++ program to check if the count of alphabets and numbers in a string are equal or not . ; Function to count the number of alphabets ; Counter to store the number of alphabets in the string ; Every character in the string is iterated ; To check if the character is an alphabet or not ; Function to count the number of numbers ; Counter to store the number of alphabets in the string ; Every character in the string is iterated ; To check if the character is a digit or not ; Function to check if the count of alphabets is equal to the count of numbers or not ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countOfLetters ( string str ) { int letter = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) letter ++ ; } return letter ; } int countOfNumbers ( string str ) { int number = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= '0' && str [ i ] <= '9' ) number ++ ; } return number ; } void check ( string str ) { if ( countOfLetters ( str ) == countOfNumbers ( str ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } int main ( ) { string str = " GeeKs01324" ; check ( str ) ; return 0 ; }
Minimum cost to traverse from one index to another in the String | C ++ implementation of the above approach . ; function to find the minimum cost ; graph ; adjacency matrix ; initialising adjacency matrix ; creating adjacency list ; pushing left adjacent elelemt for index ' k ' ; pushing right adjacent element for index ' k ' ; queue to perform BFS ; visited array ; variable to store depth of BFS ; BFS ; number of elements in the current level ; inner loop ; current element ; popping queue ; base case ; checking if the current node is required node ; iterating through the current node ; updating depth ; Driver Code ; input variables ; function to find the minimum cost
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinCost ( string s , int i , int j ) { vector < vector < int > > gr ( 26 ) ; bool edge [ 26 ] [ 26 ] ; for ( int k = 0 ; k < 26 ; k ++ ) for ( int l = 0 ; l < 26 ; l ++ ) edge [ k ] [ l ] = 0 ; for ( int k = 0 ; k < s . size ( ) ; k ++ ) { if ( k - 1 >= 0 and ! edge [ s [ k ] - 97 ] [ s [ k - 1 ] - 97 ] ) gr [ s [ k ] - 97 ] . push_back ( s [ k - 1 ] - 97 ) , edge [ s [ k ] - 97 ] [ s [ k - 1 ] - 97 ] = 1 ; if ( k + 1 <= s . size ( ) - 1 and ! edge [ s [ k ] - 97 ] [ s [ k + 1 ] - 97 ] ) gr [ s [ k ] - 97 ] . push_back ( s [ k + 1 ] - 97 ) , edge [ s [ k ] - 97 ] [ s [ k + 1 ] - 97 ] = 1 ; } queue < int > q ; q . push ( s [ i ] - 97 ) ; bool v [ 26 ] = { 0 } ; int d = 0 ; while ( q . size ( ) ) { int cnt = q . size ( ) ; while ( cnt -- ) { int curr = q . front ( ) ; q . pop ( ) ; if ( v [ curr ] ) continue ; v [ curr ] = 1 ; if ( curr == s [ j ] - 97 ) return d ; for ( auto it : gr [ curr ] ) q . push ( it ) ; } d ++ ; } return -1 ; } int main ( ) { string s = " abcde " ; int i = 0 ; int j = 4 ; cout << findMinCost ( s , i , j ) ; }