Loading

Leetcode290.单词规律

题目连接:https://leetcode-cn.com/problems/word-pattern/

代码:

class Solution {
    public boolean wordPattern(String pattern, String s) {
        String[] t = s.split(" ");
        String[] map = new String[26];
        for(int i=0; i<map.length; i++) map[i] = "";
        Set<String> set = new HashSet<>();
        if(t.length != pattern.length()) return false;
        for(int i=0; i<pattern.length(); i++){
            int idx = (int) (pattern.charAt(i) - 'a');
            if("".equals(map[idx])){
                if(set.contains(t[i])) return false;
                map[idx] = t[i];
            }else if(!t[i].equals(map[idx])){
                return false;
            }
            set.add(t[i]);
        }
        return true;
    }
}
执行用时:1 ms, 在所有 Java 提交中击败了98.94%的用户
内存消耗:36.2 MB, 在所有 Java 提交中击败了93.94%的用户

 

 
posted @ 2020-12-16 15:31  yoyuLiu  阅读(87)  评论(0编辑  收藏  举报