Given two string **s1** and **s2**, the task is to check if characters of the first string can be mapped with the character of the second string such that if a character **ch1** is mapped with some character **ch2** then all the occurrences of **ch1** will only be mapped with **ch2** for both the strings.

**Examples:**

Input:s1 = “axx”, s2 = “cbc”Output:Yes

‘a’ in s1 can be mapped to ‘b’ in s2

and ‘x’ in s1 can be mapped to ‘c’ in s2.

Input:s1 = “a”, s2 = “df”Output:No

**Approach:** If the lengths of both the strings are unequal then the strings cannot be mapped else create two frequency arrays **freq1[]** and **freq2[]** which will store the frequencies of all the characters of the given strings **s1** and **s2** respectively. Now, for every non-zero value in **freq1[]** find an equal value in **freq2[]**. If all the non-zero values from **freq1[]** can be mapped to some value in **freq2[]** then the answer is possible else not.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `#define MAX 26 ` ` ` `// Function that returns true if the mapping is possible ` `bool` `canBeMapped(string s1, ` `int` `l1, string s2, ` `int` `l2) `
`{ ` ` ` ` ` `// Both the strings are of un-equal lengths `
` ` `if` `(l1 != l2) `
` ` `return` `false` `; `
` ` ` ` `// To store the frequencies of the `
` ` `// characters in both the string `
` ` `int` `freq1[MAX] = { 0 }; `
` ` `int` `freq2[MAX] = { 0 }; `
` ` ` ` `// Update frequencies of the characters `
` ` `for` `(` `int` `i = 0; i < l1; i++) `
` ` `freq1[s1[i] - ` `'a'` `]++; `
` ` `for` `(` `int` `i = 0; i < l2; i++) `
` ` `freq2[s2[i] - ` `'a'` `]++; `
` ` ` ` `// For every character of s1 `
` ` `for` `(` `int` `i = 0; i < MAX; i++) { `
` ` ` ` `// If current character is `
` ` `// not present in s1 `
` ` `if` `(freq1[i] == 0) `
` ` `continue` `; `
` ` `bool` `found = ` `false` `; `
` ` ` ` `// Find a character in s2 that has frequency `
` ` `// equal to the current character's `
` ` `// frequency in s1 `
` ` `for` `(` `int` `j = 0; j < MAX; j++) { `
` ` ` ` `// If such character is found `
` ` `if` `(freq1[i] == freq2[j]) { `
` ` ` ` `// Set the frequnecy to -1 so that `
` ` `// it doesn't get picked again `
` ` `freq2[j] = -1; `
` ` ` ` `// Set found to true `
` ` `found = ` `true` `; `
` ` `break` `; `
` ` `} `
` ` `} `
` ` ` ` `// If there is no character in s2 `
` ` `// that could be mapped to the `
` ` `// current character in s1 `
` ` `if` `(!found) `
` ` `return` `false` `; `
` ` `} `
` ` ` ` `return` `true` `; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `string s1 = ` `"axx"` `; `
` ` `string s2 = ` `"cbc"` `; `
` ` `int` `l1 = s1.length(); `
` ` `int` `l2 = s2.length(); `
` ` ` ` `if` `(canBeMapped(s1, l1, s2, l2)) `
` ` `cout << ` `"Yes"` `; `
` ` `else`
` ` `cout << ` `"No"` `; `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java implementation of the approach ` `class` `GFG `
`{ ` ` ` ` ` `static` `int` `MAX = ` `26` `; `
` ` ` ` `// Function that returns true if the mapping is possible `
` ` `public` `static` `boolean` `canBeMapped(String s1, ` `int` `l1, `
` ` `String s2, ` `int` `l2) `
` ` `{ `
` ` ` ` `// Both the strings are of un-equal lengths `
` ` `if` `(l1 != l2) `
` ` `return` `false` `; `
` ` ` ` `// To store the frequencies of the `
` ` `// characters in both the string `
` ` `int` `[] freq1 = ` `new` `int` `[MAX]; `
` ` `int` `[] freq2 = ` `new` `int` `[MAX]; `
` ` ` ` `// Update frequencies of the characters `
` ` `for` `(` `int` `i = ` `0` `; i < l1; i++) `
` ` `freq1[s1.charAt(i) - ` `'a'` `]++; `
` ` `for` `(` `int` `i = ` `0` `; i < l2; i++) `
` ` `freq2[s2.charAt(i) - ` `'a'` `]++; `
` ` ` ` `// For every character of s1 `
` ` `for` `(` `int` `i = ` `0` `; i < MAX; i++) { `
` ` ` ` `// If current character is `
` ` `// not present in s1 `
` ` `if` `(freq1[i] == ` `0` `) `
` ` `continue` `; `
` ` `boolean` `found = ` `false` `; `
` ` ` ` `// Find a character in s2 that has frequency `
` ` `// equal to the current character's `
` ` `// frequency in s1 `
` ` `for` `(` `int` `j = ` `0` `; j < MAX; j++) `
` ` `{ `
` ` ` ` `// If such character is found `
` ` `if` `(freq1[i] == freq2[j]) `
` ` `{ `
` ` ` ` `// Set the frequnecy to -1 so that `
` ` `// it doesn't get picked again `
` ` `freq2[j] = -` `1` `; `
` ` ` ` `// Set found to true `
` ` `found = ` `true` `; `
` ` `break` `; `
` ` `} `
` ` `} `
` ` ` ` `// If there is no character in s2 `
` ` `// that could be mapped to the `
` ` `// current character in s1 `
` ` `if` `(!found) `
` ` `return` `false` `; `
` ` `} `
` ` ` ` `return` `true` `; `
` ` `} `
` ` ` ` `// Driver code `
` ` `public` `static` `void` `main(String[] args) `
` ` `{ `
` ` `String s1 = ` `"axx"` `; `
` ` `String s2 = ` `"cbc"` `; `
` ` `int` `l1 = s1.length(); `
` ` `int` `l2 = s2.length(); `
` ` ` ` `if` `(canBeMapped(s1, l1, s2, l2)) `
` ` `System.out.println(` `"Yes"` `); `
` ` `else`
` ` `System.out.println(` `"No"` `); `
` ` ` ` `} `
`} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

`# Python 3 implementation of the approach ` ` ` `MAX` `=` `26`
` ` `# Function that returns true if the mapping is possible ` `def` `canBeMapped(s1, l1, s2, l2): `
` ` `# Both the strings are of un-equal lengths `
` ` `if` `(l1 !` `=` `l2): `
` ` `return` `False`
` ` ` ` `# To store the frequencies of the `
` ` `# characters in both the string `
` ` `freq1 ` `=` `[` `0` `for` `i ` `in` `range` `(` `MAX` `)] `
` ` `freq2 ` `=` `[` `0` `for` `i ` `in` `range` `(` `MAX` `)] `
` ` ` ` `# Update frequencies of the characters `
` ` `for` `i ` `in` `range` `(l1): `
` ` `freq1[` `ord` `(s1[i]) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1`
` ` `for` `i ` `in` `range` `(l2): `
` ` `freq2[` `ord` `(s2[i]) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1`
` ` ` ` `# For every character of s1 `
` ` `for` `i ` `in` `range` `(` `MAX` `): `
` ` `# If current character is `
` ` `# not present in s1 `
` ` `if` `(freq1[i] ` `=` `=` `0` `): `
` ` `continue`
` ` `found ` `=` `False`
` ` ` ` `# Find a character in s2 that has frequency `
` ` `# equal to the current character's `
` ` `# frequency in s1 `
` ` `for` `j ` `in` `range` `(` `MAX` `): `
` ` `# If such character is found `
` ` `if` `(freq1[i] ` `=` `=` `freq2[j]): `
` ` `# Set the frequnecy to -1 so that `
` ` `# it doesn't get picked again `
` ` `freq2[j] ` `=` `-` `1`
` ` ` ` `# Set found to true `
` ` `found ` `=` `True`
` ` `break`
` ` ` ` `# If there is no character in s2 `
` ` `# that could be mapped to the `
` ` `# current character in s1 `
` ` `if` `(found` `=` `=` `False` `): `
` ` `return` `False`
` ` ` ` `return` `True`
` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` `s1 ` `=` `"axx"`
` ` `s2 ` `=` `"cbc"`
` ` `l1 ` `=` `len` `(s1) `
` ` `l2 ` `=` `len` `(s2) `
` ` ` ` `if` `(canBeMapped(s1, l1, s2, l2)): `
` ` `print` `(` `"Yes"` `) `
` ` `else` `: `
` ` `print` `(` `"No"` `) `
` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

`// C# implementation of the approach ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `static` `int` `MAX = 26; `
` ` ` ` `// Function that returns true `
` ` `// if the mapping is possible `
` ` `public` `static` `Boolean canBeMapped(String s1, ` `int` `l1, `
` ` `String s2, ` `int` `l2) `
` ` `{ `
` ` ` ` `// Both the strings are of un-equal lengths `
` ` `if` `(l1 != l2) `
` ` `return` `false` `; `
` ` ` ` `// To store the frequencies of the `
` ` `// characters in both the string `
` ` `int` `[] freq1 = ` `new` `int` `[MAX]; `
` ` `int` `[] freq2 = ` `new` `int` `[MAX]; `
` ` ` ` `// Update frequencies of the characters `
` ` `for` `(` `int` `i = 0; i < l1; i++) `
` ` `freq1[s1[i] - ` `'a'` `]++; `
` ` `for` `(` `int` `i = 0; i < l2; i++) `
` ` `freq2[s2[i] - ` `'a'` `]++; `
` ` ` ` `// For every character of s1 `
` ` `for` `(` `int` `i = 0; i < MAX; i++) `
` ` `{ `
` ` ` ` `// If current character is `
` ` `// not present in s1 `
` ` `if` `(freq1[i] == 0) `
` ` `continue` `; `
` ` `Boolean found = ` `false` `; `
` ` ` ` `// Find a character in s2 that has frequency `
` ` `// equal to the current character's `
` ` `// frequency in s1 `
` ` `for` `(` `int` `j = 0; j < MAX; j++) `
` ` `{ `
` ` ` ` `// If such character is found `
` ` `if` `(freq1[i] == freq2[j]) `
` ` `{ `
` ` ` ` `// Set the frequnecy to -1 so that `
` ` `// it doesn't get picked again `
` ` `freq2[j] = -1; `
` ` ` ` `// Set found to true `
` ` `found = ` `true` `; `
` ` `break` `; `
` ` `} `
` ` `} `
` ` ` ` `// If there is no character in s2 `
` ` `// that could be mapped to the `
` ` `// current character in s1 `
` ` `if` `(!found) `
` ` `return` `false` `; `
` ` `} `
` ` `return` `true` `; `
` ` `} `
` ` ` ` `// Driver code `
` ` `public` `static` `void` `Main(String[] args) `
` ` `{ `
` ` `String s1 = ` `"axx"` `; `
` ` `String s2 = ` `"cbc"` `; `
` ` `int` `l1 = s1.Length; `
` ` `int` `l2 = s2.Length; `
` ` ` ` `if` `(canBeMapped(s1, l1, s2, l2)) `
` ` `Console.WriteLine(` `"Yes"` `); `
` ` `else`
` ` `Console.WriteLine(` `"No"` `); `
` ` `} `
`} ` ` ` `// This code is contributed ` `// by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find all strings formed from characters mapped to digits of a number
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Replace all occurrences of character X with character Y in given string
- Count occurrences of a string that can be constructed from another given string
- Maximum consecutive occurrences of a string in another given string
- Minimize ASCII values sum after removing all occurrences of one character
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Sum of all distances between occurrences of same characters in a given string
- Minimum string such that every adjacent character of given string is still adjacent
- Remove all occurrences of a character in a string
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Remove all occurrences of a character in a string | Recursive approach
- Rearrange the given string such that all prime multiple indexes have same character
- Longest subsequence with at least one character appearing in every string
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Check if max occurring character of one string appears same no. of times in other
- Find length of longest subsequence of one string which is substring of another string
- Replace every character of string by character whose ASCII value is K times more than it

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

**Practice Tags :**