Who can tell me why my solution can't pass the 31th test!I'll appreciate you very much!


  • 0

    with the input:

        String pattern = "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdd";
        String str = "s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s t t";
    
    public boolean wordPattern(String pattern, String str) {
        String[] strings = str.split(" ");
        if (pattern.length() != strings.length) {
            return false;
        }
        HashMap<String, Integer> hashMap1 = new HashMap<>();
        HashMap<Character, Integer> hashMap2 = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if (hashMap2.containsKey(pattern.charAt(i))
                    && hashMap1.containsKey(strings[i])
                    && hashMap1.get(strings[i]) == hashMap2.get(pattern.charAt(i))) {
                hashMap2.put(pattern.charAt(i), i);
                hashMap1.put(strings[i], i);
            } else if (!hashMap2.containsKey(pattern.charAt(i))
                    && !hashMap1.containsKey(strings[i])) {
                hashMap2.put(pattern.charAt(i), i);
                hashMap1.put(strings[i], i);
            } else {
                return false;
            }
        }
        return true;
    }

Log in to reply
 

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