A problem about lsomorphic Strings in java


  • -1
    Y

    public static boolean isIsomorphic(String s, String t) {
    Map<Character, Character> hm = new HashMap<Character, Character>();
    if (s.length() != t.length())
    return false;
    char[] s1 = s.toCharArray();
    char[] t1 = t.toCharArray();
    for (int i = 0; i < s1.length; i++) {
    if (hm.containsKey(s1[i])) {
    if ((t1[i] != hm.get(s1[i])))
    return false;
    }
    hm.put(s1[i], t1[i]);
    }

    	return true;
    }
    

    can‘t AC in leetcode,when"aa", "ab"
    but in eclipse i have no problems


  • 0
    A
    public boolean isIsomorphic(String s, String t) {
        HashMap<Character, Character> charSet = new HashMap<>();
        char[] sArray = s.toCharArray();
        char[] tArray = t.toCharArray();
        
        for(int i = 0; i < sArray.length; i++)
        {
            if(charSet.containsKey(sArray[i]))
            {  
                if(charSet.get(sArray[i]).charValue() != tArray[i]) 
                {
                    return false;
                }
            }
            else
                charSet.put(sArray[i], tArray[i]);
        }
        
        return true;
    }
    

    I'm also receiving an error for test case 'aa', 'ab' but works fine in eclipse.


Log in to reply
 

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