diff --git a/README.md b/README.md index 19499966..18c38b1b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# **[LeetCode Records](https://leetcode.com/u/Zanger/)** (396 solved) +# **[LeetCode Records](https://leetcode.com/u/Zanger/)** (400 solved) > My LeetCode Profile: [Zanger](https://leetcode.com/u/Zanger/) @@ -28,7 +28,7 @@ This repo is a collection of my LeetCode solutions, primarily written in Python, ------ -[Array](), [Hash Table](), [String](), [Depth-First Search](), [Tree](), [Binary Tree](), [Sorting](), [Math](), [Breadth-First Search](), [Two Pointers](), [Linked List](), [Stack](), [Greedy](), [Matrix](), [Simulation](), [Bit Manipulation](), [Design](), [Dynamic Programming](), [Database](), [Binary Search](), [Trie](), [Heap (Priority Queue)](), [Counting](), [Recursion](), [Backtracking](), [Binary Search Tree](), [Sliding Window](), [Prefix Sum](), [Monotonic Stack](), [Queue](), [Union Find](), [Divide and Conquer](), [String Matching](), [Graph](), [Hash Function](), [Combinatorics](), [Randomized](), [Monotonic Queue](), [Iterator](), [Bucket Sort](), [Geometry](), [Counting Sort](), [Brainteaser](), [Game Theory](), [Interactive](), [Memoization](), [Rolling Hash](), [Quickselect](), [Doubly-Linked List](), [Data Stream](), [Bitmask](), [Merge Sort](), [Ordered Set](), [Suffix Array](), [Topological Sort](), [Shortest Path](), [Number Theory](), [Reservoir Sampling]() +[Array](), [Hash Table](), [String](), [Depth-First Search](), [Tree](), [Binary Tree](), [Sorting](), [Math](), [Breadth-First Search](), [Two Pointers](), [Linked List](), [Stack](), [Greedy](), [Matrix](), [Simulation](), [Design](), [Bit Manipulation](), [Dynamic Programming](), [Database](), [Binary Search](), [Trie](), [Heap (Priority Queue)](), [Counting](), [Recursion](), [Backtracking](), [Binary Search Tree](), [Sliding Window](), [Prefix Sum](), [Queue](), [Monotonic Stack](), [Union Find](), [Divide and Conquer](), [String Matching](), [Graph](), [Hash Function](), [Combinatorics](), [Randomized](), [Monotonic Queue](), [Doubly-Linked List](), [Iterator](), [Bucket Sort](), [Geometry](), [Counting Sort](), [Brainteaser](), [Game Theory](), [Interactive](), [Memoization](), [Data Stream](), [Rolling Hash](), [Quickselect](), [Bitmask](), [Merge Sort](), [Ordered Set](), [Suffix Array](), [Topological Sort](), [Shortest Path](), [Number Theory](), [Reservoir Sampling]() ------ @@ -183,6 +183,7 @@ This repo is a collection of my LeetCode solutions, primarily written in Python, | 542 | [01 Matrix]() | Medium | | [solution]() | py | | 543 | [Diameter of Binary Tree]() | Easy | N150 | [solution]() | py, java, c | | 565 | [Array Nesting]() | Medium | | [solution]() | py | +| 567 | [Permutation in String]() | Medium | N150 | [solution]() | py | | 572 | [Subtree of Another Tree]() | Easy | N150, B75 | [solution]() | py | | 577 | [Employee Bonus]() | Easy | | [solution]() | sql | | 584 | [Find Customer Referee]() | Easy | | [solution]() | sql | @@ -193,6 +194,7 @@ This repo is a collection of my LeetCode solutions, primarily written in Python, | 611 | [Valid Triangle Number]() | Medium | | [solution]() | py | | 616 | [Add Bold Tag in String]() | Medium | | [solution]() | py | | 619 | [Biggest Single Number]() | Easy | | [solution]() | sql | +| 622 | [Design Circular Queue]() | Medium | | [solution]() | py | | 637 | [Average of Levels in Binary Tree]() | Easy | | [solution]() | py | | 645 | [Set Mismatch]() | Easy | | [solution]() | py, java | | 647 | [Palindromic Substrings]() | Medium | N150, B75 | [solution]() | py | @@ -287,6 +289,7 @@ This repo is a collection of my LeetCode solutions, primarily written in Python, | 1317 | [Convert Integer to the Sum of Two No-Zero Integers]() | Easy | | [solution]() | c | | 1351 | [Count Negative Numbers in a Sorted Matrix]() | Easy | | [solution]() | py, java | | 1372 | [Longest ZigZag Path in a Binary Tree]() | Medium | | [solution]() | py | +| 1381 | [Design a Stack With Increment Operation]() | Medium | | [solution]() | py | | 1382 | [Balance a Binary Search Tree]() | Medium | Daily | [solution]() | py | | 1404 | [Number of Steps to Reduce a Number in Binary Representation to One]() | Medium | Daily | [solution]() | py | | 1426 | [Counting Elements]() | Easy | | [solution]() | c | @@ -298,6 +301,7 @@ This repo is a collection of my LeetCode solutions, primarily written in Python, | 1457 | [Pseudo-Palindromic Paths in a Binary Tree]() | Medium | | [solution]() | py | | 1460 | [Make Two Arrays Equal by Reversing Subarrays]() | Easy | | [solution]() | c | | 1470 | [Shuffle the Array]() | Easy | | [solution]() | c | +| 1472 | [Design Browser History]() | Medium | | [solution]() | py | | 1474 | [Delete N Nodes After M Nodes of a Linked List]() | Easy | | [solution]() | java, c | | 1481 | [Least Number of Unique Integers after K Removals]() | Medium | | [solution]() | py | | 1482 | [Minimum Number of Days to Make m Bouquets]() | Medium | | [solution]() | py | diff --git a/markdowns/Questions_By_Code_Length.md b/markdowns/Questions_By_Code_Length.md index c169180d..79bdd060 100644 --- a/markdowns/Questions_By_Code_Length.md +++ b/markdowns/Questions_By_Code_Length.md @@ -108,6 +108,7 @@ Calculations are based on the code files's byte sizes. | 771 | [Jewels and Stones]() | Easy | | [solution](<_771. Jewels and Stones.md>) | py, java | Jun 02, 2024 | | 863 | [All Nodes Distance K in Binary Tree]() | Medium | | [solution](<_863. All Nodes Distance K in Binary Tree.md>) | py | Jun 26, 2024 | | 1122 | [Relative Sort Array]() | Easy | Daily | [solution](<_1122. Relative Sort Array.md>) | py, java | Jun 10, 2024 | +| 567 | [Permutation in String]() | Medium | N150 | [solution](<_567. Permutation in String.md>) | py | Jul 05, 2024 | | 1209 | [Remove All Adjacent Duplicates in String II]() | Medium | | [solution](<_1209. Remove All Adjacent Duplicates in String II.md>) | py | Jun 17, 2024 | | 1171 | [Remove Zero Sum Consecutive Nodes from Linked List]() | Medium | | [solution](<_1171. Remove Zero Sum Consecutive Nodes from Linked List.md>) | py | Jun 15, 2024 | | 791 | [Custom Sort String]() | Medium | | [solution](<_791. Custom Sort String.md>) | java | Jun 24, 2024 | @@ -120,6 +121,7 @@ Calculations are based on the code files's byte sizes. | 919 | [Complete Binary Tree Inserter]() | Medium | | [solution](<_919. Complete Binary Tree Inserter.md>) | py | Jul 05, 2024 | | 271 | [Encode and Decode Strings]() | Medium | N150, B75 | [solution](<_271. Encode and Decode Strings.md>) | py | Jun 12, 2024 | | 24 | [Swap Nodes in Pairs]() | Medium | | [solution](<_24. Swap Nodes in Pairs.md>) | c | Jun 06, 2024 | +| 622 | [Design Circular Queue]() | Medium | | [solution](<_622. Design Circular Queue.md>) | py | Jul 05, 2024 | | 1 | [Two Sum]() | Easy | N150, B75 | [solution](<_1. Two Sum.md>) | py, java | May 22, 2024 | | 1268 | [Search Suggestions System]() | Medium | | [solution](<_1268. Search Suggestions System.md>) | py | Jun 28, 2024 | | 20 | [Valid Parentheses]() | Easy | N150, B75 | [solution](<_20. Valid Parentheses.md>) | java | May 22, 2024 | @@ -236,6 +238,7 @@ Calculations are based on the code files's byte sizes. | 807 | [Max Increase to Keep City Skyline]() | Medium | | [solution](<_807. Max Increase to Keep City Skyline.md>) | java | Jun 22, 2024 | | 3174 | bw contest 132 - q1 | Easy | Contest | [solution](<_3174. bw contest 132 - q.md>) | py | Jun 08, 2024 | | 49 | [Group Anagrams]() | Medium | N150, B75 | [solution](<_49. Group Anagrams.md>) | py | Jun 12, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<_1472. Design Browser History.md>) | py | Jul 05, 2024 | | 720 | [Longest Word in Dictionary]() | Medium | | [solution](<_720. Longest Word in Dictionary.md>) | py | Jun 27, 2024 | | 1858 | [Longest Word With All Prefixes]() | Medium | | [solution](<_1858. Longest Word With All Prefixes.md>) | py | Jun 29, 2024 | | 1455 | [Check If a Word Occurs As a Prefix of Any Word in a Sentence]() | Easy | | [solution](<_1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.md>) | py | Jun 01, 2024 | @@ -288,6 +291,7 @@ Calculations are based on the code files's byte sizes. | 1669 | [Merge In Between Linked Lists]() | Medium | | [solution](<_1669. Merge In Between Linked Lists.md>) | c | Jun 24, 2024 | | 14 | [Longest Common Prefix]() | Easy | | [solution](<_14. Longest Common Prefix.md>) | java | May 22, 2024 | | 3043 | [Find the Length of the Longest Common Prefix]() | Medium | | [solution](<_3043. Find the Length of the Longest Common Prefix.md>) | py | Jun 27, 2024 | +| 1381 | [Design a Stack With Increment Operation]() | Medium | | [solution](<_1381. Design a Stack With Increment Operation.md>) | py | Jul 05, 2024 | | 1002 | [Find Common Characters]() | Easy | Daily | [solution](<_1002. Find Common Characters.md>) | py | Jun 04, 2024 | | 206 | [Reverse Linked List]() | Easy | N150, B75 | [solution](<_206. Reverse Linked List.md>) | java | Jun 27, 2024 | | 2083 | [Substrings That Begin and End With the Same Letter]() | Medium | Weekly Premium | [solution](<_2083. Substrings That Begin and End With the Same Letter.md>) | py, c | Jun 10, 2024 | diff --git a/markdowns/Questions_By_Recent.md b/markdowns/Questions_By_Recent.md index e60dc220..a4a75c1c 100644 --- a/markdowns/Questions_By_Recent.md +++ b/markdowns/Questions_By_Recent.md @@ -6,6 +6,10 @@ Calculations are based on the date of the first solve. | # | Title | Level | Cats | Solution | Languages | Date Complete | |-----:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:---------------|:---------------------------------------------------------------------------------------------------|:---------------------------------|:----------------| +| 1381 | [Design a Stack With Increment Operation]() | Medium | | [solution](<_1381. Design a Stack With Increment Operation.md>) | py | Jul 05, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<_1472. Design Browser History.md>) | py | Jul 05, 2024 | +| 622 | [Design Circular Queue]() | Medium | | [solution](<_622. Design Circular Queue.md>) | py | Jul 05, 2024 | +| 567 | [Permutation in String]() | Medium | N150 | [solution](<_567. Permutation in String.md>) | py | Jul 05, 2024 | | 380 | [Insert Delete GetRandom O(1)]() | Medium | | [solution](<_380. Insert Delete GetRandom O(1).md>) | java | Jul 05, 2024 | | 381 | [Insert Delete GetRandom O(1) - Duplicates allowed]() | Hard | | [solution](<_381. Insert Delete GetRandom O(1) - Duplicates allowed.md>) | java | Jul 05, 2024 | | 2582 | [Pass the Pillow]() | Easy | Daily | [solution](<_2582. Pass the Pillow.md>) | py, java, cs, js, cpp, c | Jul 05, 2024 | diff --git a/markdowns/Topics.md b/markdowns/Topics.md index a87620bd..4a0ab54b 100644 --- a/markdowns/Topics.md +++ b/markdowns/Topics.md @@ -4,23 +4,23 @@ ------ -- [Array]() (178 completed) -- [Hash Table]() (88 completed) -- [String]() (87 completed) +- [Array]() (181 completed) +- [Hash Table]() (89 completed) +- [String]() (88 completed) - [Depth-First Search]() (68 completed) - [Tree]() (66 completed) - [Binary Tree]() (61 completed) - [Sorting]() (55 completed) - [Math]() (49 completed) - [Breadth-First Search]() (47 completed) -- [Two Pointers]() (43 completed) -- [Linked List]() (38 completed) -- [Stack]() (35 completed) +- [Two Pointers]() (44 completed) +- [Linked List]() (40 completed) +- [Stack]() (37 completed) - [Greedy]() (31 completed) - [Matrix]() (30 completed) - [Simulation]() (26 completed) +- [Design]() (25 completed) - [Bit Manipulation]() (24 completed) -- [Design]() (22 completed) - [Dynamic Programming]() (20 completed) - [Database]() (18 completed) - [Binary Search]() (18 completed) @@ -30,10 +30,10 @@ - [Recursion]() (14 completed) - [Backtracking]() (13 completed) - [Binary Search Tree]() (13 completed) -- [Sliding Window]() (11 completed) +- [Sliding Window]() (12 completed) - [Prefix Sum]() (10 completed) +- [Queue]() (9 completed) - [Monotonic Stack]() (9 completed) -- [Queue]() (8 completed) - [Union Find]() (8 completed) - [Divide and Conquer]() (6 completed) - [String Matching]() (6 completed) @@ -42,6 +42,7 @@ - [Combinatorics]() (4 completed) - [Randomized]() (4 completed) - [Monotonic Queue]() (3 completed) +- [Doubly-Linked List]() (3 completed) - [Iterator]() (3 completed) - [Bucket Sort]() (3 completed) - [Geometry]() (2 completed) @@ -50,10 +51,9 @@ - [Game Theory]() (2 completed) - [Interactive]() (2 completed) - [Memoization]() (2 completed) +- [Data Stream]() (2 completed) - [Rolling Hash]() (2 completed) - [Quickselect]() (2 completed) -- [Doubly-Linked List]() (2 completed) -- [Data Stream]() (1 completed) - [Bitmask]() (1 completed) - [Merge Sort]() (1 completed) - [Ordered Set]() (1 completed) diff --git a/markdowns/_1381. Design a Stack With Increment Operation.md b/markdowns/_1381. Design a Stack With Increment Operation.md new file mode 100644 index 00000000..98fcb064 --- /dev/null +++ b/markdowns/_1381. Design a Stack With Increment Operation.md @@ -0,0 +1,62 @@ +# 1381. [Design a Stack With Increment Operation]() + +*[Back to top](<../README.md>)* + +------ + +> *First completed : July 05, 2024* +> +> *Last updated : July 05, 2024* + + +------ + +> **Related Topics** : **[Array](), [Stack](), [Design]()** +> +> **Acceptance Rate** : **77.091 %** + + +------ + +*To see the question prompt, click the title.* + +> ## This question has the honour of being my 400th question on LeetCode. +> +> **On Day 39 of my grind, 8:35PM PST, this question was done. Simple one to end off this sprint. :)** + +------ + +## Solutions + +- [m1381.py](<../my-submissions/m1381.py>) +### Python +#### [m1381.py](<../my-submissions/m1381.py>) +```Python +class CustomStack: + + def __init__(self, maxSize: int): + self.stk = [] + self.maxx = maxSize + + def push(self, x: int) -> None: + if len(self.stk) >= self.maxx : + return + self.stk.append(x) + + def pop(self) -> int: + if not self.stk : + return -1 + return self.stk.pop() + + def increment(self, k: int, val: int) -> None: + for i in range(min(k, len(self.stk))) : + self.stk[i] += val + + +# Your CustomStack object will be instantiated and called as such: +# obj = CustomStack(maxSize) +# obj.push(x) +# param_2 = obj.pop() +# obj.increment(k,val) +``` + diff --git a/markdowns/_1472. Design Browser History.md b/markdowns/_1472. Design Browser History.md new file mode 100644 index 00000000..a3b83a36 --- /dev/null +++ b/markdowns/_1472. Design Browser History.md @@ -0,0 +1,56 @@ +# 1472. [Design Browser History]() + +*[Back to top](<../README.md>)* + +------ + +> *First completed : July 05, 2024* +> +> *Last updated : July 05, 2024* + + +------ + +> **Related Topics** : **[Array](), [Linked List](), [Stack](), [Design](), [Doubly-Linked List](), [Data Stream]()** +> +> **Acceptance Rate** : **77.568 %** + + +------ + +*To see the question prompt, click the title.* + +## Solutions + +- [m1472.py](<../my-submissions/m1472.py>) +### Python +#### [m1472.py](<../my-submissions/m1472.py>) +```Python +class BrowserHistory: + + def __init__(self, homepage: str): + self.history = [homepage] + self.spot = 0 + + def visit(self, url: str) -> None: + self.spot += 1 + if self.spot < len(self.history) : + self.history = self.history[:self.spot] + self.history.append(url) + + def back(self, steps: int) -> str: + self.spot = max(0, self.spot - steps) + return self.history[self.spot] + + def forward(self, steps: int) -> str: + self.spot = min(len(self.history) - 1, self.spot + steps) + return self.history[self.spot] + + +# Your BrowserHistory object will be instantiated and called as such: +# obj = BrowserHistory(homepage) +# obj.visit(url) +# param_2 = obj.back(steps) +# param_3 = obj.forward(steps) +``` + diff --git a/markdowns/_567. Permutation in String.md b/markdowns/_567. Permutation in String.md new file mode 100644 index 00000000..eb316a4d --- /dev/null +++ b/markdowns/_567. Permutation in String.md @@ -0,0 +1,124 @@ +# 567. [Permutation in String]() + +*[Back to top](<../README.md>)* + +------ + +> *First completed : July 05, 2024* +> +> *Last updated : July 05, 2024* + + +------ + +> **Related Topics** : **[Hash Table](), [Two Pointers](), [String](), [Sliding Window]()** +> +> **Acceptance Rate** : **44.454 %** + + +------ + +*To see the question prompt, click the title.* + +> +> #### Version 1 +> +> In version 1, I just went with a straightforward counter approach. +> Since `Python` `Counter` objects have a built-in comparing method, +> I decided to simply use that. It's still technically $O(n)$ in total +> since the counter size will be at most 26 (since it's only counting) +> letters, but it's still less than ideal. +> +> #### Version 2 +> +> In version 2, I swapped over to, while still using a counter, only +> committing a comparison for the values that are being updated. That is, +> the values at the front of the window being added and the values +> are the end of the window that are being dropped. We have to find a case +> where all values line up, so we iterate initially for the first `len(s1)` +> letters then proceed with only incremental updates from that point onwards. +> +> +> #### Fun Testcase +> +> When creating version 2, I encountered a very fun test case: +> ``` +> s1 = "trinitrophenylmethylnitramine" +> s2 = "dinitrophenylhydrazinetrinitrophenylmethylnitramine" +> ``` +> Clearly, `s1` matches the ending of `s2` exactly, but my original +> code for version 2 (the incomplete `v2`) was resulting in `False`. +> Eventually, I realized that the cause was that during addings and +> subtractions of the same letter, it would **cause match to increment by 2** +> due to the the "new values" equallying the expected values in `s1`'s counter. +> This would only happen when the values where originally the correct +> value when it occured. +> +> I added the `if cL == cR : continue` check and that was resolved quickly +> (as a if the previous wasn't a solution and didn't return, then this clearly +> wouldn't be a solution either since it's equal in the letter counts). + +------ + +## Solutions + +- [m567 v1 Counter Equivalence.py](<../my-submissions/m567 v1 Counter Equivalence.py>) +- [m567 v2 Running Counter.py](<../my-submissions/m567 v2 Running Counter.py>) +### Python +#### [m567 v1 Counter Equivalence.py](<../my-submissions/m567 v1 Counter Equivalence.py>) +```Python +class Solution: + def checkInclusion(self, s1: str, s2: str) -> bool: + cnt1 = Counter(s1) + cnt2 = Counter(s2[:len(s1) - 1]) + + for cL, cR in zip(s2, s2[len(s1) - 1 : len(s2)]) : + cnt2[cR] += 1 + if cnt1 == cnt2 : + return True + cnt2[cL] -= 1 + + return False +``` + +#### [m567 v2 Running Counter.py](<../my-submissions/m567 v2 Running Counter.py>) +```Python +class Solution: + def checkInclusion(self, s1: str, s2: str) -> bool: + cnt1 = [0] * 26 + cnt2 = [0] * 26 + + if len(s1) > len(s2) : + return False + + for c1, c2 in zip(s1, s2) : + cnt1[ord(c1) - ord('a')] += 1 + cnt2[ord(c2) - ord('a')] += 1 + + matches = [cnt1[x] == cnt2[x] for x in range(len(cnt1))].count(True) + + if matches == 26 : + return True + for cL, cR in zip(s2, s2[len(s1):]) : + cnt2[ord(cL) - ord('a')] -= 1 + cnt2[ord(cR) - ord('a')] += 1 + + if cL == cR : + continue + + if cnt2[ord(cR) - ord('a')] == cnt1[ord(cR) - ord('a')] : + matches += 1 + elif cnt2[ord(cR) - ord('a')] == cnt1[ord(cR) - ord('a')] + 1 : + matches -= 1 + + if cnt2[ord(cL) - ord('a')] == cnt1[ord(cL) - ord('a')] : + matches += 1 + elif cnt2[ord(cL) - ord('a')] == cnt1[ord(cL) - ord('a')] - 1 : + matches -= 1 + + if matches == 26 : + return True + + return False +``` + diff --git a/markdowns/_622. Design Circular Queue.md b/markdowns/_622. Design Circular Queue.md new file mode 100644 index 00000000..03c6e82a --- /dev/null +++ b/markdowns/_622. Design Circular Queue.md @@ -0,0 +1,82 @@ +# 622. [Design Circular Queue]() + +*[Back to top](<../README.md>)* + +------ + +> *First completed : July 05, 2024* +> +> *Last updated : July 05, 2024* + + +------ + +> **Related Topics** : **[Array](), [Linked List](), [Design](), [Queue]()** +> +> **Acceptance Rate** : **51.504 %** + + +------ + +*To see the question prompt, click the title.* + +## Solutions + +- [m622.py](<../my-submissions/m622.py>) +### Python +#### [m622.py](<../my-submissions/m622.py>) +```Python +class MyCircularQueue: + + def __init__(self, k: int): + self.vals = [0] * k + self.size = 0 + self.pop = 0 + self.add = 0 + self.k = k + + def enQueue(self, value: int) -> bool: + if self.size >= self.k : + return False + if self.size == 0 : + self.pop = 0 + self.add = 0 + self.vals[0] = value + else : + self.add = (self.add + 1) % self.k + self.vals[self.add] = value + self.size += 1 + return True + + def deQueue(self) -> bool: + if self.isEmpty() : + return False + + if self.pop != self.add : + self.pop = (self.pop + 1) % self.k + self.size -= 1 + return True + + def Front(self) -> int: + return self.vals[self.pop] if self.size > 0 else -1 + + def Rear(self) -> int: + return self.vals[self.add] if self.size > 0 else -1 + + def isEmpty(self) -> bool: + return self.size == 0 + + def isFull(self) -> bool: + return self.size == self.k + + +# Your MyCircularQueue object will be instantiated and called as such: +# obj = MyCircularQueue(k) +# param_1 = obj.enQueue(value) +# param_2 = obj.deQueue() +# param_3 = obj.Front() +# param_4 = obj.Rear() +# param_5 = obj.isEmpty() +# param_6 = obj.isFull() +``` + diff --git a/markdowns/by_topic/Array.md b/markdowns/by_topic/Array.md index 2fabcbec..8a6a54ad 100644 --- a/markdowns/by_topic/Array.md +++ b/markdowns/by_topic/Array.md @@ -1,4 +1,4 @@ -# [Array]() (178 completed) +# [Array]() (181 completed) *[Back to top](<../../README.md>)* @@ -58,6 +58,7 @@ | 565 | [Array Nesting]() | Medium | | [solution](<../_565. Array Nesting.md>) | py | Jul 02, 2024 | | 611 | [Valid Triangle Number]() | Medium | | [solution](<../_611. Valid Triangle Number.md>) | py | May 22, 2024 | | 616 | [Add Bold Tag in String]() | Medium | | [solution](<../_616. Add Bold Tag in String.md>) | py | Jun 27, 2024 | +| 622 | [Design Circular Queue]() | Medium | | [solution](<../_622. Design Circular Queue.md>) | py | Jul 05, 2024 | | 645 | [Set Mismatch]() | Easy | | [solution](<../_645. Set Mismatch.md>) | py, java | Jun 01, 2024 | | 648 | [Replace Words]() | Medium | Daily | [solution](<../_648. Replace Words.md>) | py, java | Jun 06, 2024 | | 654 | [Maximum Binary Tree]() | Medium | | [solution](<../_654. Maximum Binary Tree.md>) | py | Jun 11, 2024 | @@ -115,12 +116,14 @@ | 1282 | [Group the People Given the Group Size They Belong To]() | Medium | | [solution](<../_1282. Group the People Given the Group Size They Belong To.md>) | py | Jun 27, 2024 | | 1284 | [Minimum Number of Flips to Convert Binary Matrix to Zero Matrix]() | Hard | | [solution](<../_1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix.md>) | py | Jun 08, 2024 | | 1351 | [Count Negative Numbers in a Sorted Matrix]() | Easy | | [solution](<../_1351. Count Negative Numbers in a Sorted Matrix.md>) | py, java | Jun 01, 2024 | +| 1381 | [Design a Stack With Increment Operation]() | Medium | | [solution](<../_1381. Design a Stack With Increment Operation.md>) | py | Jul 05, 2024 | | 1426 | [Counting Elements]() | Easy | | [solution](<../_1426. Counting Elements.md>) | c | Jun 04, 2024 | | 1436 | [Destination City]() | Easy | | [solution](<../_1436. Destination City.md>) | java | Jun 01, 2024 | | 1438 | [Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit]() | Medium | Daily | [solution](<../_1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.md>) | java | Jun 22, 2024 | | 1442 | [Count Triplets That Can Form Two Arrays of Equal XOR]() | Medium | Daily | [solution](<../_1442. Count Triplets That Can Form Two Arrays of Equal XOR.md>) | py | May 29, 2024 | | 1460 | [Make Two Arrays Equal by Reversing Subarrays]() | Easy | | [solution](<../_1460. Make Two Arrays Equal by Reversing Subarrays.md>) | c | Jun 04, 2024 | | 1470 | [Shuffle the Array]() | Easy | | [solution](<../_1470. Shuffle the Array.md>) | c | Jun 03, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<../_1472. Design Browser History.md>) | py | Jul 05, 2024 | | 1481 | [Least Number of Unique Integers after K Removals]() | Medium | | [solution](<../_1481. Least Number of Unique Integers after K Removals.md>) | py | Jun 15, 2024 | | 1482 | [Minimum Number of Days to Make m Bouquets]() | Medium | | [solution](<../_1482. Minimum Number of Days to Make m Bouquets.md>) | py | Jun 18, 2024 | | 1502 | [Can Make Arithmetic Progression From Sequence]() | Easy | | [solution](<../_1502. Can Make Arithmetic Progression From Sequence.md>) | py, c | Jun 04, 2024 | diff --git a/markdowns/by_topic/Data Stream.md b/markdowns/by_topic/Data Stream.md index d31c0c78..26d1e409 100644 --- a/markdowns/by_topic/Data Stream.md +++ b/markdowns/by_topic/Data Stream.md @@ -1,9 +1,10 @@ -# [Data Stream]() (1 completed) +# [Data Stream]() (2 completed) *[Back to top](<../../README.md>)* ------ -| # | Title | Level | Cats | Solution | Languages | Date Complete | -|----:|:---------------------------------------------------------------------------------------------------|:--------|:-------|:----------------------------------------------------------|:------------|:----------------| -| 703 | [Kth Largest Element in a Stream]() | Easy | N150 | [solution](<../_703. Kth Largest Element in a Stream.md>) | py | Jul 04, 2024 | \ No newline at end of file +| # | Title | Level | Cats | Solution | Languages | Date Complete | +|-----:|:---------------------------------------------------------------------------------------------------|:--------|:-------|:----------------------------------------------------------|:------------|:----------------| +| 703 | [Kth Largest Element in a Stream]() | Easy | N150 | [solution](<../_703. Kth Largest Element in a Stream.md>) | py | Jul 04, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<../_1472. Design Browser History.md>) | py | Jul 05, 2024 | \ No newline at end of file diff --git a/markdowns/by_topic/Design.md b/markdowns/by_topic/Design.md index 2fba4a7a..44ed624f 100644 --- a/markdowns/by_topic/Design.md +++ b/markdowns/by_topic/Design.md @@ -1,4 +1,4 @@ -# [Design]() (22 completed) +# [Design]() (25 completed) *[Back to top](<../../README.md>)* @@ -19,11 +19,14 @@ | 381 | [Insert Delete GetRandom O(1) - Duplicates allowed]() | Hard | | [solution](<../_381. Insert Delete GetRandom O(1) - Duplicates allowed.md>) | java | Jul 05, 2024 | | 431 | [Encode N-ary Tree to Binary Tree]() | Hard | | [solution](<../_431. Encode N-ary Tree to Binary Tree.md>) | py, java | Jun 28, 2024 | | 535 | [Encode and Decode TinyURL]() | Medium | | [solution](<../_535. Encode and Decode TinyURL.md>) | py | Jun 07, 2024 | +| 622 | [Design Circular Queue]() | Medium | | [solution](<../_622. Design Circular Queue.md>) | py | Jul 05, 2024 | | 676 | [Implement Magic Dictionary]() | Medium | | [solution](<../_676. Implement Magic Dictionary.md>) | py | Jun 27, 2024 | | 703 | [Kth Largest Element in a Stream]() | Easy | N150 | [solution](<../_703. Kth Largest Element in a Stream.md>) | py | Jul 04, 2024 | | 707 | [Design Linked List]() | Medium | | [solution](<../_707. Design Linked List.md>) | java | Jun 21, 2024 | | 919 | [Complete Binary Tree Inserter]() | Medium | | [solution](<../_919. Complete Binary Tree Inserter.md>) | py | Jul 05, 2024 | | 1166 | [Design File System]() | Medium | | [solution](<../_1166. Design File System.md>) | py | Jun 27, 2024 | +| 1381 | [Design a Stack With Increment Operation]() | Medium | | [solution](<../_1381. Design a Stack With Increment Operation.md>) | py | Jul 05, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<../_1472. Design Browser History.md>) | py | Jul 05, 2024 | | 1570 | [Dot Product of Two Sparse Vectors]() | Medium | | [solution](<../_1570. Dot Product of Two Sparse Vectors.md>) | py, java, c | Jun 06, 2024 | | 1804 | [Implement Trie II (Prefix Tree)]() | Medium | | [solution](<../_1804. Implement Trie II (Prefix Tree).md>) | py | Jun 27, 2024 | | 2013 | [Detect Squares]() | Medium | N150 | [solution](<../_2013. Detect Squares.md>) | py | Jun 28, 2024 | diff --git a/markdowns/by_topic/Doubly-Linked List.md b/markdowns/by_topic/Doubly-Linked List.md index 189cf535..a7b23fb3 100644 --- a/markdowns/by_topic/Doubly-Linked List.md +++ b/markdowns/by_topic/Doubly-Linked List.md @@ -1,10 +1,11 @@ -# [Doubly-Linked List]() (2 completed) +# [Doubly-Linked List]() (3 completed) *[Back to top](<../../README.md>)* ------ -| # | Title | Level | Cats | Solution | Languages | Date Complete | -|----:|:---------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:-------|:----------------------------------------------------------------------------------|:------------|:----------------| -| 426 | [Convert Binary Search Tree to Sorted Doubly Linked List]() | Medium | | [solution](<../_426. Convert Binary Search Tree to Sorted Doubly Linked List.md>) | py | Jun 11, 2024 | -| 430 | [Flatten a Multilevel Doubly Linked List]() | Medium | | [solution](<../_430. Flatten a Multilevel Doubly Linked List.md>) | java, cpp | Jun 26, 2024 | \ No newline at end of file +| # | Title | Level | Cats | Solution | Languages | Date Complete | +|-----:|:---------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:-------|:----------------------------------------------------------------------------------|:------------|:----------------| +| 426 | [Convert Binary Search Tree to Sorted Doubly Linked List]() | Medium | | [solution](<../_426. Convert Binary Search Tree to Sorted Doubly Linked List.md>) | py | Jun 11, 2024 | +| 430 | [Flatten a Multilevel Doubly Linked List]() | Medium | | [solution](<../_430. Flatten a Multilevel Doubly Linked List.md>) | java, cpp | Jun 26, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<../_1472. Design Browser History.md>) | py | Jul 05, 2024 | \ No newline at end of file diff --git a/markdowns/by_topic/Hash Table.md b/markdowns/by_topic/Hash Table.md index 31e25e47..e66e968a 100644 --- a/markdowns/by_topic/Hash Table.md +++ b/markdowns/by_topic/Hash Table.md @@ -1,4 +1,4 @@ -# [Hash Table]() (88 completed) +# [Hash Table]() (89 completed) *[Back to top](<../../README.md>)* @@ -30,6 +30,7 @@ | 451 | [Sort Characters By Frequency]() | Medium | | [solution](<../_451. Sort Characters By Frequency.md>) | py | Jun 17, 2024 | | 523 | [Continuous Subarray Sum]() | Medium | Daily | [solution](<../_523. Continuous Subarray Sum.md>) | py | Jun 08, 2024 | | 535 | [Encode and Decode TinyURL]() | Medium | | [solution](<../_535. Encode and Decode TinyURL.md>) | py | Jun 07, 2024 | +| 567 | [Permutation in String]() | Medium | N150 | [solution](<../_567. Permutation in String.md>) | py | Jul 05, 2024 | | 616 | [Add Bold Tag in String]() | Medium | | [solution](<../_616. Add Bold Tag in String.md>) | py | Jun 27, 2024 | | 645 | [Set Mismatch]() | Easy | | [solution](<../_645. Set Mismatch.md>) | py, java | Jun 01, 2024 | | 648 | [Replace Words]() | Medium | Daily | [solution](<../_648. Replace Words.md>) | py, java | Jun 06, 2024 | diff --git a/markdowns/by_topic/Linked List.md b/markdowns/by_topic/Linked List.md index c7aac228..37adfce6 100644 --- a/markdowns/by_topic/Linked List.md +++ b/markdowns/by_topic/Linked List.md @@ -1,4 +1,4 @@ -# [Linked List]() (38 completed) +# [Linked List]() (40 completed) *[Back to top](<../../README.md>)* @@ -25,6 +25,7 @@ | 382 | [Linked List Random Node]() | Medium | | [solution](<../_382. Linked List Random Node.md>) | py, java, c | Jun 21, 2024 | | 426 | [Convert Binary Search Tree to Sorted Doubly Linked List]() | Medium | | [solution](<../_426. Convert Binary Search Tree to Sorted Doubly Linked List.md>) | py | Jun 11, 2024 | | 430 | [Flatten a Multilevel Doubly Linked List]() | Medium | | [solution](<../_430. Flatten a Multilevel Doubly Linked List.md>) | java, cpp | Jun 26, 2024 | +| 622 | [Design Circular Queue]() | Medium | | [solution](<../_622. Design Circular Queue.md>) | py | Jul 05, 2024 | | 707 | [Design Linked List]() | Medium | | [solution](<../_707. Design Linked List.md>) | java | Jun 21, 2024 | | 708 | [Insert into a Sorted Circular Linked List]() | Medium | | [solution](<../_708. Insert into a Sorted Circular Linked List.md>) | py | Jun 21, 2024 | | 725 | [Split Linked List in Parts]() | Medium | | [solution](<../_725. Split Linked List in Parts.md>) | py | Jun 21, 2024 | @@ -32,6 +33,7 @@ | 1019 | [Next Greater Node In Linked List]() | Medium | | [solution](<../_1019. Next Greater Node In Linked List.md>) | py | Jun 21, 2024 | | 1171 | [Remove Zero Sum Consecutive Nodes from Linked List]() | Medium | | [solution](<../_1171. Remove Zero Sum Consecutive Nodes from Linked List.md>) | py | Jun 15, 2024 | | 1265 | [Print Immutable Linked List in Reverse]() | Medium | | [solution](<../_1265. Print Immutable Linked List in Reverse.md>) | py, c | Jun 06, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<../_1472. Design Browser History.md>) | py | Jul 05, 2024 | | 1474 | [Delete N Nodes After M Nodes of a Linked List]() | Easy | | [solution](<../_1474. Delete N Nodes After M Nodes of a Linked List.md>) | java, c | Jun 06, 2024 | | 1669 | [Merge In Between Linked Lists]() | Medium | | [solution](<../_1669. Merge In Between Linked Lists.md>) | c | Jun 24, 2024 | | 1721 | [Swapping Nodes in a Linked List]() | Medium | | [solution](<../_1721. Swapping Nodes in a Linked List.md>) | c | Jun 06, 2024 | diff --git a/markdowns/by_topic/Queue.md b/markdowns/by_topic/Queue.md index a2a30175..893cdad8 100644 --- a/markdowns/by_topic/Queue.md +++ b/markdowns/by_topic/Queue.md @@ -1,4 +1,4 @@ -# [Queue]() (8 completed) +# [Queue]() (9 completed) *[Back to top](<../../README.md>)* @@ -9,6 +9,7 @@ | 239 | [Sliding Window Maximum]() | Hard | N150 | [solution](<../_239. Sliding Window Maximum.md>) | py | Jun 03, 2024 | | 341 | [Flatten Nested List Iterator]() | Medium | | [solution](<../_341. Flatten Nested List Iterator.md>) | java | Jul 02, 2024 | | 353 | [Design Snake Game]() | Medium | | [solution](<../_353. Design Snake Game.md>) | py | Jun 28, 2024 | +| 622 | [Design Circular Queue]() | Medium | | [solution](<../_622. Design Circular Queue.md>) | py | Jul 05, 2024 | | 950 | [Reveal Cards In Increasing Order]() | Medium | | [solution](<../_950. Reveal Cards In Increasing Order.md>) | py | Jun 11, 2024 | | 995 | [Minimum Number of K Consecutive Bit Flips]() | Hard | Daily | [solution](<../_995. Minimum Number of K Consecutive Bit Flips.md>) | py, c | Jun 23, 2024 | | 1438 | [Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit]() | Medium | Daily | [solution](<../_1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.md>) | java | Jun 22, 2024 | diff --git a/markdowns/by_topic/Sliding Window.md b/markdowns/by_topic/Sliding Window.md index 7b5e19eb..2474eda2 100644 --- a/markdowns/by_topic/Sliding Window.md +++ b/markdowns/by_topic/Sliding Window.md @@ -1,4 +1,4 @@ -# [Sliding Window]() (11 completed) +# [Sliding Window]() (12 completed) *[Back to top](<../../README.md>)* @@ -9,6 +9,7 @@ | 3 | [Longest Substring Without Repeating Characters]() | Medium | N150, B75 | [solution](<../_3. Longest Substring Without Repeating Characters.md>) | java | May 22, 2024 | | 239 | [Sliding Window Maximum]() | Hard | N150 | [solution](<../_239. Sliding Window Maximum.md>) | py | Jun 03, 2024 | | 424 | [Longest Repeating Character Replacement]() | Medium | N150, B75 | [solution](<../_424. Longest Repeating Character Replacement.md>) | py | Jul 05, 2024 | +| 567 | [Permutation in String]() | Medium | N150 | [solution](<../_567. Permutation in String.md>) | py | Jul 05, 2024 | | 713 | [Subarray Product Less Than K]() | Medium | | [solution](<../_713. Subarray Product Less Than K.md>) | py | Jul 01, 2024 | | 930 | [Binary Subarrays With Sum]() | Medium | | [solution](<../_930. Binary Subarrays With Sum.md>) | py | Jun 21, 2024 | | 995 | [Minimum Number of K Consecutive Bit Flips]() | Hard | Daily | [solution](<../_995. Minimum Number of K Consecutive Bit Flips.md>) | py, c | Jun 23, 2024 | diff --git a/markdowns/by_topic/Stack.md b/markdowns/by_topic/Stack.md index 7c8c08bd..4da33012 100644 --- a/markdowns/by_topic/Stack.md +++ b/markdowns/by_topic/Stack.md @@ -1,4 +1,4 @@ -# [Stack]() (35 completed) +# [Stack]() (37 completed) *[Back to top](<../../README.md>)* @@ -35,6 +35,8 @@ | 1209 | [Remove All Adjacent Duplicates in String II]() | Medium | | [solution](<../_1209. Remove All Adjacent Duplicates in String II.md>) | py | Jun 17, 2024 | | 1249 | [Minimum Remove to Make Valid Parentheses]() | Medium | | [solution](<../_1249. Minimum Remove to Make Valid Parentheses.md>) | py | Jun 10, 2024 | | 1265 | [Print Immutable Linked List in Reverse]() | Medium | | [solution](<../_1265. Print Immutable Linked List in Reverse.md>) | py, c | Jun 06, 2024 | +| 1381 | [Design a Stack With Increment Operation]() | Medium | | [solution](<../_1381. Design a Stack With Increment Operation.md>) | py | Jul 05, 2024 | +| 1472 | [Design Browser History]() | Medium | | [solution](<../_1472. Design Browser History.md>) | py | Jul 05, 2024 | | 1700 | [Number of Students Unable to Eat Lunch]() | Easy | | [solution](<../_1700. Number of Students Unable to Eat Lunch.md>) | py | Jun 01, 2024 | | 1762 | [Buildings With an Ocean View]() | Medium | | [solution](<../_1762. Buildings With an Ocean View.md>) | py | Jun 10, 2024 | | 2130 | [Maximum Twin Sum of a Linked List]() | Medium | | [solution](<../_2130. Maximum Twin Sum of a Linked List.md>) | py | Jun 20, 2024 | diff --git a/markdowns/by_topic/String.md b/markdowns/by_topic/String.md index cd10dc2e..feb6fcd9 100644 --- a/markdowns/by_topic/String.md +++ b/markdowns/by_topic/String.md @@ -1,4 +1,4 @@ -# [String]() (87 completed) +# [String]() (88 completed) *[Back to top](<../../README.md>)* @@ -37,6 +37,7 @@ | 424 | [Longest Repeating Character Replacement]() | Medium | N150, B75 | [solution](<../_424. Longest Repeating Character Replacement.md>) | py | Jul 05, 2024 | | 451 | [Sort Characters By Frequency]() | Medium | | [solution](<../_451. Sort Characters By Frequency.md>) | py | Jun 17, 2024 | | 535 | [Encode and Decode TinyURL]() | Medium | | [solution](<../_535. Encode and Decode TinyURL.md>) | py | Jun 07, 2024 | +| 567 | [Permutation in String]() | Medium | N150 | [solution](<../_567. Permutation in String.md>) | py | Jul 05, 2024 | | 616 | [Add Bold Tag in String]() | Medium | | [solution](<../_616. Add Bold Tag in String.md>) | py | Jun 27, 2024 | | 647 | [Palindromic Substrings]() | Medium | N150, B75 | [solution](<../_647. Palindromic Substrings.md>) | py | Jul 04, 2024 | | 648 | [Replace Words]() | Medium | Daily | [solution](<../_648. Replace Words.md>) | py, java | Jun 06, 2024 | diff --git a/markdowns/by_topic/Two Pointers.md b/markdowns/by_topic/Two Pointers.md index 0f4447d7..59da8fcb 100644 --- a/markdowns/by_topic/Two Pointers.md +++ b/markdowns/by_topic/Two Pointers.md @@ -1,4 +1,4 @@ -# [Two Pointers]() (43 completed) +# [Two Pointers]() (44 completed) *[Back to top](<../../README.md>)* @@ -30,6 +30,7 @@ | 392 | [Is Subsequence]() | Easy | | [solution](<../_392. Is Subsequence.md>) | java | Jun 21, 2024 | | 408 | [Valid Word Abbreviation]() | Easy | | [solution](<../_408. Valid Word Abbreviation.md>) | py | Jun 02, 2024 | | 475 | [Heaters]() | Medium | | [solution](<../_475. Heaters.md>) | py | Jun 07, 2024 | +| 567 | [Permutation in String]() | Medium | N150 | [solution](<../_567. Permutation in String.md>) | py | Jul 05, 2024 | | 611 | [Valid Triangle Number]() | Medium | | [solution](<../_611. Valid Triangle Number.md>) | py | May 22, 2024 | | 647 | [Palindromic Substrings]() | Medium | N150, B75 | [solution](<../_647. Palindromic Substrings.md>) | py | Jul 04, 2024 | | 826 | [Most Profit Assigning Work]() | Medium | Daily | [solution](<../_826. Most Profit Assigning Work.md>) | py | Jun 17, 2024 | diff --git a/my-submissions/m1381.md b/my-submissions/m1381.md new file mode 100644 index 00000000..208b0eab --- /dev/null +++ b/my-submissions/m1381.md @@ -0,0 +1,3 @@ +## This question has the honour of being my 400th question on LeetCode. + +**On Day 39 of my grind, 8:35PM PST, this question was done. Simple one to end off this sprint. :)** \ No newline at end of file diff --git a/my-submissions/m1381.py b/my-submissions/m1381.py new file mode 100644 index 00000000..6641c97b --- /dev/null +++ b/my-submissions/m1381.py @@ -0,0 +1,26 @@ +class CustomStack: + + def __init__(self, maxSize: int): + self.stk = [] + self.maxx = maxSize + + def push(self, x: int) -> None: + if len(self.stk) >= self.maxx : + return + self.stk.append(x) + + def pop(self) -> int: + if not self.stk : + return -1 + return self.stk.pop() + + def increment(self, k: int, val: int) -> None: + for i in range(min(k, len(self.stk))) : + self.stk[i] += val + + +# Your CustomStack object will be instantiated and called as such: +# obj = CustomStack(maxSize) +# obj.push(x) +# param_2 = obj.pop() +# obj.increment(k,val) \ No newline at end of file diff --git a/my-submissions/m1472.py b/my-submissions/m1472.py new file mode 100644 index 00000000..42e0523c --- /dev/null +++ b/my-submissions/m1472.py @@ -0,0 +1,26 @@ +class BrowserHistory: + + def __init__(self, homepage: str): + self.history = [homepage] + self.spot = 0 + + def visit(self, url: str) -> None: + self.spot += 1 + if self.spot < len(self.history) : + self.history = self.history[:self.spot] + self.history.append(url) + + def back(self, steps: int) -> str: + self.spot = max(0, self.spot - steps) + return self.history[self.spot] + + def forward(self, steps: int) -> str: + self.spot = min(len(self.history) - 1, self.spot + steps) + return self.history[self.spot] + + +# Your BrowserHistory object will be instantiated and called as such: +# obj = BrowserHistory(homepage) +# obj.visit(url) +# param_2 = obj.back(steps) +# param_3 = obj.forward(steps) \ No newline at end of file diff --git a/my-submissions/m567 v1 Counter Equivalence.py b/my-submissions/m567 v1 Counter Equivalence.py new file mode 100644 index 00000000..d5a6eaf4 --- /dev/null +++ b/my-submissions/m567 v1 Counter Equivalence.py @@ -0,0 +1,12 @@ +class Solution: + def checkInclusion(self, s1: str, s2: str) -> bool: + cnt1 = Counter(s1) + cnt2 = Counter(s2[:len(s1) - 1]) + + for cL, cR in zip(s2, s2[len(s1) - 1 : len(s2)]) : + cnt2[cR] += 1 + if cnt1 == cnt2 : + return True + cnt2[cL] -= 1 + + return False \ No newline at end of file diff --git a/my-submissions/m567 v2 Running Counter.py b/my-submissions/m567 v2 Running Counter.py new file mode 100644 index 00000000..7bd0b113 --- /dev/null +++ b/my-submissions/m567 v2 Running Counter.py @@ -0,0 +1,37 @@ +class Solution: + def checkInclusion(self, s1: str, s2: str) -> bool: + cnt1 = [0] * 26 + cnt2 = [0] * 26 + + if len(s1) > len(s2) : + return False + + for c1, c2 in zip(s1, s2) : + cnt1[ord(c1) - ord('a')] += 1 + cnt2[ord(c2) - ord('a')] += 1 + + matches = [cnt1[x] == cnt2[x] for x in range(len(cnt1))].count(True) + + if matches == 26 : + return True + for cL, cR in zip(s2, s2[len(s1):]) : + cnt2[ord(cL) - ord('a')] -= 1 + cnt2[ord(cR) - ord('a')] += 1 + + if cL == cR : + continue + + if cnt2[ord(cR) - ord('a')] == cnt1[ord(cR) - ord('a')] : + matches += 1 + elif cnt2[ord(cR) - ord('a')] == cnt1[ord(cR) - ord('a')] + 1 : + matches -= 1 + + if cnt2[ord(cL) - ord('a')] == cnt1[ord(cL) - ord('a')] : + matches += 1 + elif cnt2[ord(cL) - ord('a')] == cnt1[ord(cL) - ord('a')] - 1 : + matches -= 1 + + if matches == 26 : + return True + + return False \ No newline at end of file diff --git a/my-submissions/m567.md b/my-submissions/m567.md new file mode 100644 index 00000000..34230c6f --- /dev/null +++ b/my-submissions/m567.md @@ -0,0 +1,37 @@ + +#### Version 1 + +In version 1, I just went with a straightforward counter approach. +Since `Python` `Counter` objects have a built-in comparing method, +I decided to simply use that. It's still technically $O(n)$ in total +since the counter size will be at most 26 (since it's only counting) +letters, but it's still less than ideal. + +#### Version 2 + +In version 2, I swapped over to, while still using a counter, only +committing a comparison for the values that are being updated. That is, +the values at the front of the window being added and the values +are the end of the window that are being dropped. We have to find a case +where all values line up, so we iterate initially for the first `len(s1)` +letters then proceed with only incremental updates from that point onwards. + + +#### Fun Testcase + +When creating version 2, I encountered a very fun test case: +``` +s1 = "trinitrophenylmethylnitramine" +s2 = "dinitrophenylhydrazinetrinitrophenylmethylnitramine" +``` +Clearly, `s1` matches the ending of `s2` exactly, but my original +code for version 2 (the incomplete `v2`) was resulting in `False`. +Eventually, I realized that the cause was that during addings and +subtractions of the same letter, it would **cause match to increment by 2** +due to the the "new values" equallying the expected values in `s1`'s counter. +This would only happen when the values where originally the correct +value when it occured. + +I added the `if cL == cR : continue` check and that was resolved quickly +(as a if the previous wasn't a solution and didn't return, then this clearly +wouldn't be a solution either since it's equal in the letter counts). \ No newline at end of file diff --git a/my-submissions/m622.py b/my-submissions/m622.py new file mode 100644 index 00000000..ae858098 --- /dev/null +++ b/my-submissions/m622.py @@ -0,0 +1,52 @@ +class MyCircularQueue: + + def __init__(self, k: int): + self.vals = [0] * k + self.size = 0 + self.pop = 0 + self.add = 0 + self.k = k + + def enQueue(self, value: int) -> bool: + if self.size >= self.k : + return False + if self.size == 0 : + self.pop = 0 + self.add = 0 + self.vals[0] = value + else : + self.add = (self.add + 1) % self.k + self.vals[self.add] = value + self.size += 1 + return True + + def deQueue(self) -> bool: + if self.isEmpty() : + return False + + if self.pop != self.add : + self.pop = (self.pop + 1) % self.k + self.size -= 1 + return True + + def Front(self) -> int: + return self.vals[self.pop] if self.size > 0 else -1 + + def Rear(self) -> int: + return self.vals[self.add] if self.size > 0 else -1 + + def isEmpty(self) -> bool: + return self.size == 0 + + def isFull(self) -> bool: + return self.size == self.k + + +# Your MyCircularQueue object will be instantiated and called as such: +# obj = MyCircularQueue(k) +# param_1 = obj.enQueue(value) +# param_2 = obj.deQueue() +# param_3 = obj.Front() +# param_4 = obj.Rear() +# param_5 = obj.isEmpty() +# param_6 = obj.isFull() \ No newline at end of file