Problem:
Given an array of strings, return the count of the number of strings with the given length.
wordsCount({"a", "bb", "b", "ccc"}, 1) → 2
wordsCount({"a", "bb", "b", "ccc"}, 3) → 1
wordsCount({"a", "bb", "b", "ccc"}, 4) → 0
Solution:
1 | public int wordsCount(String[] words, int len) { |
2 | int count = 0 ; |
3 | for ( int i = 0 ; i < words.length; i++) { |
4 | if (words[i].length() == len) |
5 | count++; |
6 | } |
7 | return count; |
8 | } |
int count=0;
ReplyDeletefor(String w : words){
if(w.length()==len) count++;
}
return count;
public int wordsCount(String[] words, int len) {
ReplyDeleteint count = 0;
for (final String word : words) {
if (word.length() == len) {
count++;
}
}
return count;
}
public int wordsCount(String[] words, int len) {
ReplyDeleteint c=0;
for(int i=0;i<words.length;i++)
if(words[i].length()==len)
c++;
return c;
}
public int wordsCount(String[] words, int len)
ReplyDelete{
int counter = 0;
for(int i = 0; i < words.length; i++)
{
if(words[i].length() == len) counter++;
}
return counter;
}