My simple Java solution


  • 0
    D

    Character - index - String

    public boolean wordPattern(String pattern, String str) {
            Map<Object, Object> map = new HashMap<>();
            char[] pt = pattern.toCharArray();
            String[] st = str.split(" ");
            if (pt.length != st.length) {
                return false;
            }
            for (int i=0; i<pt.length; ++i) {
                if (!Objects.equals(map.put(pt[i], i), map.put(st[i], i))) {
                    return false;
                }
            }
            return true;
        }
    

Log in to reply
 

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