diff --git a/longest-substring-without-repeating-characters/taewanseoul.ts b/longest-substring-without-repeating-characters/taewanseoul.ts new file mode 100644 index 000000000..3f3fc63e7 --- /dev/null +++ b/longest-substring-without-repeating-characters/taewanseoul.ts @@ -0,0 +1,31 @@ +/** + * 3. Longest Substring Without Repeating Characters + * Given a string s, find the length of the longest substring without repeating characters. + * + * https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ + * + */ + +// O(n^2) time +// O(n) space +function lengthOfLongestSubstring(s: string): number { + let result = 0; + + for (let i = 0; i < s.length; i++) { + let set = new Set(); + let substring = 0; + + for (let j = i; j < s.length; j++) { + if (set.has(s[j])) { + break; + } + + set.add(s[j]); + substring++; + } + + result = Math.max(result, substring); + } + + return result; +} diff --git a/reverse-linked-list/taewanseoul.ts b/reverse-linked-list/taewanseoul.ts new file mode 100644 index 000000000..ebc73a220 --- /dev/null +++ b/reverse-linked-list/taewanseoul.ts @@ -0,0 +1,45 @@ +/** + * 206. Reverse Linked List + * Given the head of a singly linked list, reverse the list, and return the reversed list. + * + * https://leetcode.com/problems/reverse-linked-list/description/ + * + */ + +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +class ListNode { + val: number; + next: ListNode | null; + constructor(val?: number, next?: ListNode | null) { + this.val = val === undefined ? 0 : val; + this.next = next === undefined ? null : next; + } +} + +// O(n) time +// O(1) space +function reverseList(head: ListNode | null): ListNode | null { + let prev: ListNode | null = null; + let next: ListNode | null = null; + + while (head) { + next = head.next; + head.next = prev; + + prev = head; + head = next; + } + + return prev; +}