Any one can correct me why my code can't pass big cases?


  • 0
    H

    public boolean wordPattern(String pattern, String str) {
    String[] strs = str.split(" ");
    if (strs.length != pattern.length()) {
    return false;
    }
    Map<String, Integer> pattern_map = new HashMap<>();
    Map<String, Integer> str_map = new HashMap<>();
    for (int i = 0; i < strs.length; i++) {
    if (pattern_map.getOrDefault(pattern.charAt(i) + "", -1) != str_map.getOrDefault(strs[i], -1)) {
    return false;
    }
    pattern_map.put(pattern.charAt(i) + "", i);
    str_map.put(strs[i], i);
    }
    return true;
    }


Log in to reply
 

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