-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1763.java
More file actions
31 lines (29 loc) · 973 Bytes
/
prblm1763.java
File metadata and controls
31 lines (29 loc) · 973 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
import java.util.*;
public class prblm1763 {
public static void main(String[] args) {
String s = "YazaAay";
System.out.println(new prblm1763().longestNiceSubstring(s));
}
public String longestNiceSubstring(String s) {
int k = -1, max = 0;
for (int i = 0; i < s.length(); i++) {
Set<Character> set = new HashSet<>();
for (int j = i; j < s.length(); j++) {
set.add(s.charAt(j));
boolean ok = true;
for (char ch : set) {
char b = (char) (ch ^ 32);
if (!(set.contains(ch) && set.contains(b))) {
ok = false;
break;
}
}
if (ok && max < j - i + 1) {
max = j - i + 1;
k = i;
}
}
}
return k == -1 ? "" : s.substring(k, k + max);
}
}