leetcode--Isomorphic Strings

来源:互联网 发布:淘宝店铺层级划分 编辑:程序博客网 时间:2024/06/06 10:42

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

分析:建立HashMap来保存映射关系--建立HashSet来保存被映射

one:元素遍历,若s元素在Map里 s元素Map的键不是t元素false;若元素不在Map里在被映射的set里false,否则分别加入Map及set。

public class Solution {    public boolean isIsomorphic(String s, String t) {        if(s==null||t==null){return false;}        if(s.length()!=t.length())return false;        Map<Character,Character> map = new HashMap<Character,Character>();        Set<Character> set = new HashSet<Character>();                for(int i=0;i<s.length();i++){            char c1 = s.charAt(i);            char c2 = t.charAt(i);                        if(map.containsKey(c1)){                if(map.get(c1)!=c2) return false;            }else{                if(set.contains(c2))return false;                else{                    map.put(c1,c2);                    set.add(c2);                }            }        }        return true;    }}

public class Solution {    public boolean isIsomorphic(String s, String t) {        Map m = new HashMap();        for (Integer i=0; i<s.length(); ++i)            if (m.put(s.charAt(i), i) != m.put(t.charAt(i)+"", i))             return false;        return true;    }}

public class Solution {        public boolean isIsomorphic(String sString, String tString) {        char[] s = sString.toCharArray();        char[] t = tString.toCharArray();        int length = s.length;        if(length != t.length) return false;        char[] sm = new char[256];        char[] tm = new char[256];        for(int i=0; i<length; i++){            char sc = s[i];            char tc = t[i];            if(sm[sc] == 0 && tm[tc] == 0){                sm[sc] = tc;                tm[tc] = sc;            }else{                if(sm[sc] != tc || tm[tc] != sc){                    return false;                }            }        }        return true;    }}

class Solution {public:    bool isIsomorphic(string s, string t) {        int m1[256] = {0}, m2[256] = {0}, n = s.size();        for (int i = 0; i < n; ++i) {            if (m1[s[i]] != m2[t[i]]) return false;            m1[s[i]] = i + 1;            m2[t[i]] = i + 1;        }        return true;    }};

public boolean isIsomorphic(String s1, String s2) {        Map<Character, Integer> m1 = new HashMap<>();        Map<Character, Integer> m2 = new HashMap<>();            for(Integer i = 0; i < s1.length(); i++) {            if(m1.put(s1.charAt(i), i) != m2.put(s2.charAt(i), i)) {                return false;            }        }        return true;    }



0 0
原创粉丝点击