From af7cfa24dd0b1e956e5e0138cb755572b3d99326 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 27 Aug 2023 20:23:43 +0300 Subject: [PATCH] Added tasks 9, 10 --- README.md | 40 ++++---- .../ts/g0001_0100/s0001_two_sum/readme.md | 16 ++-- .../s0002_add_two_numbers/readme.md | 12 +-- .../readme.md | 5 +- .../s0009_palindrome_number/readme.md | 64 +++++++++++++ .../readme.md | 92 +++++++++++++++++++ 6 files changed, 195 insertions(+), 34 deletions(-) create mode 100644 src/main/ts/g0001_0100/s0009_palindrome_number/readme.md create mode 100644 src/main/ts/g0001_0100/s0010_regular_expression_matching/readme.md diff --git a/README.md b/README.md index 16d0e8b..a784d22 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ | | | | | | |-|-|-|-|-|- -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 66 | 93.20 +| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 51 | 97.89 #### Day 3 Array @@ -136,13 +136,13 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 660 | 36.76 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 480 | 35.37 #### Day 10 Linked List | | | | | | |-|-|-|-|-|- -| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 106 | 79.11 +| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 88 | 93.29 #### Day 11 Linked List @@ -230,7 +230,7 @@ | | | | | | |-|-|-|-|-|- -| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 95 | 70.71 +| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 81 | 68.46 #### Day 7 Breadth First Search Depth First Search @@ -343,7 +343,7 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 660 | 36.76 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 480 | 35.37 #### Day 15 Dynamic Programming @@ -630,7 +630,7 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 660 | 36.76 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 480 | 35.37 #### Day 18 @@ -790,7 +790,7 @@ | | | | | | |-|-|-|-|-|- -| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 106 | 79.11 +| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 88 | 93.29 #### Day 16 @@ -1007,7 +1007,7 @@ | | | | | | |-|-|-|-|-|- -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 66 | 93.20 +| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 51 | 97.89 #### Day 14 Stack @@ -1090,7 +1090,7 @@ | | | | | | |-|-|-|-|-|- -| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 95 | 70.71 +| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 81 | 68.46 #### Day 15 Tree @@ -1129,13 +1129,14 @@ | | | | | | |-|-|-|-|-|- | 0007 |[Reverse Integer](src/main/ts/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 56 | 95.68 +| 0009 |[Palindrome Number](src/main/ts/g0001_0100/s0009_palindrome_number)| Easy | Math | 137 | 88.64 #### Udemy Strings | | | | | | |-|-|-|-|-|- -| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 95 | 70.71 -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 660 | 36.76 +| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 81 | 68.46 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 480 | 35.37 #### Udemy Binary Search @@ -1146,7 +1147,7 @@ | | | | | | |-|-|-|-|-|- -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 66 | 93.20 +| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 51 | 97.89 #### Udemy Two Pointers @@ -1192,6 +1193,7 @@ | | | | | | |-|-|-|-|-|- +| 0010 |[Regular Expression Matching](src/main/ts/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion | 69 | 92.65 #### Udemy Backtracking/Recursion @@ -1212,12 +1214,14 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 0010 |[Regular Expression Matching](src/main/ts/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion, Udemy_Dynamic_Programming | 69 | 92.65 +| 0009 |[Palindrome Number](src/main/ts/g0001_0100/s0009_palindrome_number)| Easy | Math, Udemy_Integers | 137 | 88.64 | 0008 |[String to Integer (atoi)](src/main/ts/g0001_0100/s0008_string_to_integer_atoi)| Medium | Top_Interview_Questions, String | 56 | 99.01 | 0007 |[Reverse Integer](src/main/ts/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math, Udemy_Integers | 56 | 95.68 -| 0006 |[Zigzag Conversion](src/main/ts/g0001_0100/s0006_zigzag_conversion)| Medium | String | 80 | 93.13 -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Data_Structure_II_Day_9_String, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_17, Udemy_Strings | 660 | 36.76 -| 0004 |[Median of Two Sorted Arrays](src/main/ts/g0001_0100/s0004_median_of_two_sorted_arrays)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer | 93 | 97.16 -| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Algorithm_I_Day_6_Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Udemy_Strings | 95 | 70.71 -| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Data_Structure_II_Day_10_Linked_List, Programming_Skills_II_Day_15 | 106 | 79.11 -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array, Level_1_Day_13_Hashmap, Udemy_Arrays | 66 | 93.20 +| 0006 |[Zigzag Conversion](src/main/ts/g0001_0100/s0006_zigzag_conversion)| Medium | String | 64 | 95.38 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Data_Structure_II_Day_9_String, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_17, Udemy_Strings | 480 | 35.37 +| 0004 |[Median of Two Sorted Arrays](src/main/ts/g0001_0100/s0004_median_of_two_sorted_arrays)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer | 86 | 92.15 +| 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Algorithm_I_Day_6_Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Udemy_Strings | 81 | 68.46 +| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Data_Structure_II_Day_10_Linked_List, Programming_Skills_II_Day_15 | 88 | 93.29 +| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array, Level_1_Day_13_Hashmap, Udemy_Arrays | 51 | 97.89 diff --git a/src/main/ts/g0001_0100/s0001_two_sum/readme.md b/src/main/ts/g0001_0100/s0001_two_sum/readme.md index d33a903..cdc7a3d 100644 --- a/src/main/ts/g0001_0100/s0001_two_sum/readme.md +++ b/src/main/ts/g0001_0100/s0001_two_sum/readme.md @@ -17,28 +17,28 @@ You can return the answer in any order. **Output:** [0,1] -**Output:** Because nums[0] + nums[1] == 9, we return [0, 1]. +**Explanation:** Because nums[0] + nums[1] == 9, we return [0, 1]. **Example 2:** **Input:** nums = [3,2,4], target = 6 -**Output:** [1,2] +**Output:** [1,2] **Example 3:** **Input:** nums = [3,3], target = 6 -**Output:** [0,1] +**Output:** [0,1] **Constraints:** -- 2 <= nums.length <= 104 -- -109 <= nums[i] <= 109 -- -109 <= target <= 109 -- **Only one valid answer exists.** +* 2 <= nums.length <= 104 +* -109 <= nums[i] <= 109 +* -109 <= target <= 109 +* **Only one valid answer exists.** -**Follow-up:** Can you come up with an algorithm that is less than O(n2) time complexity? +**Follow-up:** Can you come up with an algorithm that is less than O(n2) time complexity? ## Solution diff --git a/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md b/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md index 6b24201..cdae041 100644 --- a/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md +++ b/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md @@ -17,25 +17,25 @@ You may assume the two numbers do not contain any leading zero, except the numbe **Output:** [7,0,8] -**Explanation:** 342 + 465 = 807. +**Explanation:** 342 + 465 = 807. **Example 2:** **Input:** l1 = [0], l2 = [0] -**Output:** [0] +**Output:** [0] **Example 3:** **Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] -**Output:** [8,9,9,9,0,0,0,1] +**Output:** [8,9,9,9,0,0,0,1] **Constraints:** -- The number of nodes in each linked list is in the range `[1, 100]`. -- `0 <= Node.val <= 9` -- It is guaranteed that the list represents a number that does not have leading zeros. +* The number of nodes in each linked list is in the range `[1, 100]`. +* `0 <= Node.val <= 9` +* It is guaranteed that the list represents a number that does not have leading zeros. ## Solution diff --git a/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md b/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md index 233e0c4..9ddee15 100644 --- a/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md +++ b/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md @@ -29,7 +29,9 @@ Given a string `s`, find the length of the **longest** **substring** without rep **Output:** 3 -**Explanation:** The answer is "wke", with the length of 3. +**Explanation:** + +The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. @@ -46,7 +48,6 @@ function lengthOfLongestSubstring(s: string): number { let maxLength = 0 let length = 0 let start = 0 - for (let i = 0; i < s.length; i++) { const char = s[i] if (hash[char] !== undefined && hash[char] >= start) { diff --git a/src/main/ts/g0001_0100/s0009_palindrome_number/readme.md b/src/main/ts/g0001_0100/s0009_palindrome_number/readme.md new file mode 100644 index 0000000..b4a08f9 --- /dev/null +++ b/src/main/ts/g0001_0100/s0009_palindrome_number/readme.md @@ -0,0 +1,64 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript) +[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork) + +## 9\. Palindrome Number + +Easy + +Given an integer `x`, return `true` if `x` is palindrome integer. + +An integer is a **palindrome** when it reads the same backward as forward. For example, `121` is palindrome while `123` is not. + +**Example 1:** + +**Input:** x = 121 + +**Output:** true + +**Example 2:** + +**Input:** x = -121 + +**Output:** false + +**Explanation:** From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. + +**Example 3:** + +**Input:** x = 10 + +**Output:** false + +**Explanation:** Reads 01 from right to left. Therefore it is not a palindrome. + +**Example 4:** + +**Input:** x = -101 + +**Output:** false + +**Constraints:** + +* -231 <= x <= 231 - 1 + +**Follow up:** Could you solve it without converting the integer to a string? + +## Solution + +```typescript +function isPalindrome(x: number): boolean { + if (x < 0) { + return false + } + let rev: number = 0 + let localX: number = x + while (localX > 0) { + rev *= 10 + rev += localX % 10 + localX = Math.floor(localX / 10) + } + return rev === x +} + +export { isPalindrome } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0010_regular_expression_matching/readme.md b/src/main/ts/g0001_0100/s0010_regular_expression_matching/readme.md new file mode 100644 index 0000000..72b609d --- /dev/null +++ b/src/main/ts/g0001_0100/s0010_regular_expression_matching/readme.md @@ -0,0 +1,92 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript) +[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork) + +## 10\. Regular Expression Matching + +Hard + +Given an input string `s` and a pattern `p`, implement regular expression matching with support for `'.'` and `'*'` where: + +* `'.'` Matches any single character. +* `'*'` Matches zero or more of the preceding element. + +The matching should cover the **entire** input string (not partial). + +**Example 1:** + +**Input:** s = "aa", p = "a" + +**Output:** false + +**Explanation:** "a" does not match the entire string "aa". + +**Example 2:** + +**Input:** s = "aa", p = "a\*" + +**Output:** true + +**Explanation:** '\*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". + +**Example 3:** + +**Input:** s = "ab", p = ".\*" + +**Output:** true + +**Explanation:** ".\*" means "zero or more (\*) of any character (.)". + +**Example 4:** + +**Input:** s = "aab", p = "c\*a\*b" + +**Output:** true + +**Explanation:** c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab". + +**Example 5:** + +**Input:** s = "mississippi", p = "mis\*is\*p\*." + +**Output:** false + +**Constraints:** + +* `1 <= s.length <= 20` +* `1 <= p.length <= 30` +* `s` contains only lowercase English letters. +* `p` contains only lowercase English letters, `'.'`, and `'*'`. +* It is guaranteed for each appearance of the character `'*'`, there will be a previous valid character to match. + +## Solution + +```typescript +function isMatch(s: string, p: string): boolean { // NOSONAR + const result = new Array(s.length + 1) + for (let i = 0; i < result.length; i++) { + result[i] = new Array(p.length + 1).fill(false) + } + result[0][0] = true + for (let j = 1; j < result[0].length; j++) { + const pChar = p[j - 1] + if (pChar === '*') { + result[0][j] = result[0][j - 2] + } + } + for (let i = 1; i < result.length; i++) { + for (let j = 1; j < result[0].length; j++) { + const sChar = s[i - 1] + const pChar = p[j - 1] + if (sChar === pChar || pChar === '.') { + result[i][j] = result[i - 1][j - 1] + } else if (pChar === '*') { + const prevCharMatch = sChar === p[j - 2] || p[j - 2] === '.' ? result[i - 1][j] : false + result[i][j] = result[i][j - 2] || prevCharMatch + } + } + } + return result[result.length - 1][result[0].length - 1] +} + +export { isMatch } +``` \ No newline at end of file