Why my java solution can not pass.


  • 0
    S

    public class Solution {
    public boolean isIsomorphic(String s, String t) {

        if(s.equals(t))
        return true;
        
        Map<Character, String> ms = new HashMap<Character, String>();
        Map<Character, String> mt = new HashMap<Character, String>();
        
        char[] cs = s.toCharArray();
        char[] ct = t.toCharArray();
        
        for(int i = 0;i<cs.length; i++)
        if(ms.containsKey(cs[i]))
        ms.put(cs[i], ms.get(cs[i])+""+i);
        else
        ms.put(cs[i],i+"");
        
        for(int i = 0;i<ct.length; i++)
        if(mt.containsKey(ct[i]))
        mt.put(ct[i],mt.get(ct[i])+""+i);
        else
        mt.put(ct[i],i+"");
       
       Set<Character> ss = ms.keySet();
       Set<Character> st = mt.keySet();
       
       Iterator<Character> it = st.iterator();
       
       for(char tmps: ss){
           if(it.hasNext()){
               //String tmpt = mt.get(it.next());
               String values = ms.get(tmps);
               String valuet = mt.get(it.next());
               if(values.equals(valuet))
               continue;
               else
               return false;
           }
           return false;
           
       }
       
       return true;
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.