Skip to content

Commit 3ccc4ad

Browse files
authored
Added tasks 222-289
1 parent d515509 commit 3ccc4ad

File tree

16 files changed

+383
-0
lines changed

16 files changed

+383
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
393393
| 0020 |[Valid Parentheses](src/main/python/g0001_0100/s0020_valid_parentheses/Solution0020.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
394394
| 0005 |[Longest Palindromic Substring](src/main/python/g0001_0100/s0005_longest_palindromic_substring/Solution0005.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 47 | 97.38
395395
| 0394 |[Decode String](src/main/python/g0301_0400/s0394_decode_string/Solution0394.py)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, LeetCode_75_Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
396+
| 0242 |[Valid Anagram](src/main/python/g0201_0300/s0242_valid_anagram/Solution0242.py)| Easy | String, Hash_Table, Sorting | 11 | 72.46
396397
| 0049 |[Group Anagrams](src/main/python/g0001_0100/s0049_group_anagrams/Solution0049.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 11 | 82.75
397398
| 0151 |[Reverse Words in a String](src/main/python/g0101_0200/s0151_reverse_words_in_a_string/Solution0151.py)| Medium | String, Two_Pointers, LeetCode_75_Array/String | 0 | 100.00
398399

@@ -553,6 +554,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
553554
| 0122 |[Best Time to Buy and Sell Stock II](src/main/python/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii/Solution0122.py)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 0 | 100.00
554555
| 0055 |[Jump Game](src/main/python/g0001_0100/s0055_jump_game/Solution0055.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 12 | 85.38
555556
| 0045 |[Jump Game II](src/main/python/g0001_0100/s0045_jump_game_ii/Solution0045.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 8 | 57.34
557+
| 0274 |[H-Index](src/main/python/g0201_0300/s0274_h_index/Solution0274.py)| Medium | Array, Sorting, Counting_Sort | 0 | 100.00
556558
| 0238 |[Product of Array Except Self](src/main/python/g0201_0300/s0238_product_of_array_except_self/Solution0238.py)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, LeetCode_75_Array/String, Big_O_Time_O(n^2)_Space_O(n) | 15 | 97.12
557559
| 0134 |[Gas Station](src/main/python/g0101_0200/s0134_gas_station/Solution0134.py)| Medium | Top_Interview_Questions, Array, Greedy | 15 | 88.80
558560
| 0135 |[Candy](src/main/python/g0101_0200/s0135_candy/Solution0135.py)| Hard | Array, Greedy | 7 | 97.91
@@ -592,12 +594,14 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
592594
| 0054 |[Spiral Matrix](src/main/python/g0001_0100/s0054_spiral_matrix/Solution0054.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation | 0 | 100.00
593595
| 0048 |[Rotate Image](src/main/python/g0001_0100/s0048_rotate_image/Solution0048.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
594596
| 0073 |[Set Matrix Zeroes](src/main/python/g0001_0100/s0073_set_matrix_zeroes/Solution0073.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 3 | 71.07
597+
| 0289 |[Game of Life](src/main/python/g0201_0300/s0289_game_of_life/Solution0289.py)| Medium | Array, Matrix, Simulation | 0 | 100.00
595598

596599
#### Top Interview 150 Hashmap
597600

598601
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
599602
|-|-|-|-|-|-
600603
| 0205 |[Isomorphic Strings](src/main/python/g0201_0300/s0205_isomorphic_strings/Solution0205.py)| Easy | String, Hash_Table | 3 | 95.32
604+
| 0242 |[Valid Anagram](src/main/python/g0201_0300/s0242_valid_anagram/Solution0242.py)| Easy | String, Hash_Table, Sorting | 11 | 72.46
601605
| 0049 |[Group Anagrams](src/main/python/g0001_0100/s0049_group_anagrams/Solution0049.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 11 | 82.75
602606
| 0001 |[Two Sum](src/main/python/g0001_0100/s0001_two_sum/Solution0001.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 0 | 100.00
603607
| 0202 |[Happy Number](src/main/python/g0201_0300/s0202_happy_number/Solution0202.py)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 0 | 100.00
@@ -608,6 +612,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
608612

609613
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
610614
|-|-|-|-|-|-
615+
| 0228 |[Summary Ranges](src/main/python/g0201_0300/s0228_summary_ranges/Solution0228.py)| Easy | Array | 0 | 100.00
611616
| 0056 |[Merge Intervals](src/main/python/g0001_0100/s0056_merge_intervals/Solution0056.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 72.98
612617
| 0057 |[Insert Interval](src/main/python/g0001_0100/s0057_insert_interval/Solution0057.py)| Medium | Array | 0 | 100.00
613618

@@ -653,6 +658,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
653658
| 0129 |[Sum Root to Leaf Numbers](src/main/python/g0101_0200/s0129_sum_root_to_leaf_numbers/Solution0129.py)| Medium | Depth_First_Search, Tree, Binary_Tree | 0 | 100.00
654659
| 0124 |[Binary Tree Maximum Path Sum](src/main/python/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution0124.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 11 | 91.40
655660
| 0173 |[Binary Search Tree Iterator](src/main/python/g0101_0200/s0173_binary_search_tree_iterator/Solution0173.py)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 2 | 92.39
661+
| 0222 |[Count Complete Tree Nodes](src/main/python/g0201_0300/s0222_count_complete_tree_nodes/Solution0222.py)| Easy | Depth_First_Search, Tree, Binary_Search, Binary_Tree | 0 | 100.00
656662
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/python/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution0236.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/DFS, Big_O_Time_O(n)_Space_O(n) | 41 | 97.74
657663

658664
#### Top Interview 150 Binary Tree BFS
@@ -818,6 +824,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
818824

819825
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
820826
|-|-|-|-|-|-
827+
| 0242 |[Valid Anagram](src/main/python/g0201_0300/s0242_valid_anagram/Solution0242.py)| Easy | String, Hash_Table, Sorting | 11 | 72.46
821828

822829
#### Day 7 Linked List
823830

@@ -1352,6 +1359,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
13521359

13531360
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13541361
|-|-|-|-|-|-
1362+
| 0222 |[Count Complete Tree Nodes](src/main/python/g0201_0300/s0222_count_complete_tree_nodes/Solution0222.py)| Easy | Depth_First_Search, Tree, Binary_Search, Binary_Tree | 0 | 100.00
13551363

13561364
#### Day 11
13571365

@@ -1592,6 +1600,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
15921600

15931601
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15941602
|-|-|-|-|-|-
1603+
| 0242 |[Valid Anagram](src/main/python/g0201_0300/s0242_valid_anagram/Solution0242.py)| Easy | String, Hash_Table, Sorting | 11 | 72.46
15951604

15961605
#### Day 12 Class and Object
15971606

@@ -1735,16 +1744,21 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
17351744
| 0322 |[Coin Change](src/main/python/g0301_0400/s0322_coin_change/Solution0322.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(m\*n)_Space_O(amount) | 454 | 96.33
17361745
| 0300 |[Longest Increasing Subsequence](src/main/python/g0201_0300/s0300_longest_increasing_subsequence/Solution0300.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(n\*log_n)_Space_O(n) | 11 | 78.54
17371746
| 0295 |[Find Median from Data Stream](src/main/python/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.py)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Top_Interview_150_Heap, Big_O_Time_O(n\*log_n)_Space_O(n) | 131 | 72.46
1747+
| 0289 |[Game of Life](src/main/python/g0201_0300/s0289_game_of_life/Solution0289.py)| Medium | Array, Matrix, Simulation, Top_Interview_150_Matrix | 0 | 100.00
17381748
| 0287 |[Find the Duplicate Number](src/main/python/g0201_0300/s0287_find_the_duplicate_number/Solution0287.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 16 | 96.25
17391749
| 0283 |[Move Zeroes](src/main/python/g0201_0300/s0283_move_zeroes/Solution0283.py)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, LeetCode_75_Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 3 | 80.05
1750+
| 0274 |[H-Index](src/main/python/g0201_0300/s0274_h_index/Solution0274.py)| Medium | Array, Sorting, Counting_Sort, Top_Interview_150_Array/String | 0 | 100.00
1751+
| 0242 |[Valid Anagram](src/main/python/g0201_0300/s0242_valid_anagram/Solution0242.py)| Easy | String, Hash_Table, Sorting, Data_Structure_I_Day_6_String, Programming_Skills_I_Day_11_Containers_and_Libraries, Udemy_Strings, Top_Interview_150_Hashmap | 11 | 72.46
17401752
| 0240 |[Search a 2D Matrix II](src/main/python/g0201_0300/s0240_search_a_2d_matrix_ii/Solution0240.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 143 | 64.84
17411753
| 0239 |[Sliding Window Maximum](src/main/python/g0201_0300/s0239_sliding_window_maximum/Solution0239.py)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 152 | 81.96
17421754
| 0238 |[Product of Array Except Self](src/main/python/g0201_0300/s0238_product_of_array_except_self/Solution0238.py)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, LeetCode_75_Array/String, Data_Structure_II_Day_5_Array, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n^2)_Space_O(n) | 15 | 97.12
17431755
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/python/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution0236.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/DFS, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(n)_Space_O(n) | 41 | 97.74
17441756
| 0234 |[Palindrome Linked List](src/main/python/g0201_0300/s0234_palindrome_linked_list/Solution0234.py)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 23 | 81.92
17451757
| 0230 |[Kth Smallest Element in a BST](src/main/python/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution0230.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree, Top_Interview_150_Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1758+
| 0228 |[Summary Ranges](src/main/python/g0201_0300/s0228_summary_ranges/Solution0228.py)| Easy | Array, Top_Interview_150_Intervals | 0 | 100.00
17461759
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution0226.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
17471760
| 0224 |[Basic Calculator](src/main/python/g0201_0300/s0224_basic_calculator/Solution0224.py)| Hard | String, Math, Stack, Recursion, Top_Interview_150_Stack | 19 | 93.87
1761+
| 0222 |[Count Complete Tree Nodes](src/main/python/g0201_0300/s0222_count_complete_tree_nodes/Solution0222.py)| Easy | Depth_First_Search, Tree, Binary_Search, Binary_Tree, Binary_Search_II_Day_10, Top_Interview_150_Binary_Tree_General | 0 | 100.00
17481762
| 0221 |[Maximal Square](src/main/python/g0201_0300/s0221_maximal_square/Solution0221.py)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Top_Interview_150_Multidimensional_DP, Big_O_Time_O(m\*n)_Space_O(m\*n) | 129 | 64.49
17491763
| 0219 |[Contains Duplicate II](src/main/python/g0201_0300/s0219_contains_duplicate_ii/Solution0219.py)| Easy | Array, Hash_Table, Sliding_Window, Top_Interview_150_Hashmap | 26 | 84.30
17501764
| 0215 |[Kth Largest Element in an Array](src/main/python/g0201_0300/s0215_kth_largest_element_in_an_array/Solution0215.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, LeetCode_75_Heap/Priority_Queue, Data_Structure_II_Day_20_Heap_Priority_Queue, Top_Interview_150_Heap, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 45 | 90.46
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# #Easy #Depth_First_Search #Tree #Binary_Search #Binary_Tree #Binary_Search_II_Day_10
2+
# #Top_Interview_150_Binary_Tree_General #2025_09_17_Time_0_ms_(100.00%)_Space_23.29_MB_(82.07%)
3+
4+
from typing import Optional
5+
6+
class TreeNode:
7+
def __init__(self, val: int = 0, left: Optional["TreeNode"] = None, right: Optional["TreeNode"] = None):
8+
self.val = val
9+
self.left = left
10+
self.right = right
11+
12+
class Solution:
13+
def countNodes(self, root: Optional[TreeNode]) -> int:
14+
if root is None:
15+
return 0
16+
left_height = self._left_height(root)
17+
right_height = self._right_height(root)
18+
if left_height == right_height:
19+
return (1 << left_height) - 1
20+
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
21+
22+
def _left_height(self, node: Optional[TreeNode]) -> int:
23+
if node is None:
24+
return 0
25+
return 1 + self._left_height(node.left)
26+
27+
def _right_height(self, node: Optional[TreeNode]) -> int:
28+
if node is None:
29+
return 0
30+
return 1 + self._right_height(node.right)
31+
32+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from Solution0222 import Solution, TreeNode
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_countNodes(self):
6+
left_left = TreeNode(4)
7+
left_right = TreeNode(5)
8+
left = TreeNode(2, left_left, left_right)
9+
right_left = TreeNode(6)
10+
right = TreeNode(3, right_left, None)
11+
root = TreeNode(1, left, right)
12+
self.assertEqual(Solution().countNodes(root), 6)
13+
14+
def test_countNodes_null(self):
15+
self.assertEqual(Solution().countNodes(None), 0)
16+
17+
def test_countNodes_single(self):
18+
self.assertEqual(Solution().countNodes(TreeNode(1)), 1)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
222\. Count Complete Tree Nodes
2+
3+
Medium
4+
5+
Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
6+
7+
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between `1` and <code>2<sup>h</sup></code> nodes inclusive at the last level `h`.
8+
9+
Design an algorithm that runs in less than `O(n)` time complexity.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
14+
15+
**Input:** root = [1,2,3,4,5,6]
16+
17+
**Output:** 6
18+
19+
**Example 2:**
20+
21+
**Input:** root = []
22+
23+
**Output:** 0
24+
25+
**Example 3:**
26+
27+
**Input:** root = [1]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
34+
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code>
35+
* The tree is guaranteed to be **complete**.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# #Easy #Array #Top_Interview_150_Intervals #2025_09_17_Time_0_ms_(100.00%)_Space_17.69_MB_(84.69%)
2+
3+
from typing import List
4+
5+
class Solution:
6+
def summaryRanges(self, nums: List[int]) -> List[str]:
7+
ranges: List[str] = []
8+
if not nums:
9+
return ranges
10+
a = nums[0]
11+
b = a
12+
for i in range(1, len(nums)):
13+
if nums[i] != b + 1:
14+
if a == b:
15+
ranges.append(str(a))
16+
else:
17+
ranges.append(f"{a}->{b}")
18+
a = nums[i]
19+
b = a
20+
else:
21+
b += 1
22+
if a == b:
23+
ranges.append(str(a))
24+
else:
25+
ranges.append(f"{a}->{b}")
26+
return ranges
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from Solution0228 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_summaryRanges(self):
6+
self.assertEqual(
7+
Solution().summaryRanges([0, 1, 2, 4, 5, 7]),
8+
["0->2", "4->5", "7"],
9+
)
10+
11+
def test_summaryRanges2(self):
12+
self.assertEqual(
13+
Solution().summaryRanges([0, 2, 3, 4, 6, 8, 9]),
14+
["0", "2->4", "6", "8->9"],
15+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
228\. Summary Ranges
2+
3+
Easy
4+
5+
You are given a **sorted unique** integer array `nums`.
6+
7+
Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`.
8+
9+
Each range `[a,b]` in the list should be output as:
10+
11+
* `"a->b"` if `a != b`
12+
* `"a"` if `a == b`
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [0,1,2,4,5,7]
17+
18+
**Output:** ["0->2","4->5","7"]
19+
20+
**Explanation:** The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [0,2,3,4,6,8,9]
25+
26+
**Output:** ["0","2->4","6","8->9"]
27+
28+
**Explanation:** The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
29+
30+
**Example 3:**
31+
32+
**Input:** nums = []
33+
34+
**Output:** []
35+
36+
**Example 4:**
37+
38+
**Input:** nums = [-1]
39+
40+
**Output:** ["-1"]
41+
42+
**Example 5:**
43+
44+
**Input:** nums = [0]
45+
46+
**Output:** ["0"]
47+
48+
**Constraints:**
49+
50+
* `0 <= nums.length <= 20`
51+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
52+
* All the values of `nums` are **unique**.
53+
* `nums` is sorted in ascending order.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# #Easy #String #Hash_Table #Sorting #Data_Structure_I_Day_6_String
2+
# #Programming_Skills_I_Day_11_Containers_and_Libraries #Udemy_Strings #Top_Interview_150_Hashmap
3+
# #2025_09_17_Time_11_ms_(72.46%)_Space_17.66_MB_(97.60%)
4+
5+
class Solution:
6+
def isAnagram(self, s: str, t: str) -> bool:
7+
if len(s) != len(t):
8+
return False
9+
freq = [0] * 26
10+
for ch in s:
11+
freq[ord(ch) - 97] += 1
12+
for ch in t:
13+
idx = ord(ch) - 97
14+
if freq[idx] == 0:
15+
return False
16+
freq[idx] -= 1
17+
return True
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest
2+
from Solution0242 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_isAnagram(self):
6+
self.assertTrue(Solution().isAnagram("anagram", "nagaram"))
7+
8+
def test_isAnagram2(self):
9+
self.assertFalse(Solution().isAnagram("rat", "car"))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
242\. Valid Anagram
2+
3+
Easy
4+
5+
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "anagram", t = "nagaram"
10+
11+
**Output:** true
12+
13+
**Example 2:**
14+
15+
**Input:** s = "rat", t = "car"
16+
17+
**Output:** false
18+
19+
**Constraints:**
20+
21+
* <code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code>
22+
* `s` and `t` consist of lowercase English letters.
23+
24+
**Follow up:** What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

0 commit comments

Comments
 (0)