Counting Words With a Given Prefix

🏠 ⬅️ ➡️

You are 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, pref.length <= 100
  • words[i] and pref consist of lowercase English letters.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: n, i, count
    character(len=100) :: pref
    character(len=100), allocatable :: words(:)

    call get_args(n, words, pref)

    count = count_prefixes(words, pref)

    write (unit=*, fmt='(I0)') count

contains

    subroutine get_args(n, words, pref)
        implicit none
        integer, intent(out) :: n
        character(len=100), allocatable, intent(out) :: words(:)
        character(len=100), intent(out) :: pref

        character(len=100) :: word

        read (unit=*, fmt='(I0)') n
        allocate (words(n))
        do i = 1, n
            read (unit=*, fmt='(A)') word
            words(i) = word
        end do
        read (unit=*, fmt='(A)') pref
    end subroutine get_args

    function count_prefixes(words, pref) result(count)
        implicit none
        character(len=100), intent(in) :: words(:)
        character(len=100), intent(in) :: pref
        integer :: count

        integer :: i

        count = 0
        do i = 1, size(words)
            if (index(words(i), pref) == 1) then
                count = count + 1
            end if
        end do
    end function count_prefixes

end program main
Compiled
Executed
Correct
module prefix_count

implicit none

contains

integer function prefix_count(words, pref) result(count)

character(len=*), intent(in) :: words(:)
character(len=*), intent(in) :: pref

integer :: i

count = 0

do i = 1, size(words)
    if (index(words(i), pref) == 1) then
        count = count + 1
    end if
end do

end function prefix_count

end module prefix_count

program test

use prefix_count

implicit none

character(len=*), parameter :: words(4) = ["pay ", "attention ", "practice ", "attend "]
character(len=*), parameter :: pref = "at "

integer :: count

count = prefix_count(words, pref)

if (count == 2) then
    write (*,*) "Test 1: Passed"
else
    write (*,*) "Test 1: Failed"
end if

count = prefix_count(["leetcode ", "win ", "loops ", "success "], "code ")

if (count == 0) then
    write (*,*) "Test 2: Passed"
else
    write (*,*) "Test 2: Failed"
end if

end program test
🌐 Data from online sources
def count_prefixes(words, pref):
    return sum(1 for word in words if word.startswith(pref))

The algorithm iterates through the list of words and checks if the word starts with the given prefix. If it does, increment the count. Finally, return the count.

🌐 Data from online sources
int count_prefixes(vector<string>& words, string pref) {
    int count = 0;
    for(const auto& word : words) {
        if(word.find(pref) == 0) {
            count++;
        }
    }
    return count;
}

The algorithm iterates through the list of words and checks if the word starts with the given prefix. If it does, increment the count. Finally, return the count.