LinkSearchMenuExpandDocument

1684. Count the Number of Consistent Strings

Solution Code

C#

publicclassSolution{publicintMarkCharacters(Stringword){intmarked=0;foreach(charchinword.ToCharArray()){intposition=ch-'a';marked|=1<<position;}returnmarked;}publicintCountConsistentStrings(stringallowed,string[]words){intmarkedAllowed=MarkCharacters(allowed);intanswer=0;foreach(Stringwordinwords){intmarkedWord=MarkCharacters(word);if((markedWord&markedAllowed)==markedWord)answer++;}returnanswer;}}

C++

classSolution{public:intmarkCharacters(string&word){intmarked=0;for(charch:word){intposition=ch-'a';marked|=1<<position;}returnmarked;}intcountConsistentStrings(stringallowed,vector<string>&words){intmarkedAllowed=markCharacters(allowed);intanswer=0;for(stringword:words){intmarkedWord=markCharacters(word);if((markedWord&markedAllowed)==markedWord)answer++;}returnanswer;}};

Java

classSolution{publicintmarkCharacters(Stringword){intmarked=0;for(charch:word.toCharArray()){intposition=Character.getNumericValue(ch);marked|=1<<position;}returnmarked;}publicintcountConsistentStrings(Stringallowed,String[]words){intmarkedAllowed=markCharacters(allowed);intanswer=0;for(Stringword:words){intmarkedWord=markCharacters(word);if((markedWord&markedAllowed)==markedWord)answer++;}returnanswer;}}

© 2023. All rights reserved.

close