DDSA Solutions

2185. Counting Words With a Given Prefix

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

Problem Overview

Counting Words With a Given Prefix (Easy) asks you to solve a structured algorithmic task. This is a common Array / String pattern in coding interviews. Count words where w.StartsWith(pref) is true.

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

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

Related patterns: Array, String, Prefix Sum

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));
    }
}
Was this solution helpful?

Related Problems