- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1763.java
40 lines (38 loc) · 1.32 KB
/
_1763.java
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
packagecom.fishercoder.solutions.secondthousand;
publicclass_1763 {
publicstaticclassSolution1 {
publicStringlongestNiceSubstring(Strings) {
Stringlongest = "";
for (inti = 0; i < s.length() - 1; i++) {
for (intj = i; j <= s.length(); j++) {
if (isNiceString(s.substring(i, j))) {
if (longest.length() < j - i) {
longest = s.substring(i, j);
}
}
}
}
returnlongest;
}
privatebooleanisNiceString(Stringstr) {
int[] uppercount = newint[26];
int[] lowercount = newint[26];
for (charc : str.toCharArray()) {
if (Character.isUpperCase(c)) {
uppercount[Character.toLowerCase(c) - 'a']++;
} else {
lowercount[c - 'a']++;
}
}
for (inti = 0; i < uppercount.length; i++) {
if (uppercount[i] > 0 && lowercount[i] > 0
|| (uppercount[i] == 0 && lowercount[i] == 0)) {
continue;
} else {
returnfalse;
}
}
returntrue;
}
}
}