bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2185. Counting Words With a Given Prefix

2185. Counting Words With a Given Prefix

Leetcode Solutions

Iterative Prefix Matching

  1. Initialize a counter to 0.
  2. Iterate through each word in the words array.
  3. For each word, check if it starts with the pref prefix.
    • In Python, use word.startswith(pref).
    • In Java, use word.substring(0, pref.length()).equals(pref) if the word's length is at least as long as the prefix's length.
  4. If the word contains the prefix, increment the counter.
  5. After the loop, return the counter as it represents the number of words with the given prefix.
UML Thumbnail

Filter and Count Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR