19ms C++ Code


  • 0
    W
    bool canConstruct(string ransomNote, string magazine) {
        	int lenA=ransomNote.length();
    int lenB=magazine.length();
    int  b[26];//0-25
    int  c[26];
    for(int i=0;i<26;i++){
    	b[i]=0;
    	c[i]=0;
    }
    //97-122
    
    for(int i=0;i<lenB;i++){
    	int temp=(int)magazine[i]-97;
    	b[temp]+=1;
    }
    for(int i=0;i<lenA;i++){
    int t=(int)ransomNote[i]-97;
    c[t]+=1;
    }
    
    bool flag=true;
    for(int i=0;i<26;i++){
    	
    	if(c[i]==b[i]||c[i]<b[i]){
    		continue;
    	}else{
    	    flag=false;
    		break;
    		
    	}
    	
    }
        
        return flag;
    }

Log in to reply
 

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