Given two strings str and str1, the task is to check whether one string can be converted to other by using the following operation:
- Convert all the presence of a character by a different character.
For example, if str = “abacd” and operation is to change character ‘a’ to ‘k’, then the resultant str = “kbkcd”
Input: str = “abbcaa”; str1 = “bccdbb”
Explanation: The mappings of the characters are:
c –> d
b –> c
a –> b
Input: str = “abbc”; str1 = “bcca”
Explanation: The mapping of characters are:
a –> b
b –> c
c –> a
Here, due to the presence of a cycle, a specific order cannot be found.
- According to the given operation, every unique character should map to a unique character may be same or different.
- This can easily be checked by a Hashmap.
- However, this fails in cases where there is a cycle in mapping and a specific order cannot be determined.
- One example of such case is Example 2 above.
- Therefore, for mapping, the first and final characters are stored as edges in a hashmap.
- For finding cycle with the edges, these edges are mapped one by one to a parent and are checked for cycle using Union and Find Algorithm.
Below is the implementation of the above approach.
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.
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if one string can be converted to other using given operation
- Check if a String can be converted to Pangram in K changes
- Check whether str1 can be converted to str2 with the given operations
- Check if array can be converted into strictly decreasing sequence
- Check if N can be converted to the form K power K by the given operation
- Check if permutaion of one string can break permutation of another
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be repeated to make another string
- Check if a string can be obtained by rotating another string d places
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if it is possible to convert one string into another with given constraints
- Check if it is possible to transform one string to another
- Find length of longest subsequence of one string which is substring of another string
- Check if a string can be transformed to another by sorting substrings
- Count occurrences of a string that can be constructed from another given string
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if characters of one string can be swapped to form other
- Sort an Array alphabetically when each number is converted into words
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.