WRONG answer but AC


  • 0
    T
    public class Solution {
        public boolean wordPattern(String pattern, String str) {
            HashSet<String> set=new HashSet<String>();
            int bitmap=0;
            String[] strs=str.split(" ");
            if(strs.length!=pattern.length()) return false;
            for(int i=0;i<strs.length;i++){
                char c=pattern.charAt(i);
                boolean f1=set.contains(strs[i]);
                boolean f2=((bitmap&(1<<c-'0'))!=0);
                if( f1&& f2){
                    continue;
                }else if((!f1) && (!f2)){
                    set.add(strs[i]);
                    bitmap|=1<<(c-'0');
                }else{
                    return false;
                }
            }
            return true;
        }
    }
    

    if the pair (pattern, string) is a one to one map , pattern and string would appear in the set and the bitmap at the same time.Or they don't appear at the same time.

    This is a wrong answer and an AC answer

    Test case:

    "aba"
    "b a a"

Log in to reply
 

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