📌  相关文章
📜  检查两个给定的字符串是否彼此同构 |设置 2(使用 STL)

📅  最后修改于: 2022-05-13 01:56:05.359000             🧑  作者: Mango

检查两个给定的字符串是否彼此同构 |设置 2(使用 STL)

给定两个字符串str1str2 ,任务是检查这两个字符串是否彼此同构。

例子:

方法:基于频率计数字典的方法在上一篇中有提到。在这里,我们将讨论使用 STL 的解决方案。对此的想法如下所述:

请按照以下步骤解决问题:

  • 检查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)