Isomorphic Strings

https://leetcode.com/problems/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.

 1 import java.util.HashMap;
 2 import java.util.HashSet;
 3 import java.util.Map;
 4 import java.util.Map.Entry;
 5 import java.util.Set;
 6 
 7 public class Solution {
 8     public static boolean isIsomorphic(String s, String t) {
 9         Map<Character,Integer> map=new HashMap();
10         if(s.length()!=t.length()){return false;}
11         boolean result=true;
12         int len=s.length();
13         for(int i=0;i<len;i++){
14             char c=s.charAt(i);
15             if(!map.containsKey(c)){
16             map.put(c,i);
17             }
18             else{
19             int index=map.get(c);
20             if(t.charAt(i)!=t.charAt(index)){
21                 result=false;
22                 break;
23             }
24             }
25         }
26         if(result==false){return false;}
27         
28         len=t.length();
29         map.clear();
30         for(int i=0;i<len;i++){
31             char c=t.charAt(i);
32             if(!map.containsKey(c)){
33             map.put(c,i);
34             }
35             else{
36             int index=map.get(c);
37             if(s.charAt(i)!=s.charAt(index)){
38                 result=false;
39                 break;
40             }
41             }
42         }
43         return result;
44     }
45     public static void main(String[]args){
46     System.out.println(isIsomorphic("ab","aa"));
47     }
48 }
原文地址:https://www.cnblogs.com/qq1029579233/p/4475854.html