给定两个长度相同的字符串s1和s2 ,任务是计算字符对(c1, c2)的最小数量,这样通过在任何字符串中将c1转换为 c2或c2 转换为 c1任意次数,使两个字符串相同.
例子:
Input: s1 = “abb”, s2 = “dad”
Output: 2
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”
Output: 7
方法:这个问题可以通过使用图或不相交集来解决。构建一个空图 G 并遍历字符串。仅当满足以下条件之一时才在图 G 中添加边:
- s1[i]和s2[i]都不在 G 中。
- s1[i]在 G 中,但s2[i]不在 G 中。
- s2[i]在 G 中,但s1[i]不在 G 中。
- 没有从s1[i]到s2[i] 的路径。
最小对数将是最终图 G 中的边数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function which will check if there is
// a path between a and b by using BFS
bool checkPath(char a, char b,
map> &G)
{
map visited;
deque queue;
queue.push_back(a);
visited[a] = true;
while (!queue.empty())
{
int n = queue.front();
queue.pop_front();
if (n == b) return true;
for (auto i : G[n])
{
if (visited[i] == false)
{
queue.push_back(i);
visited[i] = true;
}
}
}
return false;
}
// Function to return the
// minimum number of pairs
int countPairs(string s1, string s2,
map> &G, int x)
{
// To store the count of pairs
int count = 0;
// Iterating through the strings
for (int i = 0; i < x; i++)
{
char a = s1[i];
char b = s2[i];
// Check if we can add an edge in the graph
if (G.find(a) != G.end() and
G.find(b) == G.end() and a != b)
{
G[a].push_back(b);
G[b].push_back(a);
count++;
}
else if (G.find(b) != G.end() and
G.find(a) == G.end() and a != b)
{
G[b].push_back(a);
G[a].push_back(b);
count++;
}
else if (G.find(a) == G.end() and
G.find(b) == G.end() and a != b)
{
G[a].push_back(b);
G[b].push_back(a);
count++;
}
else
{
if (!checkPath(a, b, G) and a != b)
{
G[a].push_back(b);
G[b].push_back(a);
count++;
}
}
}
// Return the count of pairs
return count;
}
// Driver Code
int main()
{
string s1 = "abb", s2 = "dad";
int x = s1.length();
map> G;
cout << countPairs(s1, s2, G, x) << endl;
return 0;
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 implementation of the approach
from collections import defaultdict, deque
# Function which will check if there is
# a path between a and b by using BFS
def Check_Path(a, b, G):
visited = defaultdict(bool)
queue = deque()
queue.append(a)
visited[a]= True
while queue:
n = queue.popleft()
if n == b:
return True
for i in list(G[n]):
if visited[i]== False:
queue.append(i)
visited[i]= True
return False
# Function to return the minimum number of pairs
def countPairs(s1, s2, G):
name = defaultdict(bool)
# To store the count of pairs
count = 0
# Iterating through the strings
for i in range(x):
a = s1[i]
b = s2[i]
# Check if we can add an edge in the graph
if a in G and b not in G and a != b:
G[a].append(b)
G[b].append(a)
count+= 1
elif b in G and a not in G and a != b:
G[b].append(a)
G[a].append(b)
count+= 1
elif a not in G and b not in G and a != b:
G[a].append(b)
G[b].append(a)
count+= 1
else:
if not Check_Path(a, b, G) and a != b:
G[a].append(b)
G[b].append(a)
count+= 1
# Return the count of pairs
return count
# Driver code
if __name__=="__main__":
s1 ="abb"
s2 ="dad"
x = len(s1)
G = defaultdict(list)
print(countPairs(s1, s2, G))
输出:
2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。