返回介绍

solution / 0200-0299 / 0205.Isomorphic Strings / README_EN

发布于 2024-06-17 01:04:03 字数 5967 浏览 0 评论 0 收藏 0

205. Isomorphic Strings

中文文档

Description

Given two strings s and t, _determine if they are isomorphic_.

Two strings s and t are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

 

Example 1:

Input: s = "egg", t = "add"
Output: true

Example 2:

Input: s = "foo", t = "bar"
Output: false

Example 3:

Input: s = "paper", t = "title"
Output: true

 

Constraints:

  • 1 <= s.length <= 5 * 104
  • t.length == s.length
  • s and t consist of any valid ascii character.

Solutions

Solution 1: Hash Table or Array

We can use two hash tables or arrays $d_1$ and $d_2$ to record the character mapping relationship between $s$ and $t$.

Traverse $s$ and $t$, if the corresponding character mapping relationships in $d_1$ and $d_2$ are different, return false, otherwise update the corresponding character mapping relationships in $d_1$ and $d_2$. After the traversal is complete, it means that $s$ and $t$ are isomorphic, and return true.

The time complexity is $O(n)$ and the space complexity is $O(C)$. Where $n$ is the length of the string $s$; and $C$ is the size of the character set, which is $C = 256$ in this problem.

class Solution:
  def isIsomorphic(self, s: str, t: str) -> bool:
    d1 = {}
    d2 = {}
    for a, b in zip(s, t):
      if (a in d1 and d1[a] != b) or (b in d2 and d2[b] != a):
        return False
      d1[a] = b
      d2[b] = a
    return True
class Solution {
  public boolean isIsomorphic(String s, String t) {
    Map<Character, Character> d1 = new HashMap<>();
    Map<Character, Character> d2 = new HashMap<>();
    int n = s.length();
    for (int i = 0; i < n; ++i) {
      char a = s.charAt(i), b = t.charAt(i);
      if (d1.containsKey(a) && d1.get(a) != b) {
        return false;
      }
      if (d2.containsKey(b) && d2.get(b) != a) {
        return false;
      }
      d1.put(a, b);
      d2.put(b, a);
    }
    return true;
  }
}
class Solution {
public:
  bool isIsomorphic(string s, string t) {
    int d1[256]{};
    int d2[256]{};
    int n = s.size();
    for (int i = 0; i < n; ++i) {
      char a = s[i], b = t[i];
      if (d1[a] != d2[b]) {
        return false;
      }
      d1[a] = d2[b] = i + 1;
    }
    return true;
  }
};
func isIsomorphic(s string, t string) bool {
  d1 := [256]int{}
  d2 := [256]int{}
  for i := range s {
    if d1[s[i]] != d2[t[i]] {
      return false
    }
    d1[s[i]] = i + 1
    d2[t[i]] = i + 1
  }
  return true
}
function isIsomorphic(s: string, t: string): boolean {
  const d1: number[] = new Array(256).fill(0);
  const d2: number[] = new Array(256).fill(0);
  for (let i = 0; i < s.length; ++i) {
    const a = s.charCodeAt(i);
    const b = t.charCodeAt(i);
    if (d1[a] !== d2[b]) {
      return false;
    }
    d1[a] = i + 1;
    d2[b] = i + 1;
  }
  return true;
}
use std::collections::HashMap;
impl Solution {
  fn help(s: &[u8], t: &[u8]) -> bool {
    let mut map = HashMap::new();
    for i in 0..s.len() {
      if map.contains_key(&s[i]) {
        if map.get(&s[i]).unwrap() != &t[i] {
          return false;
        }
      } else {
        map.insert(s[i], t[i]);
      }
    }
    true
  }

  pub fn is_isomorphic(s: String, t: String) -> bool {
    let (s, t) = (s.as_bytes(), t.as_bytes());
    Self::help(s, t) && Self::help(t, s)
  }
}
public class Solution {
  public bool IsIsomorphic(string s, string t) {
    int[] d1 = new int[256];
    int[] d2 = new int[256];
    for (int i = 0; i < s.Length; ++i) {
      var a = s[i];
      var b = t[i];
      if (d1[a] != d2[b]) {
        return false;
      }
      d1[a] = i + 1;
      d2[b] = i + 1;
    }
    return true;
  }
}

Solution 2

class Solution:
  def isIsomorphic(self, s: str, t: str) -> bool:
    d1, d2 = [0] * 256, [0] * 256
    for i, (a, b) in enumerate(zip(s, t), 1):
      a, b = ord(a), ord(b)
      if d1[a] != d2[b]:
        return False
      d1[a] = d2[b] = i
    return True
class Solution {
  public boolean isIsomorphic(String s, String t) {
    int[] d1 = new int[256];
    int[] d2 = new int[256];
    int n = s.length();
    for (int i = 0; i < n; ++i) {
      char a = s.charAt(i), b = t.charAt(i);
      if (d1[a] != d2[b]) {
        return false;
      }
      d1[a] = i + 1;
      d2[b] = i + 1;
    }
    return true;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文