Java

class Solution {
    public boolean wordPattern(String pattern, String str) {
        HashMap<Character,Integer> map1 = new HashMap<>();
        HashMap<String,Integer> map2 = new HashMap<>();
        
        String [] s = str.split(" ");
        if(s.length!=pattern.length()) return false;
        for(int i =0;i<pattern.length();i++)
        {
            char ch = pattern.charAt(i);
            if(!map1.containsKey(ch)) map1.put(ch,i+1);
            if(!map2.containsKey(s[i])) map2.put(s[i],i+1);
            
            int a = map1.get(ch);
            int b = map2.get(s[i]);
                     
            if(a!=b) return false;
        }
        return true;
    }
}

这道题,当值>128时,map.get() != map.get() 会出现错误结果,但不知道为什么

原文地址:https://www.cnblogs.com/qlky/p/7674469.html