DDSA
Advertisement

2185. Counting Words With a Given Prefix

Time: O(n * p)
Space: O(1)

Approach

Count words where w.StartsWith(pref) is true.

2185.cs
C#
// Approach: Count words where w.StartsWith(pref) is true.
// Time: O(n * p) Space: O(1)

public class Solution
{
    public int PrefixCount(string[] words, string pref)
    {
        return words.Count(w => w.StartsWith(pref));
    }
}
Advertisement
Was this solution helpful?