Counting Words With a Given Prefix

You are given an array of strings words and a string pref. Return the number of strings in words that contain pref as a prefix. A prefix of a string s is any leading contiguous substring of s.

30:00

Counting Words With a Given Prefix
easy
Free written solution
Topics
Companies

You are given an array of strings words and a string pref. Return the number of strings in words that contain pref as a prefix. A prefix of a string s is any leading contiguous substring of s.

Example 1:
Input: {"words":["pay","attention","practice","attend"],"pref":"at"}
Output: 2
Constraints:
  • 1≤words.length≤1001 \leq \text{words.length} \leq 100

  • 1≤words[i].length,pref.length≤1001 \leq \text{words}[i].\text{length}, \text{pref.length} \leq 100

  • words[i]\text{words}[i] and pref\text{pref} consist of lowercase English letters.

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

Run code to see results.

Input
arr ={"words":["pay","attention","practice","attend"],"pref":"at"}

Slide the stencil "at" over the start of each of the 4 words. count starts at 0.

stencil (pref)

a
t

count

0

words

0

p
a
y

1

a
t
t
e
n
t
i
o
n

2

p
r
a
c
t
i
c
e

3

a
t
t
e
n
d

Variables
VariableValue
prefat
count0
DepthFunction Call
Stack empty
0/15