You are given:
words
.pref
.Your task is to count how many strings in words
start with the prefix pref
.
A prefix of a string s
is defined as any leading contiguous substring of s
. For example:
Return the count of strings in words
that have pref
as their prefix.
Input:
words = ["apple", "apply", "application", "banana"]
pref = "app"
Output:
3
Explanation:
The strings that start with the prefix "app" are:
So, the result is 3
.
Input:
words = ["cat", "car", "dog", "cart"]
pref = "ca"
Output:
3
Explanation:
The strings that start with the prefix "ca" are:
So, the result is 3
.
Input:
words = ["hello", "world", "hi", "house"]
pref = "ho"
Output:
1
Explanation:
The only string that starts with the prefix "ho" is:
So, the result is 1
.
Write a function that takes the array words
and the string pref
as input and returns the count of strings in words
that start with the prefix pref
.
https://leetcode.com/problems/counting-words-with-a-given-prefix/description/
Loading component...
Loading component...
Main Function is not defined.
INPUT : words = ["apple", "apply", "application", "banana"] , pref = "app"
OUTPUT: 3
public static int countWordsWithPrefix( String[] words , String pref) {
int count = 0;
for ( String word: words) {
if (word.strartsWith(pref)) {
count++;
}//If End
}//Loop End
return count;
} //function end;
Utility Function is not required.