-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (33 loc) · 885 Bytes
/
Copy pathsolution.java
File metadata and controls
34 lines (33 loc) · 885 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
32
33
34
// 3. Longest Substring Without Repeating Characters
// https://leetcode.com/problems/longest-substring-without-repeating-characters/
// Medium | Java | Accepted 2022-08-07
// Runtime 241 ms | Memory 118 MB
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> joe = new HashMap<>();
int maxx = 0;
int m = 0;
int k = 0;
if(s.length()==1)
{
return 1;
}
while(k<s.length())
{
if(joe.containsKey(s.charAt(k)))
{
maxx = Math.max(maxx,k-m);
m = joe.get(s.charAt(k))+1;
joe.clear();
k = m;
}
else
{
joe.put(s.charAt(k),k);
k++;
}
}
maxx = Math.max(maxx, k-m);
return maxx;
}
}