205. Isomorphic Strings

Problem:

Given two strings s and t, determine if they are isomorphic.

Two strings 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

Note:
You may assume both s and t have the same length.

思路

Solution (C++):

bool isIsomorphic(string s, string t) {
    int n = s.length();
    if (n == 0)  return true;
    vector<int> v1(256, 0), v2(256, 0);
    for (int i = 0; i < n; ++i) {
        if (v1[s[i]] != v2[t[i]])  return false;
        v1[s[i]] = i+1;
        v2[t[i]] = i+1;
    }
    return true;
}

性能

Runtime: 4 ms  Memory Usage: 7.2 MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB

相关链接如下:

知乎:littledy

欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/dysjtu1995/p/12639761.html