My accepted JAVA solution


  • 0
    A

    public class Solution {

    public boolean isIsomorphic(String s, String t) {
        Solution solution = new Solution();
        
        int[] newS = solution.convertStringToArray(s);
        int[] newT = solution.convertStringToArray(t);
        
        for (int i=0; i<newS.length; i++) {
            if (newS[i] != newT[i]) return false;
        }
        return true;
    }
    
    private int[] convertStringToArray(String a) {
        char[] ch = a.toCharArray();
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int tempValue = 0;
        int[] array = new int[ch.length];
    
        for (int i=0; i<ch.length; i++) {
            if (!map.containsKey(ch[i])) {
                map.put(ch[i],tempValue);
                array[i] = tempValue++;
            } else {
                array[i] = map.get(ch[i]);
            }
        }
        return array;
    }
    

    }


Log in to reply
 

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