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.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
temp.f95:24:30: 24 | read (unit=*, fmt='(I0)') n | 1 Error: Positive width required in format string at (1)
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
temp.f95:7:29: 7 | integer function prefix_count(words, pref) result(count) | 1 Error: MODULE attribute of ‘prefix_count’ conflicts with PROCEDURE attribute at (1) temp.f95:9:40: 9 | character(len=*), intent(in) :: words(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:36: 10 | character(len=*), intent(in) :: pref | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:12: 12 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:9: 14 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:21: 16 | do i = 1, size(words) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:40: 17 | if (index(words(i), pref) == 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:25: 18 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:7: 19 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:3: 20 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:3: 22 | end function prefix_count | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:5: 28 | use prefix_count | 1 Fatal Error: Cannot open module file ‘prefix_count.mod’ for reading at (1): No such file or directory compilation terminated.
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.
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.