-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountPY.java
More file actions
43 lines (35 loc) · 966 Bytes
/
CountPY.java
File metadata and controls
43 lines (35 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package basic;
public class CountPY {
public static boolean solution_first(String s) {
int pCount = 0;
int yCount = 0;
for (char c : s.toCharArray()) {
if (c == 'p' || c == 'P') {
pCount++;
continue;
}
if (c == 'y' || c == 'Y') {
yCount++;
}
}
if (pCount != yCount) {
return false;
}
return true;
}
public static boolean solution_2(String s) {
int pCount = 0;
int yCount = 0;
for (char c : s.toCharArray()) {
switch (c) {
case 'p','P'-> pCount++;
case 'y','Y'-> yCount++;
}
}
return pCount == yCount;
}
public static boolean solution(String s) {
s.toLowerCase();
return s.chars().filter(e -> 'p' == e).count() == s.chars().filter(e -> 'y' == e).count();
}
}