美文网首页
205. Isomorphic Strings

205. Isomorphic Strings

作者: 衣介书生 | 来源:发表于2018-02-28 15:03 被阅读7次

题目分析

原题链接,登录 LeetCode 后可用
这道题目让我们判断两个字符串是否是同构字符串。示例如下:

Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.

解题思路是用两个 Map 记录两个字符串中的字符的映射关系。

代码

class Solution {
    public boolean isIsomorphic(String s, String t) {
        Map<Character, Character> hm1 = new HashMap<Character, Character>();
        Map<Character, Character> hm2 = new HashMap<Character, Character>();
        for(int i = 0; i < s.length(); i++) {
            char c1 = s.charAt(i);
            char c2 = t.charAt(i);
            if(hm1.containsKey(c1)) {
                if(hm1.get(c1) != c2) {
                    return false;
                }
            }
            if(hm2.containsKey(c2)) {
                if(hm2.get(c2) != c1) {
                    return false;
                }
            }
            hm1.put(c1, c2);
            hm2.put(c2, c1);
        }
        return true;
    }
}

相关文章

  • 205. Isomorphic Strings

    205. Isomorphic Strings 题目:https://leetcode.com/problems/...

  • 2019-01-21

    LeetCode 205. Isomorphic Strings Description Given two st...

  • 205. Isomorphic Strings

    https://leetcode.com/problems/isomorphic-strings/descript...

  • 205. Isomorphic Strings

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

  • 205. Isomorphic Strings

    竟然这样ac了,我只是试了一下。。。想法就是对于每一个字符串都建立一个哈希表,统计他们各个字母的数量,对于相同位置...

  • 205. Isomorphic Strings

    题目分析 原题链接,登录 LeetCode 后可用这道题目让我们判断两个字符串是否是同构字符串。示例如下: 解题思...

  • 205. Isomorphic Strings

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

  • 205. Isomorphic Strings

    Given two stringssandt, determine if they are isomorphic....

  • 205. Isomorphic Strings

    首先考虑corner case,这题两个空字符返回算True…… 从左到右扫,映射关系存为字典。 如果左边扫到重复...

  • 205. Isomorphic Strings

    问题 Given two strings s and t, determine if they are isomo...

网友评论

      本文标题:205. Isomorphic Strings

      本文链接:https://www.haomeiwen.com/subject/zbabxftx.html