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.
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a String can be converted to Pangram in K changes
- Check whether str1 can be converted to str2 with the given operations
- String slicing in Python to check if a string can become empty by recursive deletion
- Queries to check if string B exists as substring in string A
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be obtained by rotating another string d places
- Check if a string can be repeated to make another string
- Check if the given string is shuffled substring of another string
- Check whether second string can be formed from characters of first string
- Check if a string contains a palindromic sub-string of even length
- Check if a given string is sum-string
- Check if given string contains all the digits
- Check if a string has m consecutive 1's or 0's
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.