Skip to content

Latest commit

 

History

History
76 lines (60 loc) · 1.77 KB

_1684. Count the Number of Consistent Strings.md

File metadata and controls

76 lines (60 loc) · 1.77 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 06, 2024

Last updated : July 01, 2024


Related Topics : Array, Hash Table, String, Bit Manipulation

Acceptance Rate : 83.341 %


Solutions

C

// Pure brute force
int countConsistentStrings(char * allowed, char ** words, int wordsSize){
    int output = 0;

    for (int i = 0; i < wordsSize; i++) {
        char* word = words[i];
        
        bool failed = true;
        while (*word) {
            failed = true;
            char* allowedPtr = allowed;
            while (*allowedPtr) {
                if (*allowedPtr == *word) {
                    failed = false;
                    break;
                }
                allowedPtr++;
            }
            if (failed) {
                break;
            }
            word++;
        }
        if (!failed) {
            output++;
        }
    }

    return output;
}

Python

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        allowed = set(allowed)

        output = len(words)
        for word in words :
            for c in word :
                if c not in allowed :
                    output -= 1
                    break

        return output