提交代码:

class Solution {
    public boolean isIsomorphic(String s, String t) {
    	if(s.length()!=t.length())	return false;
    	Map<Character,Integer> sMap=new HashMap<>(); 
    	Map<Character,Integer> tMap=new HashMap<>(); 
    	int[] sNums=new int[s.length()];
    	int[] tNums=new int[t.length()];
    	
    	int base=0;
    	for(int i=0;i<s.length();i++) {
    		if(sMap.containsKey(s.charAt(i)))
    			sNums[i]=sMap.get(s.charAt(i));
    		else {
    			sMap.put(s.charAt(i),base);
    			sNums[i]=base++;
    		}
    	}
    	
    	base=0;
    	for(int i=0;i<t.length();i++) {
    		if(tMap.containsKey(t.charAt(i)))
    			tNums[i]=tMap.get(t.charAt(i));
    		else {
    			tMap.put(t.charAt(i),base);
    			tNums[i]=base++;
    		}
    	}
    	
    	for(int i=0;i<s.length();i++)
    		if(sNums[i]!=tNums[i]) return false;
    	return true;
    }
}

运行结果:

【leetcode】205(Easy)Isomorphic Strings

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-12-11
  • 2022-12-23
  • 2021-07-31
猜你喜欢
  • 2021-10-07
  • 2021-12-06
  • 2021-12-08
  • 2021-09-08
  • 2022-01-02
  • 2021-07-13
  • 2021-09-15
相关资源
相似解决方案