leetcode 205. Isomorphic Strings

简介: 题目Given two strings s and t, determine if they are isomorphic.

题目

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.

For example,
Given “egg”, “add”, return true.

Given “foo”, “bar”, return false.

Given “paper”, “title”, return true.

理解

判断两个字符串是否是同分异构,也就是判断两个字符串里面的字符是不是一一对应。

解决

两个map

正向保证不会一个key对应两个value,反向保证不会两个key对应同一个value

public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if(s==null && t==null){
            return true;
        }else if(s==null || t==null){
            return false;
        }
        if(s.length() != t.length()){
            return false;
        }
        int N = s.length();
        Map<Character,Character> map = new HashMap<Character,Character>();
        Map<Character,Character> reservedMap = new HashMap<Character,Character>();
        for(int i=0; i<N; i++){
            if(map.containsKey(s.charAt(i))){
                if(map.get(s.charAt(i))!=t.charAt(i)){
                    return false;
                }
            }else{
                map.put(s.charAt(i),t.charAt(i));
            }
            if(reservedMap.containsKey(t.charAt(i))){
                if(reservedMap.get(t.charAt(i))!=s.charAt(i)){
                    return false;
                }
            }else{
                reservedMap.put(t.charAt(i),s.charAt(i));
            }
        }
        return true;
    }
}

一个map,一个set

map保证不会一个key对应两个value,set保证不会两个key对应同一个value

public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if(s==null && t==null){
            return true;
        }else if(s==null || t==null){
            return false;
        }
        if(s.length() != t.length()){
            return false;
        }
        int N = s.length();
        Map<Character,Character> map = new HashMap<Character,Character>();
        Set<Character> set = new HashSet<Character>();
        for(int i=0; i<N; i++){
            if(map.containsKey(s.charAt(i))){
                if(map.get(s.charAt(i))!=t.charAt(i)){
                    return false;
                }
            }else if(set.contains(t.charAt(i))){
                return false;
            }else{
                map.put(s.charAt(i),t.charAt(i));
                set.add(t.charAt(i));
            }
        }
        return true;
    }
}
目录
相关文章
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
67 0
LeetCode 415. Add Strings
LeetCode 205. Isomorphic Strings
给定两个字符串 s 和 t,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
57 0
LeetCode 205. Isomorphic Strings
LeetCode 43. Multiply Strings
给定两个表示为字符串形式的非负整数num1和num2,返回num1和num2的乘积,也表示为字符串形式。
52 0
LeetCode 43. Multiply Strings
|
Java 索引 Python
LeetCode 205:同构字符串 Isomorphic Strings
题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
869 0
LeetCode - 43. Multiply Strings
43. Multiply Strings  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定两个字符串,计算这两个字符串相乘的结果.
905 0
|
索引 Java
LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611168 翻译 给定两个字符串s和t,决定它们是否是同构的。
829 0
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0