Leetcode_290_单词规律_水题

12/16

水题
class Solution {
    
    
    public boolean wordPattern(String pattern, String s) {
    
    
        int n = pattern.length();
        Map<Character, String> mp = new HashMap<Character, String>();
        String[] str = s.split(" ");
        int m = str.length;
        if (m != n) {
    
    
            return false;
        }
        for (int i = 0; i < m; i++) {
    
    
            char ch=pattern.charAt(i);
            if (mp.containsKey(ch)) {
    
    
                if(!mp.get(ch).equals(str[i])) {
    
    
                    return false;
                }
                continue;
            }
            if(!mp.containsValue(str[i])) {
    
    
                mp.put(ch, str[i]);
                continue;
            }
            return false;
        }
        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/HDUCheater/article/details/111272199
今日推荐