Count Prefixes of a Given String

You are given a string array words and a string s, where words[i] and s comprise only of lowercase English letters.

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

A prefix of a string is a substring that occurs at the beginning of the string. A substring is a contiguous sequence of characters within a string.

Example 1
Inputwords = ["a","b","c","ab","bc","abc"], s = "abc"
Output3
The strings in words which are a prefix of s = "abc" are "a", "ab", and "abc", so the count is 3.
Example 2
Inputwords = ["a","a"], s = "aa"
Output2
Both strings are a prefix of s, and the same string can occur multiple times in words and should be counted each time.

Constraints

  • 1 <= words.length <= 1000
  • 1 <= words[i].length, s.length <= 10
  • words[i] and s consist of lowercase English letters only.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate