I am wondering why people use both map with set... there is a method in HashMap call map.containsValue()


  • 0
            return check(pattern,str,new HashMap<Character,String>());
        }
        public boolean check(String pattern,String str,Map<Character,String> map) {
            if(pattern.length() == 0 && str.length() == 0) return true;
            if(pattern.length() == 0 || str.length() == 0) return false;
            char currChar = pattern.charAt(0);
            if(map.containsKey(currChar)) {
                if(str.startsWith(map.get(currChar))) {
                    return check(pattern.substring(1),str.substring(map.get(currChar).length()),map);
                } 
                else return false;
            }
            
            for(int i = 1;i <= str.length();i++) {
                String currString = str.substring(0,i);
                if( !map.containsValue(currString)) {
                    map.put(currChar,currString);
                    if(check(pattern.substring(1),str.substring(i),map)) return true;
                    map.remove(currChar);
                }
            }
            return false;
        }

Log in to reply
 

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