Can anyone help me find the problem of getOrDefault of HashMap ?


  • 0
    Z

    public class Solution {
      public boolean wordPattern(String pattern, String str) {
        char[] ca = pattern.toCharArray();
        String[] sa = str.split("\s+");
        if(pattern.equals("") || str.equals("") || ca.length!=sa.length) return false;
         Map<Character, Integer> m1 = new HashMap<>();
         Map<String, Integer> m2 = new HashMap<>();
         for(int i=0; i<ca.length; i++){
           //int n1 = m1.getOrDefault(ca[i], 0);
           //int n2 = m2.getOrDefault(sa[i], 0);
          //if(n1!=n2) return false;
           //above is the correct form, but blow goes wrong when it comes this case
          //"ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdd","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"
           //length is 130,when debugged in eclipse, it returns false when i equals 129,how to explain this problem? In this form the values on both sides of inequality are Byte?

           if(m1.getOrDefault(ca[i], 0)!=m2.getOrDefault(sa[i], 0)) return false;
           m1.put(ca[i], i+1);
           m2.put(sa[i], i+1);
         }
         return true;
       }
    }


Log in to reply
 

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