-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay59.java
More file actions
28 lines (23 loc) · 911 Bytes
/
Day59.java
File metadata and controls
28 lines (23 loc) · 911 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
import java.util.*;
public class Day59 {
public static int lengthOfLongestSubstring(String s) {
int n = s.length();
int maxLength = 0;
Map<Character, Integer> charIndexMap = new HashMap<>();
for (int start = 0, end = 0; end < n; end++) {
char currentChar = s.charAt(end);
if (charIndexMap.containsKey(currentChar)) {
start = Math.max(start, charIndexMap.get(currentChar) + 1);
}
maxLength = Math.max(maxLength, end - start + 1);
charIndexMap.put(currentChar, end);
}
return maxLength;
}
public static void main(String[] args) {
String input1 = "abcabcbb";
System.out.println("Sample Input 1: " + lengthOfLongestSubstring(input1));
String input2 = "aaaa";
System.out.println("Sample Input 2: " + lengthOfLongestSubstring(input2));
}
}