#2255

Count Prefixes of a Given String

newbie · 240 · lc easy +18 · verified · 74.2% accepted · 604 likes · top 85%

Description

You are given a string array words and a string s, all consisting of lowercase English letters.

Return the count of strings in words that are a prefix of s.

A prefix of a string is any leading substring starting at index 0.

Example 1:

Input: words = ["a","b","c","ab","bc","abc"], s = "abc"
Output: 3
Explanation:
The strings in words which are a prefix of s = "abc" are:
"a", "ab", and "abc".
Thus the number of strings in words which are a prefix of s is 3.

Example 2:

Input: words = ["a","a"], s = "aa"
Output: 2
Explanation:
Both of the strings are a prefix of s.
Note that the same string can occur multiple times in words, and it should be counted each time.

Code

1
2
3