Easy to understand solution in java


  • 0
    S

    {if (s.length() == 0 || s.length() == 1) {
    return true;
    }
    char[] sCharArray = s.toCharArray();
    char[] tCharArray = t.toCharArray();
    Map<Character, Character> mapper = new HashMap<Character, Character>();
    mapper.put(sCharArray[0], tCharArray[0]);
    for (int i = 1, j = 1; i < s.length() && j < t.length(); i++, j++) {
    if (mapper.containsKey(sCharArray[i])) {
    if (mapper.get(sCharArray[i]).equals(tCharArray[j])) {
    continue;
    } else {
    return false;
    }
    }
    if (!mapper.containsKey(sCharArray[i])) {
    if (mapper.containsValue(tCharArray[j])) {
    return false;
    } else {
    mapper.put(sCharArray[i], tCharArray[j]);
    }
    }
    }
    return true;
    }


  • 0
    N

    The format of the code is horrible


Log in to reply
 

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