Given two strings s1 and s2 of same length, the task is to count the minimum number of pairs of characters (c1, c2) such that by transforming c1 to c2 or c2 to c1 any number of times in any string make both the strings same.
Input: s1 = “abb”, s2 = “dad”
Transform ‘a’ -> ‘d’, ‘b’ -> ‘a’ and ‘b’ -> ‘a’ -> ‘d’ in s1.
We can not take (a, d), (b, a), (b, d) as pairs because
(b, d) can be achieved by following transformation ‘b’ -> ‘a’ -> ‘d’
Input: s1 = “drpepper”, s2 = “cocacola”
Approach: This Problem can be solved by using Graphs or Disjoint Sets. Build an empty graph G and iterate through the strings. Add an edge in graph G only if one of the following conditions is met:
- Both s1[i] and s2[i] are not in G.
- s1[i] is in G but s2[i] is not in G.
- s2[i] is in G but s1[i] is not in G.
- There is no path from s1[i] to s2[i].
The minimum number of pairs will be the count of edges in the final graph G.
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.
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Minimum number of given operations required to make two strings equal
- Find the minimum number of preprocess moves required to make two strings equal
- Minimum number of operations required to make two strings equal
- Minimum cost to make two strings same
- Minimize count of given operations required to make two given strings permutations of each other
- Check whether two strings contain same characters in same order
- Meta Strings (Check if two strings can become same after a swap in one string)
- Count of same length Strings that exists lexicographically in between two given Strings
- Minimize steps required to make all array elements same by adding 1, 2 or 5
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Pairs of complete strings in two sets of strings
- Minimum number of swaps required to make the string K periodic
- Minimum number of edges required to be removed from an Undirected Graph to make it acyclic
- Minimum Cost To Make Two Strings Identical
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Minimum cost to make two strings identical by deleting the digits
- Minimum Cost to make two Numeric Strings Identical
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum characters to be deleted from the beginning of two strings to make them equal
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.
Improved By : sanjeev2552