Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ganu] Week 07 #929

Merged
merged 7 commits into from
Jan 24, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: 3. Longest Substring Without Repeating Characters
gwbaik9717 committed Jan 20, 2025
commit 516540ef8e85bcf56bccaaa348c99b55865a55b9
30 changes: 30 additions & 0 deletions longest-substring-without-repeating-characters/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// n: len(s)
// Time complexity: O(n^2)
// Space complexity: O(n)

/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
let answer = 0;
const map = new Map();

for (let i = 0; i < s.length; i++) {
const chr = s[i];

if (map.has(chr)) {
const temp = map.get(chr);
for (const [key, value] of map) {
if (value <= temp) {
map.delete(key);
}
}
}

map.set(chr, i);
answer = Math.max(answer, map.size);
}

return answer;
};