Skip to content

Instantly share code, notes, and snippets.

View doyedele1's full-sized avatar

Demilade Oyedele doyedele1

View GitHub Profile
@doyedele1
doyedele1 / solution.py
Created October 25, 2023 22:56
79. Word Search
'''
Explanation: Recursive DFS (Backtracking) Solution
* We can't reuse a character that we've visited before
So, let's do the backtracking brute-force as a human would do
word = "ABCCED"
We check if we have an A in the board since that's the first letter in the word
Yes, we do. Then we check all possible neighbors of A and check if we have a B
TC: O(rc * dfs) = O(rc * 4 ^ len(word))
@doyedele1
doyedele1 / solution.py
Created October 25, 2023 23:14
56. Merge Intervals
'''
Explanation:
[[1,3], [8, 10], [15, 18], [2,6]]
1-----3
8------10
15--------18
2------6
- Sort intervals based on the start times
[[1,3], [2,6], [8, 10], [15, 18]]
- Add the first interval to the res nested array
@doyedele1
doyedele1 / solution.py
Last active October 25, 2023 23:57
3. Longest Substring Without Repeating Characters
'''
Explanation I: Naive Solution
- Enumerate the string and check all the substrings. Check the longest substring that have no repeating characters
- Update the result to the count of the longest substring with no repeating characters
abcabcbb
- From a, we check the substring starting from a
- From b, we check the substring starting from b
abcabcbb
abca --> has repeating characters. We do not need to check the next substring, which is abcab because we already have duplicates from the previous iteration.
@doyedele1
doyedele1 / solution.py
Last active January 5, 2025 23:20
2381. Shifting Letters II
class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
n = len(s)
arr = [0] * (n + 1)
for shift in shifts:
start, end, direction = shift[0], shift[1], shift[2]
if direction == 1:
arr[start] += 1
arr[end + 1] -= 1