检查两个给定的字符串是否彼此同构 |设置 2(使用 STL)
给定两个字符串str1和str2 ,任务是检查这两个字符串是否彼此同构。
Two strings str1 and str2 are called isomorphic if there is a one-to-one mapping possible for every character of str1 to every character of str2 and all occurrences of every character in ‘str1’ map to the same character in ‘str2’.
例子:
Input: str1 = “aab”, str2 = “xxy”
Output: True
Explanation: ‘a’ is mapped to ‘x’ and ‘b’ is mapped to ‘y’.
Input: str1 = “aab”, str2 = “xyz”
Output: False
Explanation: One occurrence of ‘a’ in str1 has ‘x’ in str2 and other occurrence of ‘a’ has ‘y’.
方法:基于频率计数和字典的方法在上一篇中有提到。在这里,我们将讨论使用 STL 的解决方案。对此的想法如下所述:
Use the unordered map data structure for hashing purpose by using the character of str1 as the key and the difference between the ASCII value of the two characters on the same index as the value.
If the same character repeats itself then check whether the previously hashed value matches it or not which confirms if a character is mapped to only one character.
请按照以下步骤解决问题:
- 检查str1的大小是否与str2相同,
- 如果不是,则返回 false。
- 现在声明一个无序映射并从str1的索引 0 开始迭代。
- 对于每个字符,检查当前字符是否已经出现
- 如果没有,则添加上面提到的键值对。
- 否则,检查map[str1[curr_index]] = str1[curr_index] – str2[curr_index] 是否。如果不相等则返回false。
下面是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
// This function returns true if
// str1 and str2 are isomorphic
bool areIsomorphic(string str1, string str2)
{
// Unordered map to store the
// Hash value for each string
unordered_map fre;
int size1 = str1.size();
int size2 = str2.size();
// Check whether size equals or not,
// if not then isomorphism
// can't be achieved
if (size1 != size2)
return false;
for (int i = 0; i < size1; i++) {
// Check whether current character
// already hashed or not
if (fre.find(str1[i]) == fre.end()) {
// If not then assign
// hash value to it
fre[str1[i]] = str1[i] - str2[i];
}
// If already hashed then compare
// with its current hashed value
else if (fre[str1[i]]
!= (str1[i] - str2[i])) {
return false;
}
}
return true;
}
// Driver program
int main()
{
string s1 = "aab";
string s2 = "xxy";
// Calling function
bool ans = areIsomorphic(s1, s2);
if (ans)
cout << "True";
else
cout << "False";
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
class GFG {
// This function returns true if
// str1 and str2 are isomorphic
public static boolean areIsomorphic(String str1,
String str2)
{
// Unordered map to store the
// Hash value for each string
HashMap fre = new HashMap<>();
int size1 = str1.length();
int size2 = str2.length();
// Check whether size equals or not,
// if not then isomorphism
// can't be achieved
if (size1 != size2)
return false;
for (int i = 0; i < size1; i++) {
// Check whether current character
// already hashed or not
if (fre.get(str1.charAt(i)) == null) {
// If not then assign
// hash value to it
fre.put(
str1.charAt(i),
(int)(str1.charAt(i) - str2.charAt(i)));
}
// If already hashed then compare
// with its current hashed value
else if (fre.get(str1.charAt(i))
!= (int)((str1.charAt(i)
- str2.charAt(i)))) {
return false;
}
}
return true;
}
public static void main(String[] args)
{
String s1 = "aab";
String s2 = "xxy";
// Calling function
boolean ans = areIsomorphic(s1, s2);
if (ans != false)
System.out.print("True");
else
System.out.print("False");
}
}
// This code is contributed by Rohit Pradhan
True
时间复杂度: O(N) 其中 N 是字符串的大小
辅助空间: O(N)