0
0

More than 1 year has passed since last update.

難易度

Easy

アプローチ

Brute-force

class Solution {
    public boolean wordPattern(String pattern, String s) {
        HashMap<Character, String> map = new HashMap<>();
        String[] arg = s.split(" ");
        int length =  pattern.length();
        if(arg.length != length){
            return false;
        }
        
        for (int i = 0; i < length; i++) {
            char p = pattern.charAt(i);
            if (map.containsKey(p)) {
                String value = map.get(p);
                if (!arg[i].equals(value)) {
                    return false;
                }
            } else {
                if(map.containsValue(arg[i])){
                    return false;
                }
                map.put(p, arg[i]);
            }
        }
        return true;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0