查找表类算法//同构字符串

给定两个字符串 和 t,判断它们是否是同构的。

如果 中的字符可以被替换得到 ,那么这两个字符串是同构的。

所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。

示例 1:

输入: s = "egg", t = "add"
输出: true

示例 2:

输入: s = "foo", t = "bar"
输出: false

示例 3:

输入: s = "paper", t = "title"
输出: true

说明:
你可以假设 和 具有相同的长度。

public boolean isIsomorphic(String s, String t) {
       if(s==null || s.length()<=1)
			return true;
		HashMap<Character,Character> hm=new HashMap<Character,Character>();
		for(int i=0;i<s.length();i++){
			char ch1=s.charAt(i);
			char ch2=t.charAt(i);
			if(hm.containsKey(ch1)){
				if(hm.get(ch1)==ch2)
					continue;
				else
					return false;
			}else{
				if(hm.containsValue(ch2))
					return false;
				else
					hm.put(ch1, ch2);
			}
		}
		return true;
   }
class Solution {
    
public boolean isIsomorphic(String s, String t) {
       Map<Character,Character> mapa = new HashMap<Character,Character>();
        for(int i=0;i<t.length();i++){
            if(mapa.containsKey(s.charAt(i))){
                if(mapa.get(s.charAt(i))!=t.charAt(i))
                    return false;
            }else{
                if(mapa.containsValue(t.charAt(i)))
                    return false;
                else
                    mapa.put(s.charAt(i),t.charAt(i));
            }
        }
        return true;
    }
}

 

posted @ 2018-11-06 09:58  strawqqhat  阅读(99)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }