-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestBalancedSubstring.java
More file actions
30 lines (30 loc) · 1.03 KB
/
LongestBalancedSubstring.java
File metadata and controls
30 lines (30 loc) · 1.03 KB
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
// 3713. Longest Balanced Substring I
import java.util.*;
class Solution {
public int longestBalanced(String s) {
ArrayList<Integer> list = new ArrayList<>();
int n = s.length();
for(int i = 0; i < n; i++){
HashMap<Character, Integer> map = new HashMap<>();
for(int j = i; j < n; j++){
Character ch = s.charAt(j);
map.put(ch, map.getOrDefault(ch, 0) + 1);
if (!map.isEmpty()) {
Integer firstValue = map.values().iterator().next();
boolean allSame = true;
for (Integer value : map.values()) {
if (!value.equals(firstValue)) {
allSame = false;
break;
}
}
if (allSame) {
list.add(j-i+1);
}
}
}
}
int maxLen = Collections.max(list);
return maxLen;
}
}