codeparrot/xlcost-text-to-code huggingface dataset:
Check if a string is Isogram or not | CPP code to check string is isogram or not ; function to check isogram ; loop to store count of chars and check if it is greater than 1 ; if count > 1 , return false ; Driver code ; checking str as isogram ; checking str2 as isogram
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check_isogram ( string str ) { int length = str . length ( ) ; int mapHash [ 26 ] = { 0 } ; for ( int i = 0 ; i < length ; i ++ ) { mapHash [ str [ i ] - ' a ' ] ++ ; if ( mapHash [ str [ i ] - ' a ' ] > 1 ) { return false ; } } return true ; } int main ( ) { string str = " geeks " ; string str2 = " computer " ; if ( check_isogram ( str ) ) { cout << " True " << endl ; } else { cout << " False " << endl ; } if ( check_isogram ( str2 ) ) { cout << " True " << endl ; } else { cout << " False " << endl ; } return 0 ; }
Check if a binary string has a 0 between 1 s or not | Set 1 ( General approach ) | C ++ program to check if a string is valid or not . ; Function returns 1 when string is valid else returns 0 ; Find first occurrence of 1 in s [ ] ; Find last occurrence of 1 in s [ ] ; Check if there is any 0 in range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkString ( string s ) { int len = s . length ( ) ; int first = s . size ( ) + 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s [ i ] == '1' ) { first = i ; break ; } } int last = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { last = i ; break ; } } for ( int i = first ; i <= last ; i ++ ) if ( s [ i ] == '0' ) return false ; return true ; } int main ( ) { string s = "00011111111100000" ; checkString ( s ) ? cout << " VALID STRNEWLINE " : cout << " NOT ▁ VALID STRNEWLINE " ; return 0 ; }
Program to print all substrings of a given string | * C ++ program to print all possible * substrings of a given string * without checking for duplication . ; * Function to print all ( n * ( n + 1 ) ) / 2 * substrings of a given string s of length n . ; * Fix start index in outer loop . * Reveal new character in inner loop till end of string . * Print till - now - formed string . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAllSubstrings ( string s , int n ) { for ( int i = 0 ; i < n ; i ++ ) { char temp [ n - i + 1 ] ; int tempindex = 0 ; for ( int j = i ; j < n ; j ++ ) { temp [ tempindex ++ ] = s [ j ] ; temp [ tempindex ] = ' \0' ; printf ( " % s STRNEWLINE " , temp ) ; } } } int main ( ) { string s = " Geeky " ; printAllSubstrings ( s , s . length ( ) ) ; return 0 ; }
Reverse a string preserving space positions | C ++ program to implement the above approach ; Initialize two pointers as two corners ; Move both pointers toward each other ; If character at start or end is space , ignore it ; If both are not spaces , do swap ; Driver code
#include <iostream> NEW_LINE using namespace std ; void preserveSpace ( string & str ) { int n = str . length ( ) ; int start = 0 ; int end = n - 1 ; while ( start < end ) { if ( str [ start ] == ' ▁ ' ) { start ++ ; continue ; } else if ( str [ end ] == ' ▁ ' ) { end -- ; continue ; } else { swap ( str [ start ] , str [ end ] ) ; start ++ ; end -- ; } } } int main ( ) { string str = " internship ▁ at ▁ geeks ▁ for ▁ geeks " ; preserveSpace ( str ) ; cout << str ; return 0 ; }
Put spaces between words starting with capital letters | C ++ program to put spaces between words starting with capital letters . ; Function to amend the sentence ; Traverse the string ; Convert to lowercase if its an uppercase character ; Print space before it if its an uppercase character ; Print the character ; if lowercase character then just print ; Driver code
#include <iostream> NEW_LINE using namespace std ; void amendSentence ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { str [ i ] = str [ i ] + 32 ; if ( i != 0 ) cout << " ▁ " ; cout << str [ i ] ; } else cout << str [ i ] ; } } int main ( ) { string str = " BruceWayneIsBatman " ; amendSentence ( str ) ; return 0 ; }
C ++ program to concatenate a string given number of times | C ++ program to concatenate given string n number of times ; Function which return string by concatenating it . ; Copying given string to temporary string . ; s += s1 ; Concatenating strings ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <string> NEW_LINE using namespace std ; string repeat ( string s , int n ) { string s1 = s ; for ( int i = 1 ; i < n ; i ++ ) return s ; } int main ( ) { string s = " geeks " ; int n = 3 ; cout << repeat ( s , n ) << endl ; ; return 0 ; }
Number of distinct permutation a String can have | C ++ program to find number of distinct permutations of a string . ; Utility function to find factorial of n . ; Returns count of distinct permutations of str . ; finding frequency of all the lower case alphabet and storing them in array of integer ; finding factorial of number of appearances and multiplying them since they are repeating alphabets ; finding factorial of size of string and dividing it by factorial found after multiplying ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } int countDistinctPermutations ( string str ) { int length = str . length ( ) ; int freq [ MAX_CHAR ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < length ; i ++ ) if ( str [ i ] >= ' a ' ) freq [ str [ i ] - ' a ' ] ++ ; int fact = 1 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) fact = fact * factorial ( freq [ i ] ) ; return factorial ( length ) / fact ; } int main ( ) { string str = " fvvfhvgv " ; printf ( " % d " , countDistinctPermutations ( str ) ) ; return 0 ; }
Determine if a string has all Unique Characters | C ++ program to illustrate string with unique characters using brute force technique ; Assuming string can have characters a - z , this has 32 bits set to 0 ; if that bit is already set in checker , return false ; otherwise update and continue by setting that bit in the checker ; no duplicates encountered , return true ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool uniqueCharacters ( string str ) { int checker = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int bitAtIndex = str [ i ] - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } return true ; } int main ( ) { string str = " geeksforgeeks " ; if ( uniqueCharacters ( str ) ) { cout << " The ▁ String ▁ " << str << " ▁ has ▁ all ▁ unique ▁ characters STRNEWLINE " ; } else { cout << " The ▁ String ▁ " << str << " ▁ has ▁ duplicate ▁ characters STRNEWLINE " ; } return 0 ; }
Alternate vowel and consonant string | C ++ implementation of alternate vowel and consonant string ; ' ch ' is vowel or not ; create alternate vowel and consonant string str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required alternate vowel and consonant string ; count vowels and update vowel string ; count consonants and update consonant string ; no such string can be formed ; remove first character of vowel string then create alternate string with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant string then create alternate string with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating string with consonant ; start creating string with vowel ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; return false ; } string createAltStr ( string str1 , string str2 , int start , int l ) { string finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) finalStr = ( finalStr + str1 . at ( i ) ) + str2 . at ( j ) ; return finalStr ; } string findAltStr ( string str ) { int nv = 0 , nc = 0 ; string vstr = " " , cstr = " " ; int l = str . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str . at ( i ) ; if ( isVowel ( ch ) ) { nv ++ ; vstr = vstr + ch ; } else { nc ++ ; cstr = cstr + ch ; } } if ( abs ( nv - nc ) >= 2 ) return " no ▁ such ▁ string " ; if ( nv > nc ) return ( vstr . at ( 0 ) + createAltStr ( cstr , vstr , 1 , nv ) ) ; if ( nc > nv ) return ( cstr . at ( 0 ) + createAltStr ( vstr , cstr , 1 , nc ) ) ; if ( cstr . at ( 0 ) < vstr . at ( 0 ) ) return createAltStr ( cstr , vstr , 0 , nv ) ; return createAltStr ( vstr , cstr , 0 , nc ) ; } int main ( ) { string str = " geeks " ; cout << findAltStr ( str ) ; return 0 ; }
Check whether K | CPP program to check if k - th bit of a given number is set or not ; Driver code
#include <iostream> NEW_LINE using namespace std ; void isKthBitSet ( int n , int k ) { if ( n & ( 1 << ( k - 1 ) ) ) cout << " SET " ; else cout << " NOT ▁ SET " ; } int main ( ) { int n = 5 , k = 1 ; isKthBitSet ( n , k ) ; return 0 ; }
Reverse string without using any temporary variable | Reversing a string using reverse ( ) ; Reverse str [ beign . . end ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { string str = " geeksforgeeks " ; reverse ( str . begin ( ) , str . end ( ) ) ; cout << str ; return 0 ; }
Recursive function to check if a string is palindrome | A recursive C ++ program to check whether a given number is palindrome or not ; A recursive function that check a str [ s . . e ] is palindrome or not . ; If there is only one character ; If first and last characters do not match ; If there are more than two characters , check if middle substring is also palindrome or not . ; An empty string is considered as palindrome ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalRec ( char str [ ] , int s , int e ) { if ( s == e ) return true ; if ( str [ s ] != str [ e ] ) return false ; if ( s < e + 1 ) return isPalRec ( str , s + 1 , e - 1 ) ; return true ; } bool isPalindrome ( char str [ ] ) { int n = strlen ( str ) ; if ( n == 0 ) return true ; return isPalRec ( str , 0 , n - 1 ) ; } int main ( ) { char str [ ] = " geeg " ; if ( isPalindrome ( str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count substrings with same first and last characters | Most efficient C ++ program to count all substrings with same first and last characters . ; assuming lower case only ; Calculating frequency of each character in the string . ; Computing result using counts ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; int count [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ s [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; return result ; } int main ( ) { string s ( " abcab " ) ; cout << countSubstringWithEqualEnds ( s ) ; return 0 ; }
Maximum consecutive repeating character in string | C ++ program to find the maximum consecutive repeating character in given string ; Returns the maximum repeating character in a given string ; Traverse string except last character ; If current character matches with next ; If doesn 't match, update result  (if required) and reset count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char maxRepeating ( string str ) { int n = str . length ( ) ; int count = 0 ; char res = str [ 0 ] ; int cur_count = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 1 && str [ i ] == str [ i + 1 ] ) cur_count ++ ; else { if ( cur_count > count ) { count = cur_count ; res = str [ i ] ; } cur_count = 1 ; } } return res ; } int main ( ) { string str = " aaaabbaaccde " ; cout << maxRepeating ( str ) ; return 0 ; }
Queries on subsequence of string | C ++ program to answer subsequence queries for a given string . ; Precompute the position of each character from each position of String S ; Computing position of each character from each position of String S ; Print " Yes " if T is subsequence of S , else " No " ; Traversing the string T ; If next position is greater than length of S set flag to false . ; Setting position of next character ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  10000 NEW_LINE #define CHAR_SIZE  26 NEW_LINE using namespace std ; void precompute ( int mat [ MAX ] [ CHAR_SIZE ] , char str [ ] , int len ) { for ( int i = 0 ; i < CHAR_SIZE ; ++ i ) mat [ len ] [ i ] = len ; for ( int i = len - 1 ; i >= 0 ; -- i ) { for ( int j = 0 ; j < CHAR_SIZE ; ++ j ) mat [ i ] [ j ] = mat [ i + 1 ] [ j ] ; mat [ i ] [ str [ i ] - ' a ' ] = i ; } } bool query ( int mat [ MAX ] [ CHAR_SIZE ] , const char * str , int len ) { int pos = 0 ; for ( int i = 0 ; i < strlen ( str ) ; ++ i ) { if ( mat [ pos ] [ str [ i ] - ' a ' ] >= len ) return false ; else pos = mat [ pos ] [ str [ i ] - ' a ' ] + 1 ; } return true ; } int main ( ) { char S [ ] = " geeksforgeeks " ; int len = strlen ( S ) ; int mat [ MAX ] [ CHAR_SIZE ] ; precompute ( mat , S , len ) ; query ( mat , " gg " , len ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; query ( mat , " gro " , len ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; query ( mat , " gfg " , len ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; query ( mat , " orf " , len ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Queries for characters in a repeated string | Queries for same characters in a repeated string ; Print whether index i and j have same element or not . ; Finding relative position of index i , j . ; Checking is element are same at index i , j . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void query ( char s [ ] , int i , int j ) { int n = strlen ( s ) ; i %= n ; j %= n ; ( s [ i ] == s [ j ] ) ? ( cout << " Yes " << endl ) : ( cout << " No " << endl ) ; } int main ( ) { char X [ ] = " geeksforgeeks " ; query ( X , 0 , 8 ) ; query ( X , 8 , 13 ) ; query ( X , 6 , 15 ) ; return 0 ; }
Count of character pairs at same distance as in English alphabets | A Simple C ++ program to find pairs with distance equal to English alphabet distance ; Function to count pairs ; Increment count if characters are at same distance ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( string str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( str [ i ] - str [ j ] ) == abs ( i - j ) ) result ++ ; return result ; } int main ( ) { string str = " geeksforgeeks " ; cout << countPairs ( str ) ; return 0 ; }
Longest common subsequence with permutations allowed | C ++ program to find LCS with permutations allowed ; Function to calculate longest string str1 -- > first string str2 -- > second string count1 [ ] -- > hash array to calculate frequency of characters in str1 count [ 2 ] -- > hash array to calculate frequency of characters in str2 result -- > resultant longest string whose permutations are sub - sequence of given two strings ; calculate frequency of characters ; Now traverse hash array ; append character ( ' a ' + i ) in resultant string ' result ' by min ( count1 [ i ] , count2i ] ) times ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestString ( string str1 , string str2 ) { int count1 [ 26 ] = { 0 } , count2 [ 26 ] = { 0 } ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) count1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) count2 [ str2 [ i ] - ' a ' ] ++ ; string result ; for ( int i = 0 ; i < 26 ; i ++ ) for ( int j = 1 ; j <= min ( count1 [ i ] , count2 [ i ] ) ; j ++ ) result . push_back ( ' a ' + i ) ; cout << result ; } int main ( ) { string str1 = " geeks " , str2 = " cake " ; longestString ( str1 , str2 ) ; return 0 ; }
Check if string follows order of characters defined by a pattern or not | Set 1 | C ++ program check if characters in the input string follows the same order as determined by characters present in the given pattern ; Function to check if characters in the input string follows the same order as determined by characters present in the given pattern ; len stores length of the given pattern ; if length of pattern is more than length of input string , return false ; ; x , y are two adjacent characters in pattern ; find index of last occurrence of character x in the input string ; find index of first occurrence of character y in the input string ; return false if x or y are not present in the input string OR last occurrence of x is after the first occurrence of y in the input string ; return true if string matches the pattern ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool checkPattern ( string str , string pattern ) { int len = pattern . length ( ) ; if ( str . length ( ) < len ) return false ; for ( int i = 0 ; i < len - 1 ; i ++ ) { char x = pattern [ i ] ; char y = pattern [ i + 1 ] ; size_t last = str . find_last_of ( x ) ; size_t first = str . find_first_of ( y ) ; if ( last == string :: npos first == string :: npos last > first ) return false ; } return true ; } int main ( ) { string str = " engineers ▁ rock " ; string pattern = " gsr " ; cout << boolalpha << checkPattern ( str , pattern ) ; return 0 ; }
Calculate sum of all numbers present in a string | C ++ program to calculate sum of all numbers present in a string containing alphanumeric characters ; Function to calculate sum of all numbers present in a string containing alphanumeric characters ; A temporary string ; holds sum of all numbers present in the string ; read each character in input string ; if current character is a digit ; if current character is an alphabet ; increment sum by number found earlier ( if any ) ; reset temporary string to empty ; atoi ( temp . c_str ( ) ) takes care of trailing numbers ; Driver code ; input alphanumeric string ; Function call
#include <iostream> NEW_LINE using namespace std ; int findSum ( string str ) { string temp = " " ; int sum = 0 ; for ( char ch : str ) { if ( isdigit ( ch ) ) temp += ch ; else { sum += atoi ( temp . c_str ( ) ) ; temp = " " ; } } return sum + atoi ( temp . c_str ( ) ) ; } int main ( ) { string str = "12abc20yz68" ; cout << findSum ( str ) ; return 0 ; }
Count number of substrings with exactly k distinct characters | C ++ program to count number of substrings with exactly k distinct characters in a given string ; Function to count number of substrings with exactly k unique characters ; Initialize result ; To store count of characters from ' a ' to ' z ' ; Consider all substrings beginning with str [ i ] ; Initializing array with 0 ; Consider all substrings between str [ i . . j ] ; If this is a new character for this substring , increment dist_count . ; Increment count of current character ; If distinct character count becomes k , then increment result . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countkDist ( string str , int k ) { int n = str . length ( ) ; int res = 0 ; int cnt [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int dist_count = 0 ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int j = i ; j < n ; j ++ ) { if ( cnt [ str [ j ] - ' a ' ] == 0 ) dist_count ++ ; cnt [ str [ j ] - ' a ' ] ++ ; if ( dist_count == k ) res ++ ; if ( dist_count > k ) break ; } } return res ; } int main ( ) { string str = " abcbaa " ; int k = 3 ; cout << " Total ▁ substrings ▁ with ▁ exactly ▁ " << k << " ▁ distinct ▁ characters ▁ : " << countkDist ( str , k ) << endl ; return 0 ; }
Lower case to upper case | C ++ program to convert a string to uppercase ; Converts a string to uppercase ; Driver code
#include <iostream> NEW_LINE using namespace std ; string to_upper ( string & in ) { for ( int i = 0 ; i < in . length ( ) ; i ++ ) if ( ' a ' <= in [ i ] <= ' z ' ) in [ i ] = in [ i ] - ' a ' + ' A ' ; return in ; } int main ( ) { string str = " geeksforgeeks " ; cout << to_upper ( str ) ; return 0 ; }
Longest Common Prefix using Character by Character Matching | A C ++ Program to find the longest common prefix ; A Function to find the string having the minimum length and returns that length ; A Function that returns the longest common prefix from the array of strings ; Our resultant string char current ; The current character ; Current character ( must be same in all strings to be a part of result ) ; Append to result ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinLength ( string arr [ ] , int n ) { int min = arr [ 0 ] . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] . length ( ) < min ) min = arr [ i ] . length ( ) ; return ( min ) ; } string commonPrefix ( string arr [ ] , int n ) { int minlen = findMinLength ( arr , n ) ; string result ; for ( int i = 0 ; i < minlen ; i ++ ) { current = arr [ 0 ] [ i ] ; for ( int j = 1 ; j < n ; j ++ ) if ( arr [ j ] [ i ] != current ) return result ; result . push_back ( current ) ; } return ( result ) ; } int main ( ) { string arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) ) cout << " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " << ans ; else cout << " There ▁ is ▁ no ▁ common ▁ prefix " ; return ( 0 ) ; }
Print Concatenation of Zig | C ++ program to print string obtained by concatenation of different rows of Zig - Zag fashion ; Prints concatenation of all rows of str 's Zig-Zag fashion ; Corner Case ( Only one row ) ; Find length of string ; Create an array of strings for all n rows ; Initialize index for array of strings arr [ ] ; True if we are moving down in rows , else false ; Traverse through given string ; append current character to current row ; If last row is reached , change direction to ' up ' ; If 1 st row is reached , change direction to ' down ' ; If direction is down , increment , else decrement ; Print concatenation of all rows ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printZigZagConcat ( string str , int n ) { if ( n == 1 ) { cout << str ; return ; } int len = str . length ( ) ; string arr [ n ] ; int row = 0 ; bool down ; for ( int i = 0 ; i < len ; ++ i ) { arr [ row ] . push_back ( str [ i ] ) ; if ( row == n - 1 ) down = false ; else if ( row == 0 ) down = true ; ( down ) ? ( row ++ ) : ( row -- ) ; } for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] ; } int main ( ) { string str = " GEEKSFORGEEKS " ; int n = 3 ; printZigZagConcat ( str , n ) ; return 0 ; }
Check if two given strings are isomorphic to each other | C ++ program to check if two strings are isomorphic ; This function returns true if str1 and str2 are isomorphic ; Length of both strings must be same for one to one corresponance ; To mark visited characters in str2 ; To store mapping of every character from str1 to that of str2 . Initialize all entries of map as - 1. ; Process all characters one by on ; If current character of str1 is seen first time in it . ; If current character of str2 is already seen , one to one mapping not possible ; Mark current character of str2 as visited ; Store mapping of current characters ; If this is not first appearance of current character in str1 , then check if previous appearance mapped to same character of str2 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_CHARS  256 NEW_LINE bool areIsomorphic ( string str1 , string str2 ) { int m = str1 . length ( ) , n = str2 . length ( ) ; if ( m != n ) return false ; bool marked [ MAX_CHARS ] = { false } ; int map [ MAX_CHARS ] ; memset ( map , -1 , sizeof ( map ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map [ str1 [ i ] ] == -1 ) { if ( marked [ str2 [ i ] ] == true ) return false ; marked [ str2 [ i ] ] = true ; map [ str1 [ i ] ] = str2 [ i ] ; } else if ( map [ str1 [ i ] ] != str2 [ i ] ) return false ; } return true ; } int main ( ) { cout << areIsomorphic ( " aab " , " xxy " ) << endl ; cout << areIsomorphic ( " aab " , " xyz " ) << endl ; return 0 ; }