-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestSubstring.java
67 lines (58 loc) · 2.12 KB
/
longestSubstring.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// source: https://leetcode.com/problems/longest-substring-without-repeating-characters/
// Author: Chen
// Date: Jul-2-2024
/****************************************************************************************************************************
* Given a string s, find the length of the longest substring without repeating characters.
* The digits are stored in reverse order, and each of their nodes contains a single digit.
* Example 1:
* Input: s = "abcabcbb"
* Output: 3
* Explanation: The answer is "abc", with the length of 3.
**************************************************************************************************************************/
// Solution 1: use HashMap to store the character and its index,
// then update the start index of the substring when a repeating character is found
// O(n) time complexity, O(n) space complexity
class Solution {
public int lengthOfLongestSubstring(String s) {
int max = 0;
HashMap<Character,Integer> hashmap = new HashMap();
for(int i=0, j=0; i<s.length() ; i++ ){
if( hashmap.containsKey(s.charAt(i)) ){
j = Math.max(hashmap.get(s.charAt(i)) + 1, j);
}
hashmap.put(s.charAt(i), i);
max = Math.max(max, i - j + 1);
}
return max;
}
}
// Solution 2: Brute force, check all the substrings
// O(n^2) time complexity, O(1) space complexity
class Solution {
public int lengthOfLongestSubstring(String s) {
int p = 0 , q = 0;
int max = 0;
while(p< s.length()){
q = p + 1;
while(q < s.length()){
boolean flag = false;
for(int i = q - 1; i >= p ; i--){
if ( s.charAt(q) == s.charAt(i) ){
flag = true;
break;
}
}
if(flag){
break;
}else{
q++;
}
}
if(q-p > maxlen){
maxlen = q - p;
}
p++;
}
return max;
}
}