• 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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    CSS左侧固定宽 右侧自适应(兼容所有浏览器)
    MySQL学习笔记之一
    删除goagnt证书方法〔chrome
    JS通过ajax动态读取xml文件内容
    display vs visibility
    android SDK更新
    关于JS APP
    Ajax HTML, JS
    Request/Server模式
    关于SOAP
  • 原文地址:https://www.cnblogs.com/dysjtu1995/p/12639761.html
Copyright © 2020-2023  润新知