forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1763.java
39 lines (37 loc) · 1.28 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
package com.fishercoder.solutions;
public class _1763 {
public static class Solution1 {
public String longestNiceSubstring(String s) {
String longest = "";
for (int i = 0; i < s.length() - 1; i++) {
for (int j = i; j <= s.length(); j++) {
if (isNiceString(s.substring(i, j))) {
if (longest.length() < j - i) {
longest = s.substring(i, j);
}
}
}
}
return longest;
}
private boolean isNiceString(String str) {
int[] uppercount = new int[26];
int[] lowercount = new int[26];
for (char c : str.toCharArray()) {
if (Character.isUpperCase(c)) {
uppercount[Character.toLowerCase(c) - 'a']++;
} else {
lowercount[c - 'a']++;
}
}
for (int i = 0; i < uppercount.length; i++) {
if (uppercount[i] > 0 && lowercount[i] > 0 || (uppercount[i] == 0 && lowercount[i] == 0)) {
continue;
} else {
return false;
}
}
return true;
}
}
}