Home > Backend Development > PHP Tutorial > Counting Words With a Given Prefix

Counting Words With a Given Prefix

Susan Sarandon
Release: 2025-01-09 18:03:42
Original
614 people have browsed it
<code class="language-php"><?php
/**
 * @param String[] $words
 * @param String $pref
 * @return Integer
 */
function countWordsWithPrefix($words, $pref) {
    $count = 0;
    foreach ($words as $word) {
        if (strpos($word, $pref) === 0) {
            $count++;
        }
    }
    return $count;
}

// Example Usage
$words1 = ["pay", "attention", "practice", "attend"];
$pref1 = "at";
echo countWordsWithPrefix($words1, $pref1); // Output: 2

$words2 = ["leetcode", "win", "loops", "success"];
$pref2 = "code";
echo countWordsWithPrefix($words2, $pref2); // Output: 0
?></code>
Copy after login

Counting Words With a Given Prefix

  1. Counting Words With a Given Prefix

Difficulty: Easy

Topics: Array, String, String Matching

Given an array of strings words and a string pref, return the number of strings in words that contain pref as a prefix.

A prefix of a string s is any leading contiguous substring of s.

Example 1:

  • Input: words = ["pay","attention","practice","attend"], pref = "at"
  • Output: 2
  • Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".

Example 2:

  • Input: words = ["leetcode","win","loops","success"], pref = "code"
  • Output: 0
  • Explanation: There are no strings that contain "code" as a prefix.

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • 1 <= pref.length <= 20
  • words[i] and pref consist of lowercase English letters.

Improved Solution (using strpos):

The provided solution uses substr which is less efficient than strpos for this specific task. strpos directly checks for the prefix at the beginning of the string, avoiding unnecessary substring creation.

This improved PHP solution uses strpos:

<?php
function countWordsWithPrefix(array $words, string $pref): int {
    $count = 0;
    foreach ($words as $word) {
        if (strpos($word, $pref) === 0) { // Check if pref is at the beginning (index 0)
            $count++;
        }
    }
    return $count;
}
?>
<p><strong>Time Complexity:</strong> O(n*m) in the worst case, where n is the number of words and m is the length of the prefix.  However, on average, it will be faster than the original <code>substr solution.

Space Complexity: O(1) - Constant extra space is used.

This revised answer provides a more efficient solution and maintains the clarity of the explanation. The image remains unchanged as it's relevant to the problem statement.

The above is the detailed content of Counting Words With a Given Prefix. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template