Count the Number of Consistent Strings

You are given a string allowed made of distinct lowercase letters and an array of strings words. A word is consistent if every one of its characters appears in allowed. Return how many words in words are consistent.

30:00

Count the Number of Consistent Strings
easy
Free written solution
Topics
Companies

You are given a string allowed made of distinct lowercase letters and an array of strings words. A word is consistent if every one of its characters appears in allowed. Return how many words in words are consistent.

Example 1:
Input: {"allowed":"ab","words":["ad","bd","aaab","baa","badab"]}
Output: 2
Constraints:
  • 1≤words.length≤1041 \leq \text{words.length} \leq 10^4

  • 1≤allowed.length≤261 \leq \text{allowed.length} \leq 26

  • 1≤words[i].length≤101 \leq \text{words}[i]\text{.length} \leq 10

  • The characters in allowed are distinct.

  • words[i] and allowed contain only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"allowed":"ab","words":["ad","bd","aaab","baa","badab"]}

Turn allowed = "ab" into a 26-bit mask: bit k is 1 when the k-th letter of the alphabet is allowed.

mask built from allowed = "ab"

mask = 0

a

0

0

b

0

1

c

0

2

d

0

3

e

0

4

f

0

5

g

0

6

h

0

7

i

0

8

j

0

9

k

0

10

l

0

11

m

0

12

n

0

13

o

0

14

p

0

15

q

0

16

r

0

17

s

0

18

t

0

19

u

0

20

v

0

21

w

0

22

x

0

23

y

0

24

z

0

25

words: test each letter's bit

count = 0

a
d

b
d

a
a
a
b

b
a
a

b
a
d
a
b

yellow = letter being tested

red = bit is 0, word rejected

Variables
VariableValue
mask0
DepthFunction Call
Stack empty
0/25