Skip to content

Commit 48574da

Browse files
authored
Added tasks 290-918
1 parent 3ccc4ad commit 48574da

File tree

43 files changed

+1359
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1359
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# #Easy #String #Hash_Table #Data_Structure_II_Day_7_String #Top_Interview_150_Hashmap
2+
# #2025_09_20_Time_0_ms_(100.00%)_Space_17.99_MB_(15.86%)
3+
4+
from typing import Dict
5+
6+
class Solution:
7+
def wordPattern(self, pattern: str, s: str) -> bool:
8+
map: Dict[str, str] = {}
9+
words = s.split()
10+
if len(words) != len(pattern):
11+
return False
12+
for i in range(len(pattern)):
13+
if pattern[i] not in map:
14+
if words[i] in map.values():
15+
return False
16+
map[pattern[i]] = words[i]
17+
else:
18+
if words[i] != map[pattern[i]]:
19+
return False
20+
return True
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from Solution0290 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_wordPattern(self):
6+
self.assertTrue(Solution().wordPattern("abba", "dog cat cat dog"))
7+
8+
def test_wordPattern2(self):
9+
self.assertFalse(Solution().wordPattern("abba", "dog cat cat fish"))
10+
11+
def test_wordPattern3(self):
12+
self.assertFalse(Solution().wordPattern("aaaa", "dog cat cat dog"))
13+
14+
def test_wordPattern4(self):
15+
self.assertFalse(Solution().wordPattern("abba", "dog dog dog dog"))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
290\. Word Pattern
2+
3+
Easy
4+
5+
Given a `pattern` and a string `s`, find if `s` follows the same pattern.
6+
7+
Here **follow** means a full match, such that there is a bijection between a letter in `pattern` and a **non-empty** word in `s`.
8+
9+
**Example 1:**
10+
11+
**Input:** pattern = "abba", s = "dog cat cat dog"
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** pattern = "abba", s = "dog cat cat fish"
18+
19+
**Output:** false
20+
21+
**Example 3:**
22+
23+
**Input:** pattern = "aaaa", s = "dog cat cat dog"
24+
25+
**Output:** false
26+
27+
**Example 4:**
28+
29+
**Input:** pattern = "abba", s = "dog dog dog dog"
30+
31+
**Output:** false
32+
33+
**Constraints:**
34+
35+
* `1 <= pattern.length <= 300`
36+
* `pattern` contains only lower-case English letters.
37+
* `1 <= s.length <= 3000`
38+
* `s` contains only lower-case English letters and spaces `' '`.
39+
* `s` **does not contain** any leading or trailing spaces.
40+
* All the words in `s` are separated by a **single space**.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# #Medium #Array #Heap_Priority_Queue #Top_Interview_150_Heap
2+
# #2025_09_20_Time_78_ms_(92.40%)_Space_34.45_MB_(85.02%)
3+
4+
import heapq
5+
from typing import List
6+
7+
class Solution:
8+
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
9+
if not nums1 or not nums2:
10+
return []
11+
12+
heap = []
13+
result = []
14+
15+
# Initialize heap with first k pairs from nums1 and first element of nums2
16+
for i in range(min(len(nums1), k)):
17+
heapq.heappush(heap, (nums1[i] + nums2[0], i, 0))
18+
19+
# Extract k smallest pairs
20+
for _ in range(min(k, len(nums1) * len(nums2))):
21+
if not heap:
22+
break
23+
_, i, j = heapq.heappop(heap)
24+
result.append([nums1[i], nums2[j]])
25+
26+
# Add next pair from nums2 if available
27+
if j + 1 < len(nums2):
28+
heapq.heappush(heap, (nums1[i] + nums2[j + 1], i, j + 1))
29+
30+
return result
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from Solution0373 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_kSmallestPairs(self):
6+
self.assertEqual(
7+
Solution().kSmallestPairs([1, 7, 11], [2, 4, 6], 3),
8+
[[1, 2], [1, 4], [1, 6]]
9+
)
10+
11+
def test_kSmallestPairs2(self):
12+
self.assertEqual(
13+
Solution().kSmallestPairs([1, 1, 2], [1, 2, 3], 2),
14+
[[1, 1], [1, 1]]
15+
)
16+
17+
def test_kSmallestPairs3(self):
18+
self.assertEqual(
19+
Solution().kSmallestPairs([1, 2], [3], 3),
20+
[[1, 3], [2, 3]]
21+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
373\. Find K Pairs with Smallest Sums
2+
3+
Medium
4+
5+
You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
6+
7+
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
8+
9+
Return _the_ `k` _pairs_ <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> _with the smallest sums_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,7,11], nums2 = [2,4,6], k = 3
14+
15+
**Output:** [[1,2],[1,4],[1,6]]
16+
17+
**Explanation:** The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [1,1,2], nums2 = [1,2,3], k = 2
22+
23+
**Output:** [[1,1],[1,1]]
24+
25+
**Explanation:** The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = [1,2], nums2 = [3], k = 3
30+
31+
**Output:** [[1,3],[2,3]]
32+
33+
**Explanation:** All possible pairs are returned from the sequence: [1,3],[2,3]
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
38+
* <code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
39+
* `nums1` and `nums2` both are sorted in **ascending order**.
40+
* `1 <= k <= 1000`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# #Medium #Array #Hash_Table #Math #Design #Randomized #Programming_Skills_II_Day_20
2+
# #Top_Interview_150_Array/String #2025_09_20_Time_90_ms_(96.69%)_Space_57.80_MB_(64.58%)
3+
4+
import random
5+
6+
class RandomizedSet:
7+
def __init__(self):
8+
self.map = {} # value -> index
9+
self.list = [] # values
10+
11+
def insert(self, val: int) -> bool:
12+
if val in self.map:
13+
return False
14+
self.map[val] = len(self.list)
15+
self.list.append(val)
16+
return True
17+
18+
def remove(self, val: int) -> bool:
19+
if val not in self.map:
20+
return False
21+
22+
# Index of element to remove
23+
idx = self.map[val]
24+
last_val = self.list[-1]
25+
26+
# Swap with last element
27+
self.list[idx] = last_val
28+
self.map[last_val] = idx
29+
30+
# Remove last element
31+
self.list.pop()
32+
del self.map[val]
33+
34+
return True
35+
36+
def getRandom(self) -> int:
37+
return random.choice(self.list)
38+
39+
# Your RandomizedSet object will be instantiated and called as such:
40+
# obj = RandomizedSet()
41+
# param_1 = obj.insert(val)
42+
# param_2 = obj.remove(val)
43+
# param_3 = obj.getRandom()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
from RandomizedSet0380 import RandomizedSet
3+
4+
class RandomizedSetTest(unittest.TestCase):
5+
def test_randomizedSet(self):
6+
result = []
7+
randomized_set = None
8+
result.append(str(randomized_set))
9+
randomized_set = RandomizedSet()
10+
result.append(str(randomized_set.insert(1)))
11+
result.append(str(randomized_set.remove(2)))
12+
result.append(str(randomized_set.insert(2)))
13+
random_val = randomized_set.getRandom()
14+
result.append(str(random_val))
15+
result.append(str(randomized_set.remove(1)))
16+
result.append(str(randomized_set.insert(2)))
17+
result.append(str(randomized_set.getRandom()))
18+
19+
expected1 = ["None", "True", "False", "True", "1", "True", "False", "2"]
20+
expected2 = ["None", "True", "False", "True", "2", "True", "False", "2"]
21+
22+
if random_val == 1:
23+
self.assertEqual(result, expected1)
24+
else:
25+
self.assertEqual(result, expected2)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
380\. Insert Delete GetRandom O(1)
2+
3+
Medium
4+
5+
Implement the `RandomizedSet` class:
6+
7+
* `RandomizedSet()` Initializes the `RandomizedSet` object.
8+
* `bool insert(int val)` Inserts an item `val` into the set if not present. Returns `true` if the item was not present, `false` otherwise.
9+
* `bool remove(int val)` Removes an item `val` from the set if present. Returns `true` if the item was present, `false` otherwise.
10+
* `int getRandom()` Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the **same probability** of being returned.
11+
12+
You must implement the functions of the class such that each function works in **average** `O(1)` time complexity.
13+
14+
**Example 1:**
15+
16+
**Input**
17+
18+
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
19+
[[], [1], [2], [2], [], [1], [2], []]
20+
21+
**Output:** [null, true, false, true, 2, true, false, 2]
22+
23+
**Explanation:**
24+
25+
RandomizedSet randomizedSet = new RandomizedSet();
26+
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
27+
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
28+
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
29+
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
30+
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
31+
randomizedSet.insert(2); // 2 was already in the set, so return false.
32+
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
33+
34+
**Constraints:**
35+
36+
* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code>
37+
* At most `2 * `<code>10<sup>5</sup></code> calls will be made to `insert`, `remove`, and `getRandom`.
38+
* There will be **at least one** element in the data structure when `getRandom` is called.

0 commit comments

Comments
 (0)