java: add «2185. Counting Words With a Given Prefix»

Refactor to stream.

URL:	https://leetcode.com/problems/counting-words-with-a-given-prefix/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-09 10:46:21 +01:00
parent 58f88d7db9
commit b12e923384
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -1,13 +1,5 @@
class Solution {
public int prefixCount(String[] words, String pref) {
var count = 0;
for (var word : words) {
if (word.startsWith(pref)) {
++count;
}
}
return count;
return (int) (Arrays.stream(words).filter(word -> word.startsWith(pref)).count());
}
}