-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2981.java
More file actions
49 lines (41 loc) · 1.37 KB
/
LC2981.java
File metadata and controls
49 lines (41 loc) · 1.37 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* LC2981
*/
import java.util.*;
public class LC2981 {
public static int maximumLength(String s) {
HashMap<String, Integer> map = new HashMap<>();
int n = s.length();
// Generate all special substrings and store in map
for (int i = 0; i < n; i++) {
StringBuilder curString = new StringBuilder();
for (int j = i; j < n; j++) {
if (curString.length() == 0 || s.charAt(j) == curString.charAt(curString.length() - 1)) {
curString.append(s.charAt(j));
String cur = curString.toString();
map.put(cur, map.getOrDefault(cur, 0) + 1);
} else {
break;
}
}
}
int maxLength = -1;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String str = entry.getKey();
int freq = entry.getValue();
if (freq > 2) {
maxLength = Math.max(maxLength, str.length());
}
}
return maxLength;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The String Here : ");
String str = sc.nextLine();
System.out.println();
int ans = maximumLength(str);
System.out.println(ans);
sc.close();
}
}