leetcode.com , algoexpert.io solutions in python and swift
[![GitHub last commit][last-commit-shield]][last-commit-url] [![GitHub repo size in bytes][repo-size-shield]][repo-size-url] [![Contributors][contributors-shield]][contributors-url] [![Forks][forks-shield]][forks-url] [![Stargazers][stars-shield]][stars-url] [![Issues][issues-shield]][issues-url] [![MIT License][license-shield]][license-url] [![LinkedIn][linkedin-shield]][linkedin-url] []() [
]()
This repo contains around 700 (600 Leetcode.com and 100 Algoexpert.io) problems in total, with solutions using Swift and Python
This repo contains my solutions to algorithmic problems in leetcode.com and algoexpert.io written in Swift and Python.
Show your support by giving it a STAR
About โข Topics โข Tips โข LeetCode.com โข AlgoExpert.io โข References
About
I have solved quite a number of problems from several topics. See the below table for further details.
Topics
- Binary Search
- Binary Search Tree
- Binary Tree(Segment Tree)
- N-aray Tree(Trie, Binary Indexed Tree)
- Graph(Dijkstra, Union Find, Kruskal, Prim's, Minimum Spanning Tree, Topological Ordering...etc)
- Stack
- Queue
- Array
- Sorting
- Hash Table
- Heap
- Linked list
- Bit Operation
- Dynamic programming
- Backtracking(Permutations & Combinations & Subsets...etc)
- Math
- and more...
Questions from
My other related repos
- Coding Interview Patterns
- Coding Interview Gym
- iOS Interview Gym
- Behavioural Interview Gym
- System Design Interview Gym
- Object Oriented Design Interview Gym
My leetcode account
Tips
- Check this Golden post first.
- Whenever you solve a new question with some new techniques/algorithms, try to solve atleast 2 similar problem in a row. This way, your understanding to the new techniques/algorithms will be better.
- Solve all leetcode cards
LeetCode
1. Arrays , HashMap, Set
Leetcode problems with solutions and tutorials/videos
| # | Title | Solution | Tutorial | Level | Remarks | | --- | --- | --- | --- | --- | --- | |01| 1. Two Sum| Python, Swift | --- | --- | โ ๐ โญ ๐ญ ๐ง ๐ | |02| 485. Max Consecutive Ones| Swift| |03| 476. Number Complement| Swift| |04| 461. Hamming Distance| Swift| |05| 9. Palindrome Number| Python| |06| 88. Merge Sorted Array| Python| |07| 811. Subdomain Visit Count| Python| |08| 771. Jewels and Stones| Python| |09| 414. Third Maximum Number| Python| |10| 259. 3Sum Smaller| Python| |11| 16. 3Sum Closest| Python| |12| 15. 3Sum| Python| |13| 118. Pascal's Triangle| Python| |14| 119. Pascal's Triangle II| Python| |15| 238. Product of Array Except Self| Python, Swift | |16| 724. Find Pivot Index| Python| | |17| 747. Largest Number At Least Twice of Others| Python| | |18| 581. Shortest Unsorted Continuous Subarray| Python| | |19| 904. Fruit Into Baskets| Python| Article, Video 1, Video 2| Medium | Sliding Window, Two Pointer | |20| 56. Merge Intervals| Python| Article, Swift | |21| 334. Increasing Triplet Subsequence| Python| --- | Medium | --- | |22| 792. Number of Matching Subsequences| Python| Official | Medium | Very tricky. Check again | |23| 912. Sort an Array (Merge Sort)| Python.py)|--- | Medium | --- | |24| 1118. Number of Days in a Month| Python|--- | Easy | --- | |25| 349. Intersection of Two Arrays| Python| Art 1, Art 2 | Easy | Very tricky and versatile | |26| 350. Intersection of Two Arrays II| Python| [](), [](), []() | Easy | Very tricky and versatile | |27| 303. Range Sum Query - Immutable| Python| Vid 1 | Easy | --- | |28| 560. Subarray Sum Equals K| Python, Swift | Art 1, Art 2, Art 3, Art 4, Art 5, Art 6 | Medium | Very tricky and persuasive with Sliding Window but it's not. This is a classic running sum problem | |29| 325. Maximum Size Subarray Sum Equals k| Python| Art 1 | Medium | Very tricky and persuasive with Sliding Window but it's not. This is a classic running sum problem | |30| 1074. Number of Submatrices That Sum to Target| Python| Art 1, Art 2, Art 3, Art 4 | Extremely Hard | Very tricky and hard, check again. Also DP | |31| 167. Two Sum II - Input array is sorted| Python| --- | Easy | Two pointer basics | |32| 1099. Two Sum Less Than K| Python| --- | Easy | Two pointer basics | |33| 26. Remove Duplicates from Sorted Array| Python| --- | Easy | Two pointer basics | |34| 977. Squares of a Sorted Array| Python| --- | Easy | Two pointer basics | |35| 713. Subarray Product Less Than K| Python| --- | Medium | Two pointer ad Sliding Window | |36| 75. Sort Colors| Python| Vid 1 | Medium | Two pointer | |37| 18. 4Sum| Python| --- | Medium | Two pointer | |38| 11. Container With Most Water| Python| --- | Medium | Two pointer | |39| 202. Happy Number| Python| Art 1 | Easy | Fast pointer Slow pointer | |40| 457. Circular Array Loop| Python| --- | Medium | Fast pointer Slow pointer. Check again | |41| 448. Find All Numbers Disappeared in an Array| Python| --- | Easy | Cyclic Sort | |42| 287. Find the Duplicate Number| Python| --- | Medium | Cyclic Sort. TODO: Check all other approaches | |43| 442. Find All Duplicates in an Array| Python| --- | Medium | Cyclic Sort TODO: Check all other approaches | |44| 41. First Missing Positive| Python| --- | Hard | Cyclic Sort, Very important | |45| 939. Minimum Area Rectangle | Python| Art 1 | Medium | Hash and Set, Very important | |46| 359. Logger Rate Limiter | Python| --- | Easy | Hash and Set, Very important | |47| 48. Rotate Image | Python| Must, Vid 1 | Medium | Very important | |48| 362. Design Hit Counter | Python, Swift| --- | Medium | --- | |49| 289. Game of Life | Python, Swift| --- | Medium | --- | |50| 54. Spiral Matrix | Python, Swift| Official, Algoexpert.io | Medium | --- | |51| 380. Insert Delete GetRandom O(1) | Python.py), Swift.swift)| Official | Medium | --- | |52| 835. Image Overlap | Python, Swift| Art 1, Art 2 | Medium | --- | |53| 1051. Height Checker | Python, Swift | --- | Easy | | |55| 683. K Empty Slots | Python, Swift | Art 1, Art 2, Art 3 | Hard | TODO: Check monotonic queue approach. Solved it with sliding widow | |56| 849. Maximize Distance to Closest Person | Python, Swift | Art 1 | Easy | Not so easy and intuitive. Can be solve is a variety of way. I have used Binary search(A bit over engineered solution). Can be simplifies | |57| 283. Move Zeroes | Python, Swift | --- | Easy | Not so easy and intuitive. Uses fast and slow pointer | |58| 163. Missing Ranges | Python, Swift | --- | Medium | Man, it's a very trick problem | |59| 1089. Duplicate Zeros | Python, Swift | Art 1 | Easy | Not so easy and intuitive. Check again | |60| 941. Valid Mountain Array | Python, Swift | | Easy | | |61| 731. My Calendar II | Python, Swift | | Medium | Merge interval | |62| 59. Spiral Matrix II | Python, Swift | | Medium | loved it | |63| 525. Contiguous Array | Python, Swift | Art 1 | Medium | loved it. Check again | |64| 379. Design Phone Directory | Python, Swift | | Medium | | |65| 280. Wiggle Sort | Python, Swift | | Medium | | |66| 246. Strobogrammatic Number | Python, Swift | | Easy | | |67| 845. Longest Mountain in Array | Python, Swift | | Medium | | |68| 66. Plus One | Python, Swift | | Easy | | |69| 957. Prison Cells After N Days | Python, Swift | Art 1, Art 2, Art 3, Art 4, Art 5 | Medium | | |70| 1232. Check If It Is a Straight Line | Python, Swift | | Easy | | |71| 348. Design Tic-Tac-Toe | Python, Swift | Vid 1, Art 1 | Medium | A fucking tricky question | |72| 1152. Analyze User Website Visit Pattern | Python, Swift | Art 1, Art 2, Art 3, Art 4 | Medium | A fucking unclear question and problem. I Fucking disliked it. | |73| 953. Verifying an Alien Dictionary | Python, Swift | | Easy(REALLY!!!) | Don't be fooled by the difficulty label | |74| 463. Island Perimeter | Python, Swift | | Easy | It's very deceiving, to think it as a grapy ad DFS problem, but it's not. Think simply | |75| 166. Fraction to Recurring Decimal | Python, Swift | Art 1 | Medium | Hate this problem. Why do companies ask this shit!!! | |75| 311. Sparse Matrix Multiplication | Python, Swift | Art 1, Art 2, Art 3, Art 4 | Medium | Very tricky | |76| 896. Monotonic Array | Python, Swift | --- | Eassy | | |77| 670. Maximum Swap | Python, Swift | Art 1 | Medium | | |78| 825. Friends Of Appropriate Ages | Python, Swift | Art 1 | Medium | Think differently from a different angle. Loved this problem | |79| 189. Rotate Array | Python, Swift | --- | Easy | Getting to inplace (O(n) time and O(1) space) solution is tricky. | |80| 1004. Max Consecutive Ones III | Python, Swift | --- | Medium | Solution is tricky. | |81| 498. Diagonal Traverse | Python, Swift | --- | Medium | --- | |82| 766. Toeplitz Matrix | Python, Swift | --- | Easy | --- | |83| 1031. Maximum Sum of Two Non-Overlapping Subarrays | Python, Swift | --- | Medium | PrefixSum | |84| 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | Python, Swift | (Art 1)[https://tinyurl.com/y69hjhjp], (Art 2)[https://tinyurl.com/y3h2fj2m] | Medium | Sliding window | |85| 1428. Leftmost Column with at Least a One | Python, Swift | --- | Medium | Binary Search | |86| 914. X of a Kind in a Deck of Cards | Python, Swift | --- | Easy (Not So) | | |87| 1275. Find Winner on a Tic Tac Toe Game | Python, Swift | Art 1 | Easy (Not So) | | |88| 532. K-diff Pairs in an Array | Python, Swift | Art 1 | Medium | | |89| 128. Longest Consecutive Sequence | Python, Swift | Art 1 | Hard | | |90| 1423. Maximum Points You Can Obtain from Cards | Python, Swift | Art 1, Art 2 | Medium | Very important. Learned new ways of sliding window | |91| 900. RLE Iterator | Python, Swift | --- | Medium | - | |92| 954. Array of Doubled Pairs | Swift | --- | Medium | - | |93| 853. Car Fleet | Swift | Vid 1, Art 1 ] | Medium | - | |94| 1877. Minimize Maximum Pair Sum in Array | Swift | Art 1 ] | Medium | - |
2. String
Leetcode problems with solutions and tutorials/videos
| # | Title | Solution | Tutorial | Level | Remarks | | --- | --- | --- | --- | --- | --- | |01| 3. Longest Substring Without Repeating Characters| | |02| 412. Fizz Buzz| Swift| |03| 937. Reorder Log Files| Python| |03| 929. Unique Email Addresses| Python| |04| 7. Reverse Integer| Python| |05| 13. Roman to Integer| Python| |06| 125. Valid Palindrome| Python, Swift | |07| 161. One Edit Distance| Python| |08| 1119. Remove Vowels from a String| Python| |09| 344. Reverse String| Python| |10| 482. License Key Formatting| Python| | |11| 3. Longest Substring Without Repeating Characters| Python| Vid 1, Vid 2 | Medium | ๐ Sliding window, Two pointer | |12| 340. Longest Substring with At Most K Distinct Characters| Python, Swift | Vid 1, Art 1, Art 2 | Hard | ๐ Sliding window, Two pointer | |13| 159. Longest Substring with At Most Two Distinct Characters| Python| Vid 1 | Hard | ๐ Sliding window, Two pointer | |14| 424. Longest Repeating Character Replacement| Python| Vid 1, Vid 2, Art 1, Art 2, Art 3, Art 4 | Medium | ๐ Sliding window, Very important | |15| 76. Minimum Window Substring | Python| Vid 1, Vid 2, Vid 3 | Hard | ๐ Sliding window, Very important | |16| 727. Minimum Window Subsequence| Python| Vid 1, Art 1, Art 2 | Hard | ๐ Sliding window and DP, check the DP approach | |17| 438. Find All Anagrams in a String| Python, Swift | Must, Art 1 | Medium | ๐ Sliding window | |18| 567. Permutation in String| Python| Must | Medium | ๐ Sliding window | |19| 844. Backspace String Compare| Python| --- | Easy | ๐ Two Pointer | |20| 809. Expressive Words| Python| Art 1 | Medium | ๐ Logic ad analytical prolem | |21| 271. Encode and Decode Strings| Python| Art 1 | Medium | ๐ TODO: Check the second approach. Very Important | |22| 299. Bulls and Cows| Python| --- | Easy | --- | |23| 833. Find And Replace in String| Python| Art 1 | Medium | --- | |24| 49. Group Anagrams| Python| Art 1, Algoexpert.io | Medium | --- | |25| 551. Student Attendance Record I| Python| | Easy | --- | |26| 242. Valid Anagram| Python, Swift | | Easy | --- | |27| 1358. Number of Substrings Containing All Three Characters| Python, Swift | Art 1 | Medium | Sliding Window | |28| 459. Repeated Substring Pattern | Python, Swift | Art 1 | Easy | I fucking love this problem | |29| 681. Next Closest Time | Python, Swift | Art 1 | Medium | I feel stupid after trying my first solution | |30| 925. Long Pressed Name | Python, Swift | --- | Easy | | |31| 949. Largest Time for Given Digits | Python, Swift | --- | Easy | Not so easy | |32| 788. Rotated Digits | Python, Swift | --- | Easy | --- | |33| 388. Longest Absolute File Path | Python, Swift | --- | Medium | --- | |34| 616. Add Bold Tag in String | Python, Swift | --- | Medium | Merge intervals(hidden) | |35| 678. Valid Parenthesis String | Python, Swift | Art 1, Art 2, Art 3, Art 4 | Medium | Super tricky. Much check again | |36| 273. Integer to English Words | Python, Swift | | Hard | MUST check again | |37| 763. Partition Labels | Python, Swift | | Medium | | |38| 819. Most Common Word | Python, Swift | Art 1 | Easy | | |39| 350. Intersection of Two Arrays II | Python, Swift | --- | Easy | Check the follow-ups | |40| 249. Group Shifted Strings | Python, Swift | Art 1 | Medium | Tricky one | |41| 158. Read N Characters Given Read4 II - Call multiple times | Python, Swift | Art 1 | Hard | Good one | |42| 415. Add Strings | Python, Swift | Art 1 | Easy | --- | |43| 65. Valid Number | Python, Swift | Art 1 | HARD | A fucking difficult but important problem. Chack DFA approach | |44| 791. Custom Sort String | Python, Swift | --- | Medium | Loved this problem | |45| 157. Read N Characters Given Read4 | Python, Swift | --- | Easy (Not So!) | Loved this problem | |46| 722. Remove Comments | Python, Swift | Art 1 | Medium | | |47| 443. String Compression | Python, Swift | Art 1 | Medium | | |48| 1525NumberofGoodWaystoSplitaString | Swift | --- | Medium | | |48| 2007FindOriginalArrayFromDoubledArray | Swift | --- | Medium | |
3. Linked List
Leetcode problems with solutions and tutorials/videos
| # | Title | Solution | Tutorial | Level | Remarks | | --- | --- | --- | --- | --- | --- | |01| 2. Add Two Numbers| Python, Swift| --- | Medium | Fundamental | |02| 707. Design Linked List| Python| |03| 21. Merge Two Sorted Lists| Python| Algoexpert.io, Vid 1 | Easy | Fundamental and very very important | |04| 234. Palindrome Linked List| Python| --- | Easy | Fast pointer Slow pointer | |05| 24. Swap Nodes in Pairs| Python| |06| 141. Linked List Cycle| Python| --- | Easy | Fast pointer Slow pointer | |07| 142. Linked List Cycle II| Python| Art 1, Art 2, AlgoExpert.io | Medium | Fast pointer Slow pointer | |08| 876. Middle of the Linked List| Python| --- | Easy | Fast pointer Slow pointer | |09| 143. Reorder List| Python| --- | Medium | Fast pointer Slow pointer | |10| 19. Remove Nth Node From End of List| Python| --- | Medium | Fast pointer Slow pointer | |11| 817. Linked List Components| Python| Art 1 | Medium | Uses set/dictionary | |12| 206. Reverse Linked List| Python| Vid 1, Vid 2 | Easy | Fundamental | |13| 92. Reverse Linked List II| Python| educative.io, Art 2 | Medium | Fundamental and very important | |14| 25. Reverse Nodes in k-Group| Python| educative.io, Art 1, Art 2 | Hard | Fundamental. TODO: Check again | |15| 24. Swap Nodes in Pairs| Python| educative.io | Medium | Fundamental | |16| 61. Rotate List| Python| educative.io | Medium | Fundamental | |17| 328. Odd Even Linked List| Python| Art 1 | Medium | Fundamental | |18| 148. Sort List| Python| Vid 1, Vid 2, Vid 3, Art 1 | Medium | Fundamental | |19| 23. Merge k Sorted Lists | Python| educative.io, Art 1 | Hard | Very important. TODO: Check heap approach | |20| 160. Intersection of Two Linked Lists| Python| Art 1, Art 2 | Easy | --- | |21| 138. Copy List with Random Pointer | Python| Vid 1, Art 1 | Medium | TODO: Check again. Very important. Learned a lot of things | |22| 430. Flatten a Multilevel Doubly Linked List | Python| backtobackswe.com, Vid 1, Art 1 | Medium | TODO: Check again. Very important. Learned a lot of things | |23| 146. LRU Cache | Python| Vid 1, backtobackswe.com, algoexpert.io | Hard | TODO: Check again. Very important. Learned a lot of things | |24| 460. LFU Cache | Python| Vid 1, backtobackswe.com, algoexpert.io | Hard | TODO: Check again. Very important. Learned a lot of things | |25| 460. LFU Cache | Python| Art 1 | Easy | | |26| 708. Insert into a Sorted Circular Linked List | Python, Swift | Art 1 | Medium | | |27| 203. Remove Linked List Elements | Python, Swift | --- | Easy | | |28| 432. All O`one Data Structure | Python, Swift | Art 1 | Hard | Super hard and super important | |29| 426. Convert Binary Search Tree to Sorted Doubly Linked List | Python, Swift | Art 1 | Medium | Super important | |30| 1265. Print Immutable Linked List in Reverse | Python, Swift | Art 1 | Medium | Really?? Are we supposed to solve a puzzle in an interview? |
4. Stack, Queue
Leetcode problems with solutions and tutorials/videos
| # | Title | Solution | Tutorial | Level | Remarks | | --- | --- | --- | --- | --- | --- | |01| 20. Valid Parentheses| Python| |02| 155. Min Stack| Python| |03| 84. Largest Rectangle in Histogram| Python| Video 01, Video 02, Article 01, Article 02 | Hard | ๐ Need to revise | |04| 394. Decode String| Python| --- | Medium | ๐ Classic stack problem | |05| 239. Sliding Window Maximum | Python| Video 1, Official, Art 1, Art 2, Art 3, Art 4 | Hard | ๐ Can be solved using Heap, Deque and DP | |06| 739. Daily Temperatures| Python| Video 1, Video 2, Art 1 | Medium | ๐ TODO: Check again. A tricky one | |07| 150. Evaluate Reverse Polish Notation | Python, Swift| Vid 1 | Medium | --- | |08| 341. Flatten Nested List Iterator | Python, Swift| Vid 1, Art 1, Art 2, Art 3, Art 4, Art 5 | Medium | TODO: Check again. Very Important. Learned new things | |09| 173. Binary Search Tree Iterator | Python, Swift, Swift| Vid 1, Art 1 | Medium | TODO: Check again. Very Important. Learned new things | |10| 284. Peeking Iterator | Python, Swift| Art 1 | Medium | --- | |11| 281. Zigzag Iterator | Python, Swift| Art 1 | Medium | --- | |12| 946. Validate Stack Sequences | Python, Swift| Art 1, Art 2 | Medium | --- | |13| 862. Shortest Subarray with Sum at Least K | Python, Swift| Art 1, Art 2, Art 3, Art 4 | Hard | Learned Monotonic Queue. Very interesting problem | |14| 346. Moving Average from Data Stream | Python, Swift| | Easy | | |15| 71. Simplify Path | Python, Swift| | Medium | | |16| 1249. Minimum Remove to Make Valid Parentheses | Python, Swift| Official | Medium | Must check | |17| 636. Exclusive Time of Functions | Python, Swift| Art 1 | Medium | Must check | |18| 921. Minimum Add to Make Parentheses Valid | Python, Swift| --- | Medium | --- | |19| 1209. Remove All Adjacent Duplicates in String II | Python, Swift| --- | Medium | --- | |20| 227. Basic Calculator II | Python, Swift| Art 1 | Medium | --- | |21| 735. Asteroid Collision | Swift| Vid 1, Art 1, Art 1 | Medium | --- |
5. Heaps
Leetcode problems with solutions and tutorials/videos
| # | Title | Solution | Tutorial | Level | Remarks | | --- | --- | --- | --- | --- | --- | |01| 358. Rearrange String k Distance Apart | Python| Educative.io, Educative.io 2 | Hard | ๐ Similar to 621. Task Scheduler. Very Important. TODO: Check Heap approach | |02| 347. Top K Frequent Elements| Python| educative.io, Helper 1, Helper 2, heapq, Counter | Medium | --- | |03| 767. Reorganize String| Python| --- | Medium | Also check Greedy approach | |04| 621. Task Scheduler | Python, Swift| Ex 1, Ex 2, Vid 1, Vid 2, Vid 3, Vid 4 | Medium | ๐ Extremely tricky. Classic problem | |05| 295. Find Median from Data Stream| Python| algoexpert.io, educative.io, educative.io 2, codinginterviewclass.com, Official, FollowUp 1 | Hard | ๐ Very Tricky and important. TODO: Check again | |06| 480. Sliding Window Median | Python| educative.io, Vid 1 | Hard | ๐ Sliding window with 2 heap. very important | |07| 4. Median of Two Sorted Arrays | Python| Article 01, Art 2 Video 1| Hard | ๐ Classic problem | |08| 215. Kth Largest Element in an Array | Python| Art 01, Algoexpert.io, educative.io| Medium | ๐ Also check Quickselect method. | |09| 373. Find K Pairs with Smallest Sums | Python| educative.io | Hard | ๐ TODO: Check again. | |10| 973. K Closest Points to Origin | Python| Algoexpert.io, educative.io, Vid 1 | Medium | ๐ Also check Quickselect and heap method | |11| 451. Sort Characters By Frequency | Python| Algoexpert.io, educative.io | Medium | ๐ Also check Quickselect and heap method | |12| 692. Top K Frequent Words | Python| Algoexpert.io, educative.io | Medium | ๐ Also check Quickselect and heap method | |13| 703. Kth Largest Element in a Stream | Python| educative.io | Medium | ๐ --- | |14| 895. Maximum Frequency Stack | Python| educative.io, Art 1 | Hard | ๐ TODO: Check again | |15| 378. Kth Smallest Element in a Sorted Matrix | Python| educative.io | Hard | ๐ TODO: Check again the Binary Search approach. Very important | |16| 632. Smallest Range Covering Elements from K Lists | Python| educative.io | Hard | ๐ TODO: Check again. Very important | |17| 846. Hand of Straights | Python| Art 1 | Medium | ๐ TODO: Check again. Very important | |18| 1167. Minimum Cost to Connect Sticks | Python| Art 1 | Medium | | |19| 1102. Path With Maximum Minimum Value | Python| Art 1, Art 2, Art 3
README truncated. View on GitHub

