LeetCode OJ 205 Isomorphic Strings

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.

Note:

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

题意:判断两个字符串同形字符串,就是说,将s中的字符替换为t中的相应字符,能够保证s中不同的字符替换成t中的不同的字符,可以想象成一个加密过程,利用某种映射,将s加密成t

public class Solution {
    public boolean isIsomorphic(String s, String t) {
        //利用map保存映射关系
        Map<Character,Character> map = new HashMap<Character,Character>();
        for(int i = 0 ; i < s.length() ; i ++){
            char key = s.charAt(i);
            char value = t.charAt(i);
            if(map.containsKey(key)){
                if(value != map.get(key)){//存在一条key->x的映射,且x != value
                    return false;
                }
            }else if(map.containsValue(value)){
                return false;//已经存在一条x->value的映射,但是x != key
            }else{//不存在key->x映射和x->value的映射
                map.put(key,value);
            }
        }
        return true;
    }
}

搜了一下,感觉别人的算法跟我的差不多,没找到很高效的算法,为什么我的运行时间那么慢

时间: 2024-10-10 06:40:55

LeetCode OJ 205 Isomorphic Strings的相关文章

【LeetCode】205 - Isomorphic Strings

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.

【LeetCode】205. Isomorphic Strings 解题小结

题目: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 character

LeetCode OJ:Isomorphic Strings(同构字符串)

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.

205. Isomorphic Strings - LeetCode

Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同 Java实现: public boolean isIsomorphic(String s, String t) { Map<String, Integer> map = new HashMap<>(); for (int i=0; i<s.length(

LeetCode 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 cha

[LeetCode] 205. Isomorphic Strings 解题思路 - Java

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.

Java [Leetcode 205]Isomorphic Strings

题目描述: 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 charac

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译 给定两个字符串s和t,决定它们是否是同构的. 如果s中的元素被替换可以得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留. 没有两个字符可以被映射到相同的字符,但字符可以映射到该字符本身. 例如, 给定"egg","add",返回真. 给定"foo","bar",返回假. 给定"paper","title",返回真. 批

leetcode 205. Isomorphic Strings(哈希表)

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.