Skip to content

Commit 60b521c

Browse files
Added Longest Substring Problem
1 parent a9ed4fc commit 60b521c

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Given a string s, find the length of the longest substring without repeating characters.
3+
4+
Example 1:
5+
6+
Input: s = "abcabcbb"
7+
Output: 3
8+
Explanation: The answer is "abc", with the length of 3.
9+
Example 2:
10+
11+
Input: s = "bbbbb"
12+
Output: 1
13+
Explanation: The answer is "b", with the length of 1.
14+
Example 3:
15+
16+
Input: s = "pwwkew"
17+
Output: 3
18+
Explanation: The answer is "wke", with the length of 3.
19+
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
20+
21+
22+
Constraints:
23+
24+
0 <= s.length <= 5 * 104
25+
s consists of English letters, digits, symbols and spaces.
26+
*/
27+
28+
/**
29+
* @param {string} s
30+
* @return {number}
31+
*/
32+
var lengthOfLongestSubstring = function(s) {
33+
if(s.length == 0) { return 0 }
34+
35+
var repeatedChars = new Set();
36+
var maxLength = 1;
37+
var currentMaxLength = 1;
38+
var start = 0;
39+
var end = 0;
40+
repeatedChars.add(s.charAt(start));
41+
42+
while(end + 1 < s.length && start < s.length) {
43+
if(repeatedChars.has(s.charAt(end + 1))) {
44+
if(repeatedChars.has(s.charAt(start))) {
45+
currentMaxLength--;
46+
repeatedChars.delete(s.charAt(start))
47+
}
48+
start++;
49+
} else {
50+
repeatedChars.add(s.charAt(end + 1));
51+
currentMaxLength++;
52+
if(currentMaxLength > maxLength) {
53+
maxLength = currentMaxLength;
54+
}
55+
end++;
56+
}
57+
}
58+
59+
return maxLength;
60+
};
61+
62+
module.exports.lengthOfLongestSubstring = lengthOfLongestSubstring;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const assert = require('assert');
2+
const lengthOfLongestSubstring = require('../../LeetcodeProblems/Algorithms/Longest_Substring').lengthOfLongestSubstring;
3+
4+
function test() {
5+
assert.equal(4, lengthOfLongestSubstring("abcdbcd"));
6+
assert.equal(0, lengthOfLongestSubstring(""));
7+
assert.equal(1, lengthOfLongestSubstring("b"));
8+
assert.equal(1, lengthOfLongestSubstring("bb"));
9+
assert.equal(3, lengthOfLongestSubstring("bbacb"));
10+
}
11+
12+
module.exports.test = test;

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
3636
| [Kth Largest Element in an Array ](/LeetcodeProblems/Algorithms/Kth_Largest_Element_in_an_Array.js) | Medium | https://leetcode.com/problems/kth-largest-element-in-an-array/ |
3737
| [Linked List Cycle II ](/LeetcodeProblems/Algorithms/Linked_List_Cycle_II.js) | Medium | https://leetcode.com/problems/linked-list-cycle-ii/ |
3838
| [Longest Palindromic Substring ](/LeetcodeProblems/Algorithms/Longest_Palindromic_Substring.js) | Medium | https://leetcode.com/problems/longest-palindromic-substring/|
39-
| [Max Area Of Island ](/LeetcodeProblems/Algorithms/Max_Area_Of_Island.js) | Medium | https://leetcode.com/problems/max-area-of-island/ |
39+
| [Longest Substring Without Reapeating Characters](/LeetcodeProblems/Algorithms/Longest_Substring.js) | Medium | https://leetcode.com/problems/longest-substring-without-repeating-characters|
40+
| [Max Area Of Island ](/LeetcodeProblems/Algorithms/Max_Area_Of_Island.js) | Medium | https://leetcode.com/problems/max-area-of-island/ |
4041
| [Maximal Square ](/LeetcodeProblems/Algorithms/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/ |
4142
| [Permutations ](/LeetcodeProblems/Algorithms/Permutations.js) | Medium | https://leetcode.com/problems/permutations/ |
4243
| [Permutations II ](/LeetcodeProblems/Algorithms/Permutations_II.js) | Medium | https://leetcode.com/problems/permutations-ii/ |

0 commit comments

Comments
 (0)