diff --git a/dsa-problems/leetcode-problems/0200-0299.md b/dsa-problems/leetcode-problems/0200-0299.md index c58965fe4..2cee3a213 100644 --- a/dsa-problems/leetcode-problems/0200-0299.md +++ b/dsa-problems/leetcode-problems/0200-0299.md @@ -10,606 +10,606 @@ keywords: --- export const problems = [ - { - "problemName": "200. Number of Islands", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/number-of-islands", - "solutionLink": "/dsa-solutions/lc-solutions/0200-0299/number-of-islands" - }, - { - "problemName": "201. Bitwise AND of Numbers Range", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/bitwise-and-of-numbers-range", - "solutionLink": "#" - }, - { - "problemName": "202. Happy Number", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/happy-number", - "solutionLink": "#" - }, - { - "problemName": "203. Remove Linked List Elements", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/remove-linked-list-elements", - "solutionLink": "#" - }, - { - "problemName": "204. Count Primes", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/count-primes", - "solutionLink": "/dsa-solutions/lc-solutions/0200-0299/count-primes" - }, - { - "problemName": "205. Isomorphic Strings", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/isomorphic-strings", - "solutionLink": "#" - }, - { - "problemName": "206. Reverse Linked List", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/reverse-linked-list", - "solutionLink": "#" - }, - { - "problemName": "207. Course Schedule", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/course-schedule", - "solutionLink": "/dsa-solutions/lc-solutions/0200-0299/course-schedule" - }, - { - "problemName": "208. Implement Trie (Prefix Tree)", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/implement-trie-prefix-tree", - "solutionLink": "#" - }, - { - "problemName": "209. Minimum Size Subarray Sum", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-size-subarray-sum", - "solutionLink": "#" - }, - { - "problemName": "210. Course Schedule II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/course-schedule-ii", - "solutionLink": "#" - }, - { - "problemName": "211. Design Add and Search Words Data Structure", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/design-add-and-search-words-data-structure", - "solutionLink": "#" - }, - { - "problemName": "212. Word Search II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/word-search-ii", - "solutionLink": "#" - }, - { - "problemName": "213. House Robber II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/house-robber-ii", - "solutionLink": "#" - }, - { - "problemName": "214. Shortest Palindrome", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/shortest-palindrome", - "solutionLink": "#" - }, - { - "problemName": "215. Kth Largest Element in an Array", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/kth-largest-element-in-an-array", - "solutionLink": "#" - }, - { - "problemName": "216. Combination Sum III", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/combination-sum-iii", - "solutionLink": "#" - }, - { - "problemName": "217. Contains Duplicate", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/contains-duplicate", - "solutionLink": "#" - }, - { - "problemName": "218. The Skyline Problem", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/the-skyline-problem", - "solutionLink": "#" - }, - { - "problemName": "219. Contains Duplicate II", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/contains-duplicate-ii", - "solutionLink": "#" - }, - { - "problemName": "220. Contains Duplicate III", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/contains-duplicate-iii", - "solutionLink": "#" - }, - { - "problemName": "221. Maximal Square", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximal-square", - "solutionLink": "#" - }, - { - "problemName": "222. Count Complete Tree Nodes", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/count-complete-tree-nodes", - "solutionLink": "#" - }, - { - "problemName": "223. Rectangle Area", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/rectangle-area", - "solutionLink": "#" - }, - { - "problemName": "224. Basic Calculator", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/basic-calculator", - "solutionLink": "#" - }, - { - "problemName": "225. Implement Stack using Queues", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/implement-stack-using-queues", - "solutionLink": "#" - }, - { - "problemName": "226. Invert Binary Tree", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/invert-binary-tree", - "solutionLink": "#" - }, - { - "problemName": "227. Basic Calculator II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/basic-calculator-ii", - "solutionLink": "#" - }, - { - "problemName": "228. Summary Ranges", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/summary-ranges", - "solutionLink": "#" - }, - { - "problemName": "229. Majority Element II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/majority-element-ii", - "solutionLink": "#" - }, - { - "problemName": "230. Kth Smallest Element in a BST", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/kth-smallest-element-in-a-bst", - "solutionLink": "#" - }, - { - "problemName": "231. Power of Two", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/power-of-two", - "solutionLink": "#" - }, - { - "problemName": "232. Implement Queue using Stacks", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/implement-queue-using-stacks", - "solutionLink": "#" - }, - { - "problemName": "233. Number of Digit One", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/number-of-digit-one", - "solutionLink": "#" - }, - { - "problemName": "234. Palindrome Linked List", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/palindrome-linked-list", - "solutionLink": "#" - }, - { - "problemName": "235. Lowest Common Ancestor of a Binary Search Tree", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree", - "solutionLink": "#" - }, - { - "problemName": "236. Lowest Common Ancestor of a Binary Tree", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree", - "solutionLink": "#" - }, - { - "problemName": "237. Delete Node in a Linked List", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/delete-node-in-a-linked-list", - "solutionLink": "#" - }, - { - "problemName": "238. Product of Array Except Self", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/product-of-array-except-self", - "solutionLink": "#" - }, - { - "problemName": "239. Sliding Window Maximum", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/sliding-window-maximum", - "solutionLink": "#" - }, - { - "problemName": "240. Search a 2D Matrix II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/search-a-2d-matrix-ii", - "solutionLink": "#" - }, - { - "problemName": "241. Different Ways to Add Parentheses", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/different-ways-to-add-parentheses", - "solutionLink": "#" - }, - { - "problemName": "242. Valid Anagram", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/valid-anagram", - "solutionLink": "#" - }, - { - "problemName": "243. Shortest Word Distance", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/shortest-word-distance", - "solutionLink": "#" - }, - { - "problemName": "244. Shortest Word Distance II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/shortest-word-distance-ii", - "solutionLink": "#" - }, - { - "problemName": "245. Shortest Word Distance III", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/shortest-word-distance-iii", - "solutionLink": "#" - }, - { - "problemName": "246. Strobogrammatic Number", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/strobogrammatic-number", - "solutionLink": "#" - }, - { - "problemName": "247. Strobogrammatic Number II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/strobogrammatic-number-ii", - "solutionLink": "#" - }, - { - "problemName": "248. Strobogrammatic Number III", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/strobogrammatic-number-iii", - "solutionLink": "#" - }, - { - "problemName": "249. Group Shifted Strings", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/group-shifted-strings", - "solutionLink": "#" - }, - { - "problemName": "250. Count Univalue Subtrees", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/count-univalue-subtrees", - "solutionLink": "#" - }, - { - "problemName": "251. Flatten 2D Vector", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/flatten-2d-vector", - "solutionLink": "#" - }, - { - "problemName": "252. Meeting Rooms", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/meeting-rooms", - "solutionLink": "#" - }, - { - "problemName": "253. Meeting Rooms II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/meeting-rooms-ii", - "solutionLink": "#" - }, - { - "problemName": "254. Factor Combinations", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/factor-combinations", - "solutionLink": "#" - }, - { - "problemName": "255. Verify Preorder Sequence in Binary Search Tree", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree", - "solutionLink": "#" - }, - { - "problemName": "256. Paint House", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/paint-house", - "solutionLink": "#" - }, - { - "problemName": "257. Binary Tree Paths", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/binary-tree-paths", - "solutionLink": "#" - }, - { - "problemName": "258. Add Digits", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/add-digits", - "solutionLink": "#" - }, - { - "problemName": "259. 3Sum Smaller", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/3sum-smaller", - "solutionLink": "#" - }, - { - "problemName": "260. Single Number III", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/single-number-iii", - "solutionLink": "#" - }, - { - "problemName": "261. Graph Valid Tree", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/graph-valid-tree", - "solutionLink": "#" - }, - { - "problemName": "262. Trips and Users", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/trips-and-users", - "solutionLink": "#" - }, - { - "problemName": "263. Ugly Number", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/ugly-number", - "solutionLink": "#" - }, - { - "problemName": "264. Ugly Number II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/ugly-number-ii", - "solutionLink": "#" - }, - { - "problemName": "265. Paint House II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/paint-house-ii", - "solutionLink": "#" - }, - { - "problemName": "266. Palindrome Permutation", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/palindrome-permutation", - "solutionLink": "#" - }, - { - "problemName": "267. Palindrome Permutation II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/palindrome-permutation-ii", - "solutionLink": "#" - }, - { - "problemName": "268. Missing Number", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/missing-number", - "solutionLink": "#" - }, - { - "problemName": "269. Alien Dictionary", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/alien-dictionary", - "solutionLink": "#" - }, - { - "problemName": "270. Closest Binary Search Tree Value", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/closest-binary-search-tree-value", - "solutionLink": "#" - }, - { - "problemName": "271. Encode and Decode Strings", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/encode-and-decode-strings", - "solutionLink": "#" - }, - { - "problemName": "272. Closest Binary Search Tree Value II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/closest-binary-search-tree-value-ii", - "solutionLink": "#" - }, - { - "problemName": "273. Integer to English Words", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/integer-to-english-words", - "solutionLink": "#" - }, - { - "problemName": "274. H-Index", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/h-index", - "solutionLink": "#" - }, - { - "problemName": "275. H-Index II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/h-index-ii", - "solutionLink": "#" - }, - { - "problemName": "276. Paint Fence", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/paint-fence", - "solutionLink": "#" - }, - { - "problemName": "277. Find the Celebrity", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-the-celebrity", - "solutionLink": "#" - }, - { - "problemName": "278. First Bad Version", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/first-bad-version", - "solutionLink": "#" - }, - { - "problemName": "279. Perfect Squares", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/perfect-squares", - "solutionLink": "#" - }, - { - "problemName": "280. Wiggle Sort", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/wiggle-sort", - "solutionLink": "#" - }, - { - "problemName": "281. Zigzag Iterator", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/zigzag-iterator", - "solutionLink": "#" - }, - { - "problemName": "282. Expression Add Operators", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/expression-add-operators", - "solutionLink": "#" - }, - { - "problemName": "283. Move Zeroes", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/move-zeroes", - "solutionLink": "#" - }, - { - "problemName": "284. Peeking Iterator", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/peeking-iterator", - "solutionLink": "#" - }, - { - "problemName": "285. Inorder Successor in BST", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/inorder-successor-in-bst", - "solutionLink": "#" - }, - { - "problemName": "286. Walls and Gates", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/walls-and-gates", - "solutionLink": "#" - }, - { - "problemName": "287. Find the Duplicate Number", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-the-duplicate-number", - "solutionLink": "#" - }, - { - "problemName": "288. Unique Word Abbreviation", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/unique-word-abbreviation", - "solutionLink": "#" - }, - { - "problemName": "289. Game of Life", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/game-of-life", - "solutionLink": "#" - }, - { - "problemName": "290. Word Pattern", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/word-pattern", - "solutionLink": "#" - }, - { - "problemName": "291. Word Pattern II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/word-pattern-ii", - "solutionLink": "#" - }, - { - "problemName": "292. Nim Game", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/nim-game", - "solutionLink": "#" - }, - { - "problemName": "293. Flip Game", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/flip-game", - "solutionLink": "#" - }, - { - "problemName": "294. Flip Game II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/flip-game-ii", - "solutionLink": "#" - }, - { - "problemName": "295. Find Median from Data Stream", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/find-median-from-data-stream", - "solutionLink": "#" - }, - { - "problemName": "296. Best Meeting Point", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/best-meeting-point", - "solutionLink": "#" - }, - { - "problemName": "297. Serialize and Deserialize Binary Tree", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/serialize-and-deserialize-binary-tree", - "solutionLink": "#" - }, - { - "problemName": "298. Binary Tree Longest Consecutive Sequence", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/binary-tree-longest-consecutive-sequence", - "solutionLink": "#" - }, - { - "problemName": "299. Bulls and Cows", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/bulls-and-cows", - "solutionLink": "#" - } +{ +"problemName": "200. Number of Islands", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/number-of-islands", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/number-of-islands" +}, +{ +"problemName": "201. Bitwise AND of Numbers Range", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/bitwise-and-of-numbers-range", +"solutionLink": "#" +}, +{ +"problemName": "202. Happy Number", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/happy-number", +"solutionLink": "#" +}, +{ +"problemName": "203. Remove Linked List Elements", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/remove-linked-list-elements", +"solutionLink": "#/dsa-solutions/lc-solutions/0200-0299/Remove-Linked-List-Elements" +}, +{ +"problemName": "204. Count Primes", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/count-primes", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/count-primes" +}, +{ +"problemName": "205. Isomorphic Strings", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/isomorphic-strings", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/isomorphic-strings" +}, +{ +"problemName": "206. Reverse Linked List", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/reverse-linked-list", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/Reverse-Linkedlist" +}, +{ +"problemName": "207. Course Schedule", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/course-schedule", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/course-schedule" +}, +{ +"problemName": "208. Implement Trie (Prefix Tree)", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/implement-trie-prefix-tree", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/Implement-Trie" +}, +{ +"problemName": "209. Minimum Size Subarray Sum", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-size-subarray-sum", +"solutionLink": "#" +}, +{ +"problemName": "210. Course Schedule II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/course-schedule-ii", +"solutionLink": "#" +}, +{ +"problemName": "211. Design Add and Search Words Data Structure", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/design-add-and-search-words-data-structure", +"solutionLink": "#" +}, +{ +"problemName": "212. Word Search II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/word-search-ii", +"solutionLink": "#" +}, +{ +"problemName": "213. House Robber II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/house-robber-ii", +"solutionLink": "#" +}, +{ +"problemName": "214. Shortest Palindrome", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/shortest-palindrome", +"solutionLink": "#" +}, +{ +"problemName": "215. Kth Largest Element in an Array", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/kth-largest-element-in-an-array", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/kth-largest-element-in-an-array" +}, +{ +"problemName": "216. Combination Sum III", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/combination-sum-iii", +"solutionLink": "#" +}, +{ +"problemName": "217. Contains Duplicate", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/contains-duplicate", +"solutionLink": "#" +}, +{ +"problemName": "218. The Skyline Problem", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/the-skyline-problem", +"solutionLink": "#" +}, +{ +"problemName": "219. Contains Duplicate II", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/contains-duplicate-ii", +"solutionLink": "#" +}, +{ +"problemName": "220. Contains Duplicate III", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/contains-duplicate-iii", +"solutionLink": "#" +}, +{ +"problemName": "221. Maximal Square", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximal-square", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/Maximal Square" +}, +{ +"problemName": "222. Count Complete Tree Nodes", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/count-complete-tree-nodes", +"solutionLink": "#" +}, +{ +"problemName": "223. Rectangle Area", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/rectangle-area", +"solutionLink": "#" +}, +{ +"problemName": "224. Basic Calculator", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/basic-calculator", +"solutionLink": "#" +}, +{ +"problemName": "225. Implement Stack using Queues", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/implement-stack-using-queues", +"solutionLink": "#" +}, +{ +"problemName": "226. Invert Binary Tree", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/invert-binary-tree", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/invert-binary-search-tree" +}, +{ +"problemName": "227. Basic Calculator II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/basic-calculator-ii", +"solutionLink": "#" +}, +{ +"problemName": "228. Summary Ranges", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/summary-ranges", +"solutionLink": "#" +}, +{ +"problemName": "229. Majority Element II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/majority-element-ii", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/Majority-Element" +}, +{ +"problemName": "230. Kth Smallest Element in a BST", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/kth-smallest-element-in-a-bst", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/Kth-smallest-element-in-BST" +}, +{ +"problemName": "231. Power of Two", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/power-of-two", +"solutionLink": "#" +}, +{ +"problemName": "232. Implement Queue using Stacks", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/implement-queue-using-stacks", +"solutionLink": "#" +}, +{ +"problemName": "233. Number of Digit One", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/number-of-digit-one", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/Number of Digit One" +}, +{ +"problemName": "234. Palindrome Linked List", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/palindrome-linked-list", +"solutionLink": "#" +}, +{ +"problemName": "235. Lowest Common Ancestor of a Binary Search Tree", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/lowest-common-Ancestor-of-binary-search-tree" +}, +{ +"problemName": "236. Lowest Common Ancestor of a Binary Tree", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree", +"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/Lowest-Common-Ancestor-of-a-Binary-Tree" +}, +{ +"problemName": "237. Delete Node in a Linked List", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/delete-node-in-a-linked-list", +"solutionLink": "#" +}, +{ +"problemName": "238. Product of Array Except Self", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/product-of-array-except-self", +"solutionLink": "#" +}, +{ +"problemName": "239. Sliding Window Maximum", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/sliding-window-maximum", +"solutionLink": "#" +}, +{ +"problemName": "240. Search a 2D Matrix II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/search-a-2d-matrix-ii", +"solutionLink": "#" +}, +{ +"problemName": "241. Different Ways to Add Parentheses", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/different-ways-to-add-parentheses", +"solutionLink": "#" +}, +{ +"problemName": "242. Valid Anagram", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/valid-anagram", +"solutionLink": "#" +}, +{ +"problemName": "243. Shortest Word Distance", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/shortest-word-distance", +"solutionLink": "#" +}, +{ +"problemName": "244. Shortest Word Distance II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/shortest-word-distance-ii", +"solutionLink": "#" +}, +{ +"problemName": "245. Shortest Word Distance III", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/shortest-word-distance-iii", +"solutionLink": "#" +}, +{ +"problemName": "246. Strobogrammatic Number", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/strobogrammatic-number", +"solutionLink": "#" +}, +{ +"problemName": "247. Strobogrammatic Number II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/strobogrammatic-number-ii", +"solutionLink": "#" +}, +{ +"problemName": "248. Strobogrammatic Number III", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/strobogrammatic-number-iii", +"solutionLink": "#" +}, +{ +"problemName": "249. Group Shifted Strings", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/group-shifted-strings", +"solutionLink": "#" +}, +{ +"problemName": "250. Count Univalue Subtrees", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/count-univalue-subtrees", +"solutionLink": "#" +}, +{ +"problemName": "251. Flatten 2D Vector", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/flatten-2d-vector", +"solutionLink": "#" +}, +{ +"problemName": "252. Meeting Rooms", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/meeting-rooms", +"solutionLink": "#" +}, +{ +"problemName": "253. Meeting Rooms II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/meeting-rooms-ii", +"solutionLink": "#" +}, +{ +"problemName": "254. Factor Combinations", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/factor-combinations", +"solutionLink": "#" +}, +{ +"problemName": "255. Verify Preorder Sequence in Binary Search Tree", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree", +"solutionLink": "#" +}, +{ +"problemName": "256. Paint House", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/paint-house", +"solutionLink": "#" +}, +{ +"problemName": "257. Binary Tree Paths", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/binary-tree-paths", +"solutionLink": "#" +}, +{ +"problemName": "258. Add Digits", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/add-digits", +"solutionLink": "#" +}, +{ +"problemName": "259. 3Sum Smaller", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/3sum-smaller", +"solutionLink": "#" +}, +{ +"problemName": "260. Single Number III", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/single-number-iii", +"solutionLink": "#" +}, +{ +"problemName": "261. Graph Valid Tree", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/graph-valid-tree", +"solutionLink": "#" +}, +{ +"problemName": "262. Trips and Users", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/trips-and-users", +"solutionLink": "#" +}, +{ +"problemName": "263. Ugly Number", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/ugly-number", +"solutionLink": "#" +}, +{ +"problemName": "264. Ugly Number II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/ugly-number-ii", +"solutionLink": "#" +}, +{ +"problemName": "265. Paint House II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/paint-house-ii", +"solutionLink": "#" +}, +{ +"problemName": "266. Palindrome Permutation", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/palindrome-permutation", +"solutionLink": "#" +}, +{ +"problemName": "267. Palindrome Permutation II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/palindrome-permutation-ii", +"solutionLink": "#" +}, +{ +"problemName": "268. Missing Number", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/missing-number", +"solutionLink": "#" +}, +{ +"problemName": "269. Alien Dictionary", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/alien-dictionary", +"solutionLink": "#" +}, +{ +"problemName": "270. Closest Binary Search Tree Value", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/closest-binary-search-tree-value", +"solutionLink": "#" +}, +{ +"problemName": "271. Encode and Decode Strings", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/encode-and-decode-strings", +"solutionLink": "#" +}, +{ +"problemName": "272. Closest Binary Search Tree Value II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/closest-binary-search-tree-value-ii", +"solutionLink": "#" +}, +{ +"problemName": "273. Integer to English Words", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/integer-to-english-words", +"solutionLink": "#" +}, +{ +"problemName": "274. H-Index", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/h-index", +"solutionLink": "#" +}, +{ +"problemName": "275. H-Index II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/h-index-ii", +"solutionLink": "#" +}, +{ +"problemName": "276. Paint Fence", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/paint-fence", +"solutionLink": "#" +}, +{ +"problemName": "277. Find the Celebrity", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-the-celebrity", +"solutionLink": "#" +}, +{ +"problemName": "278. First Bad Version", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/first-bad-version", +"solutionLink": "#" +}, +{ +"problemName": "279. Perfect Squares", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/perfect-squares", +"solutionLink": "#" +}, +{ +"problemName": "280. Wiggle Sort", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/wiggle-sort", +"solutionLink": "#" +}, +{ +"problemName": "281. Zigzag Iterator", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/zigzag-iterator", +"solutionLink": "#" +}, +{ +"problemName": "282. Expression Add Operators", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/expression-add-operators", +"solutionLink": "#" +}, +{ +"problemName": "283. Move Zeroes", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/move-zeroes", +"solutionLink": "#" +}, +{ +"problemName": "284. Peeking Iterator", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/peeking-iterator", +"solutionLink": "#" +}, +{ +"problemName": "285. Inorder Successor in BST", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/inorder-successor-in-bst", +"solutionLink": "#" +}, +{ +"problemName": "286. Walls and Gates", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/walls-and-gates", +"solutionLink": "#" +}, +{ +"problemName": "287. Find the Duplicate Number", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-the-duplicate-number", +"solutionLink": "#" +}, +{ +"problemName": "288. Unique Word Abbreviation", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/unique-word-abbreviation", +"solutionLink": "#" +}, +{ +"problemName": "289. Game of Life", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/game-of-life", +"solutionLink": "#" +}, +{ +"problemName": "290. Word Pattern", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/word-pattern", +"solutionLink": "#" +}, +{ +"problemName": "291. Word Pattern II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/word-pattern-ii", +"solutionLink": "#" +}, +{ +"problemName": "292. Nim Game", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/nim-game", +"solutionLink": "#" +}, +{ +"problemName": "293. Flip Game", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/flip-game", +"solutionLink": "#" +}, +{ +"problemName": "294. Flip Game II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/flip-game-ii", +"solutionLink": "#" +}, +{ +"problemName": "295. Find Median from Data Stream", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/find-median-from-data-stream", +"solutionLink": "#" +}, +{ +"problemName": "296. Best Meeting Point", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/best-meeting-point", +"solutionLink": "#" +}, +{ +"problemName": "297. Serialize and Deserialize Binary Tree", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/serialize-and-deserialize-binary-tree", +"solutionLink": "#" +}, +{ +"problemName": "298. Binary Tree Longest Consecutive Sequence", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/binary-tree-longest-consecutive-sequence", +"solutionLink": "#" +}, +{ +"problemName": "299. Bulls and Cows", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/bulls-and-cows", +"solutionLink": "#" +} ] -Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. \ No newline at end of file +Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. diff --git a/dsa-solutions/lc-solutions/0000-0099/0017-Letter-Combinations-of-a-Phone-Number.md b/dsa-solutions/lc-solutions/0000-0099/0017-Letter-Combinations-of-a-Phone-Number.md index 26cf596b7..76a29c9c4 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0017-Letter-Combinations-of-a-Phone-Number.md +++ b/dsa-solutions/lc-solutions/0000-0099/0017-Letter-Combinations-of-a-Phone-Number.md @@ -3,21 +3,23 @@ id: letter-combinations-of-a-phone-number title: Letter Combinations of a Phone Number (LeetCode) sidebar_label: 0017 Letter Combinations of a Phone Number tags: - - Back Tracking - - Mapping - - String + - Back Tracking + - Mapping + - String description: The problem requires generating all letter combinations corresponding to given digits (2-9). The solution utilizes backtracking to explore all combinations efficiently, employing a recursive approach in Java. +sidebar_position: 17 --- ## Problem Description -| Problem Statement | Solution Link | LeetCode Profile | -| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- | -| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) | +| Problem Statement | Solution Link | LeetCode Profile | +| :------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------- | +| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) | ### Problem Description ## Problem Statement: + Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. ### Examples @@ -32,7 +34,6 @@ Given a string containing digits from 2-9 inclusive, return all possible letter - **Input:** `digits = ""` - **Output:** `[]` - #### Example 3 - **Input:** `2` @@ -47,9 +48,11 @@ Given a string containing digits from 2-9 inclusive, return all possible letter ### Approach 1. **Mapping Digits to Letters:** + - Define a mapping of digits to their corresponding letters, similar to telephone buttons. 2. **Backtracking Function:** + - Define a recursive backtracking function to generate all possible combinations. - The function takes four parameters: - `index`: The current index in the digits string. @@ -59,6 +62,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter - After the recursive call, we remove the last character from the combination (backtracking). 3. **Base Case:** + - If the length of the current combination is equal to the length of the input digits string, we add the combination to the result list. 4. **Main Function:** @@ -153,6 +157,7 @@ public class Solution { ``` #### CPP: + ```cpp #include #include @@ -208,40 +213,41 @@ int main() { ``` #### JavaScript + ```js /** * @param {string} digits * @return {string[]} */ -var letterCombinations = function(digits) { - if (digits.length === 0) return []; - - const digitToLetters = { - '2': 'abc', - '3': 'def', - '4': 'ghi', - '5': 'jkl', - '6': 'mno', - '7': 'pqrs', - '8': 'tuv', - '9': 'wxyz' - }; - - const combinations = []; - - const backtrack = (index, path) => { - if (index === digits.length) { - combinations.push(path); - return; - } - const letters = digitToLetters[digits.charAt(index)]; - for (let letter of letters) { - backtrack(index + 1, path + letter); - } - }; - - backtrack(0, ''); - return combinations; +var letterCombinations = function (digits) { + if (digits.length === 0) return []; + + const digitToLetters = { + 2: "abc", + 3: "def", + 4: "ghi", + 5: "jkl", + 6: "mno", + 7: "pqrs", + 8: "tuv", + 9: "wxyz", + }; + + const combinations = []; + + const backtrack = (index, path) => { + if (index === digits.length) { + combinations.push(path); + return; + } + const letters = digitToLetters[digits.charAt(index)]; + for (let letter of letters) { + backtrack(index + 1, path + letter); + } + }; + + backtrack(0, ""); + return combinations; }; // Example usage: @@ -249,39 +255,40 @@ console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf" ``` #### TypeScript + ```ts class Solution { - private digitToLetters: { [key: string]: string } = { - '2': 'abc', - '3': 'def', - '4': 'ghi', - '5': 'jkl', - '6': 'mno', - '7': 'pqrs', - '8': 'tuv', - '9': 'wxyz' + private digitToLetters: { [key: string]: string } = { + "2": "abc", + "3": "def", + "4": "ghi", + "5": "jkl", + "6": "mno", + "7": "pqrs", + "8": "tuv", + "9": "wxyz", + }; + + letterCombinations(digits: string): string[] { + const combinations: string[] = []; + + const backtrack = (index: number, path: string): void => { + if (index === digits.length) { + combinations.push(path); + return; + } + const letters = this.digitToLetters[digits.charAt(index)]; + for (let letter of letters) { + backtrack(index + 1, path + letter); + } }; - letterCombinations(digits: string): string[] { - const combinations: string[] = []; - - const backtrack = (index: number, path: string): void => { - if (index === digits.length) { - combinations.push(path); - return; - } - const letters = this.digitToLetters[digits.charAt(index)]; - for (let letter of letters) { - backtrack(index + 1, path + letter); - } - }; - - if (digits.length !== 0) { - backtrack(0, ''); - } - - return combinations; + if (digits.length !== 0) { + backtrack(0, ""); } + + return combinations; + } } // Example usage: @@ -294,9 +301,11 @@ console.log(solution.letterCombinations("23")); // Output: ["ad","ae","af","bd", Here's a step-by-step algorithm for generating all possible letter combinations of a given string of digits using backtracking: 1. **Define a mapping of digits to letters:** + - Create a map where each digit from 2 to 9 is mapped to its corresponding letters on a telephone keypad. 2. **Define a backtracking function:** + - The function will take the following parameters: - `index`: The current index in the digits string. - `path`: The current combination of letters. @@ -305,6 +314,7 @@ Here's a step-by-step algorithm for generating all possible letter combinations - After the recursive call, remove the last character from the combination (backtracking). 3. **Base Case:** + - If the length of the current combination is equal to the length of the input digits string, add the combination to the result list. 4. **Main Function:** @@ -312,4 +322,4 @@ Here's a step-by-step algorithm for generating all possible letter combinations - Call the backtracking function with the initial index set to 0 and an empty string as the initial combination. - Return the list of combinations. -This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking. \ No newline at end of file +This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking. diff --git a/dsa-solutions/lc-solutions/0200-0299/0198-house-robber.md b/dsa-solutions/lc-solutions/0200-0299/0198-house-robber.md deleted file mode 100644 index 5d95f9320..000000000 --- a/dsa-solutions/lc-solutions/0200-0299/0198-house-robber.md +++ /dev/null @@ -1,218 +0,0 @@ ---- -id: house-robber -title: House Robber -sidebar_label: 0198 House Robber -tags: -- Dynamic Programming -- Array -- C++ -- Java -- Python -description: "This document provides a solution to the House Robber problem, where houses are arranged in a line." ---- - -## Problem -You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. - -Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. - -### Example 1: -Input: nums = [1,2,3,1] -Output: 4 -Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. - -### Example 2: -Input: nums = [2,7,9,3,1] -Output: 12 -Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. - -### Constraints: -- $1 <= nums.length <= 100$ -- $0 <= nums[i] <= 400$ - -## Solution -There is some frustration when people publish their perfect fine-grained algorithms without sharing any information about how they were derived. This is an attempt to change the situation. There is not much more explanation but it's rather an example of higher-level improvements. Converting a solution to the next step shouldn't be as hard as attempting to come up with the perfect algorithm at first attempt. - -This particular problem and most others can be approached using the following sequence: - -1. Find recursive relation -2. Recursive (top-down) -3. Recursive + memo (top-down) -4. Iterative + memo (bottom-up) -5. Iterative + N variables (bottom-up) - -### Step 1. Figure out recursive relation -A robber has 2 options: -a) rob current house i; -b) don't rob current house. -If option "a" is selected it means she can't rob the previous i-1 house but can safely proceed to the one before previous i-2 and gets all cumulative loot that follows. -If option "b" is selected the robber gets all the possible loot from the robbery of i-1 and all the following buildings. -So it boils down to calculating what is more profitable: -- robbery of the current house + loot from houses before the previous -- loot from the previous house robbery and any loot captured before that - -rob(i) = Math.max(rob(i - 2) + currentHouseValue, rob(i - 1)) - -### Step 2. Recursive (top-down) -Converting the recurrent relation from Step 1 shouldn't be very hard. - -```java -public int rob(int[] nums) { - return rob(nums, nums.length - 1); -} - -private int rob(int[] nums, int i) { - if (i < 0) { - return 0; - } - return Math.max(rob(nums, i - 2) + nums[i], rob(nums, i - 1)); -} -``` - -This algorithm will process the same i multiple times and it needs improvement. - -### Step 3. Recursive + memo (top-down) -```java -int[] memo; -public int rob(int[] nums) { - memo = new int[nums.length + 1]; - Arrays.fill(memo, -1); - return rob(nums, nums.length - 1); -} - -private int rob(int[] nums, int i) { - if (i < 0) { - return 0; - } - if (memo[i] >= 0) { - return memo[i]; - } - int result = Math.max(rob(nums, i - 2) + nums[i], rob(nums, i - 1)); - memo[i] = result; - return result; -} -``` -Much better, this should run in O(n) time. Space complexity is O(n) as well, because of the recursion stack, let's try to get rid of it. - -### Step 4. Iterative + memo (bottom-up) -```java -public int rob(int[] nums) { - if (nums.length == 0) return 0; - int[] memo = new int[nums.length + 1]; - memo[0] = 0; - memo[1] = nums[0]; - for (int i = 1; i < nums.length; i++) { - int val = nums[i]; - memo[i + 1] = Math.max(memo[i], memo[i - 1] + val); - } - return memo[nums.length]; -} -``` - -### Step 5. Iterative + 2 variables (bottom-up) -We can notice that in the previous step we use only memo[i] and memo[i-1], so going just 2 steps back. We can hold them in 2 variables instead. This optimization is met in Fibonacci sequence creation and some other problems. - -```java -public int rob(int[] nums) { - if (nums.length == 0) return 0; - int prev1 = 0; - int prev2 = 0; - for (int num : nums) { - int tmp = prev1; - prev1 = Math.max(prev2 + num, prev1); - prev2 = tmp; - } - return prev1; -} -``` - -## Code in Different Languages - - - - - -```cpp -#include -using namespace std; - -int rob(vector& nums) { - int n = nums.size(); - if (n == 0) return 0; - int prev1 = 0, prev2 = 0; - for (int num : nums) { - int tmp = prev1; - prev1 = max(prev2 + num, prev1); - prev2 = tmp; - } - return prev1; -} - -int main() { - vector nums = {2, 7, 9, 3, 1}; - cout << rob(nums); // Output: 12 - return 0; -} -``` - - - - -```java -public class HouseRobber { - public int rob(int[] nums) { - if (nums.length == 0) return 0; - int prev1 = 0; - int prev2 = 0; - for (int num : nums) { - int tmp = prev1; - prev1 = Math.max(prev2 + num, prev1); - prev2 = tmp; - } - return prev1; - } - - public static void main(String[] args) { - HouseRobber hr = new HouseRobber(); - int[] nums = {2, 7, 9, 3, 1}; - System.out.println(hr.rob(nums)); // Output: 12 - } -} -``` - - - - - -```python -def rob(nums): - if not nums: - return 0 - prev1, prev2 = 0, 0 - for num in nums: - tmp = prev1 - prev1 = max(prev2 + num, prev1) - prev2 = tmp - return prev1 - -nums = [2, 7, 9, 3, 1] -print(rob(nums)) # Output: 12 -``` - - - -## Complexity Analysis - -### Time Complexity: O(N) - -> **Reason**: We are running a simple iterative loop, two times. Therefore total time complexity will be O(N) + O(N) ≈ O(N) - -### Space Complexity: O(1) - -> **Reason**: We are not using extra space. - -## References - -- **LeetCode Problem**: [House Robber](https://leetcode.com/problems/house-robber/) - -- **Author's GeeksforGeeks Profile:** [Vipul Lakum](https://leetcode.com/u/vipul_lakum_02/) \ No newline at end of file diff --git a/dsa-solutions/lc-solutions/0200-0299/0206-Reverse-Linkedlist.md b/dsa-solutions/lc-solutions/0200-0299/0206-Reverse-Linkedlist.md new file mode 100644 index 000000000..b13d40e13 --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0206-Reverse-Linkedlist.md @@ -0,0 +1,304 @@ +--- +id: Reverse-LinkedList +title: Reverse LinkedList +sidebar_label: 206-Reverse-LinkedList +tags: + - Linkedlist + - Recursion + - Java + - C++ + - Python +description: "This document provides solutions for determining the Reverse Linkedlist." +--- + +# 0206-Reverse LinkedList + +### Problem Statement + +Given the head of a singly linked list, reverse the list, and return the reversed list. + +Example 1: + + ![alt text](image.png) + + Input: head = [1,2,3,4,5] + Output: [5,4,3,2,1] + +Example 2: + + ![alt text](image-1.png) + + Input: head = [1,2] + Output: [2,1] + +Example 3: +Input: head = [] +Output: [] + +### Solutions + +#### Intuition : + +The problem is asking to reverse a singly-linked list. One common approach to reverse a linked list is to use recursion. The idea is to reverse the rest of the list and then move the current node to the end. The base case of the recursion is when the current node is null, in which case the head of the reversed list is set to the previous node. + +#### Approach : + +1. The solve function is a recursive function that reverses the linked list. It takes three parameters: head (a reference to the head of the reversed list), prev (the previous node in the reversed list), and curr (the current node in the original list). + +2. If curr is null, it means we have reached the end of the original list. In this case, we set the head to prev, effectively updating the head of the reversed list, and return. + +3. Otherwise, we store the next node in a variable (nextNode) to prevent losing the reference to the rest of the list. + +4. We make a recursive call to solve with updated parameters: head remains the same, curr becomes the next node (nextNode), and prev becomes the current node (curr). + +5. After the recursive call, we update the next pointer of the current node (curr->next) to point to the previous node (prev), effectively reversing the link. + +6. The reverseList function is a wrapper function that checks if the list is empty or has only one element. If so, it returns the head unchanged. Otherwise, it calls the solve function to reverse the list and returns the updated head. + + + + + ```cpp + #include + + // Definition for singly-linked list. + struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} + }; + + class Solution { + public: + void solve(ListNode* &head, ListNode* prev, ListNode* curr) { + if (curr == nullptr) { + head = prev; + return; + } + ListNode* nextNode = curr->next; + solve(head, curr, nextNode); + curr->next = prev; + } + + ListNode* reverseList(ListNode* head) { + if (head == nullptr || head->next == nullptr) { + return head; + } + solve(head, nullptr, head); + return head; + } + }; + + // Helper function to print the linked list + void printList(ListNode* node) { + while (node != nullptr) { + std::cout << node->val << " "; + node = node->next; + } + } + + // Driver code + int main() { + Solution solution; + ListNode* head = new ListNode(1); + head->next = new ListNode(2); + head->next->next = new ListNode(3); + head->next->next->next = new ListNode(4); + head->next->next->next->next = new ListNode(5); + + std::cout << "Original list: "; + printList(head); + + head = solution.reverseList(head); + + std::cout << "\nReversed list: "; + printList(head); + + return 0; + } + ``` + + + + ```java + // Definition for singly-linked list. + class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + } + + public class Solution { + public void solve(ListNode head, ListNode prev, ListNode curr) { + if (curr == null) { + head = prev; + return; + } + ListNode nextNode = curr.next; + solve(head, curr, nextNode); + curr.next = prev; + } + + public ListNode reverseList(ListNode head) { + if (head == null || head.next == null) { + return head; + } + solve(head, null, head); + return head; + } + + public static void main(String[] args) { + Solution solution = new Solution(); + ListNode head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = new ListNode(3); + head.next.next.next = new ListNode(4); + head.next.next.next.next = new ListNode(5); + + System.out.print("Original list: "); + printList(head); + + head = solution.reverseList(head); + + System.out.print("\nReversed list: "); + printList(head); + } + + // Helper function to print the linked list + public static void printList(ListNode node) { + while (node != null) { + System.out.print(node.val + " "); + node = node.next; + } + } + } + ``` + + + + ```python + # Definition for singly-linked list. + class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + class Solution: + def solve(self, head: 'ListNode', prev: 'ListNode', curr: 'ListNode') -> None: + if curr is None: + head[0] = prev + return + nextNode = curr.next + self.solve(head, curr, nextNode) + curr.next = prev + + def reverseList(self, head: 'ListNode') -> 'ListNode': + if head is None or head.next is None: + return head + head_ref = [head] + self.solve(head_ref, None, head) + return head_ref[0] + + # Helper function to print the linked list + def printList(node): + while node: + print(node.val, end=" ") + node = node.next + + # Driver code + if __name__ == "__main__": + solution = Solution() + head = ListNode(1) + head.next = ListNode(2) + head.next.next = ListNode(3) + head.next.next.next = ListNode(4) + head.next.next.next.next = ListNode(5) + + print("Original list: ", end="") + printList(head) + + head = solution.reverseList(head) + + print("\nReversed list: ", end="") + printList(head) + ``` + + + + ```c + #include + #include + + // Definition for singly-linked list. + struct ListNode { + int val; + struct ListNode *next; + }; + + void solve(struct ListNode** head, struct ListNode* prev, struct ListNode* curr) { + if (curr == NULL) { + *head = prev; + return; + } + struct ListNode* nextNode = curr->next; + solve(head, curr, nextNode); + curr->next = prev; + } + + struct ListNode* reverseList(struct ListNode* head) { + if (head == NULL || head->next == NULL) { + return head; + } + solve(&head, NULL, head); + return head; + } + + // Helper function to print the linked list + void printList(struct ListNode* node) { + while (node != NULL) { + printf("%d ", node->val); + node = node->next; + } + } + + // Driver code + int main() { + struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode)); + head->val = 1; + head->next = (struct ListNode*)malloc(sizeof(struct ListNode)); + head->next->val = 2; + head->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); + head->next->next->val = 3; + head->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); + head->next->next->next->val = 4; + head->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); + head->next->next->next->next->val = 5; + head->next->next->next->next->next = NULL; + + printf("Original list: "); + printList(head); + + head = reverseList(head); + + printf("\nReversed list: "); + printList(head); + + return 0; + } + ``` + + + + +## Video lecture + + diff --git a/dsa-solutions/lc-solutions/0200-0299/0208-Implement-Trie.md b/dsa-solutions/lc-solutions/0200-0299/0208-Implement-Trie.md new file mode 100644 index 000000000..e30abfd5d --- /dev/null +++ b/dsa-solutions/lc-solutions/0200-0299/0208-Implement-Trie.md @@ -0,0 +1,306 @@ +--- +id: Implement-Trie +title: Implement Trie +sidebar_label: 0208 Implement-Trie +tags: + - Trie + - java + - python + - c++ + - Recursion +description: "This document provides a solution to the Implementation of Trie." +--- + +## Problem Statement + +A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. + +Implement the Trie class: + +1. Trie() Initializes the trie object. + +2. void insert(String word) Inserts the string word into the trie. + +3. boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise. + +4. boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise. + +### Example 1: + +Input: +["Trie", "insert", "search", "search", "startsWith", "insert", "search"] +[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]] +Output +[null, null, true, false, true, null, true] + +Explanation + +``` + Trie trie = new Trie(); + + trie.insert("apple"); + trie.search("apple"); // return True + trie.search("app"); // return False + trie.startsWith("app"); // return True + trie.insert("app"); + trie.search("app"); // return True +``` + +## Solutions + + A Trie prefix tree is a tree data structure which is used to store the strings. Each node in the Trie represents a character of the string and stores the boolean value to indicate whether it is the end of a word. The Trie tree is used to find all the possible words by traversing all the nodes of the tree. Insertion in the Trie tree is performed by iterating each character of the string and checking if the character is already present in the Trie tree. If it is present, then move to the corresponding node of that character, otherwise create a new node and connect it with the Trie tree. + + + + + ```cpp + #include + #include + + class TrieNode { + public: + TrieNode *child[26]; + bool isWord; + TrieNode() { + isWord = false; + for (auto &a : child) a = nullptr; + } + }; + + class Trie { + TrieNode* root; + public: + Trie() { + root = new TrieNode(); + } + void insert(std::string s) { + TrieNode *p = root; + for (auto &a : s) { + int i = a - 'a'; + if (!p->child[i]) p->child[i] = new TrieNode(); + p = p->child[i]; + } + p->isWord = true; + } + bool search(std::string key, bool prefix=false) { + TrieNode *p = root; + for (auto &a : key) { + int i = a - 'a'; + if (!p->child[i]) return false; + p = p->child[i]; + } + if (prefix == false) return p->isWord; + return true; + } + bool startsWith(std::string prefix) { + return search(prefix, true); + } + }; + + // Driver code + int main() { + Trie trie; + trie.insert("apple"); + std::cout << trie.search("apple") << "\n"; // returns true + std::cout << trie.search("app") << "\n"; // returns false + std::cout << trie.startsWith("app") << "\n"; // returns true + trie.insert("app"); + std::cout << trie.search("app") << "\n"; // returns true + return 0; + } + ``` + + + + ```java + class TrieNode { + TrieNode[] child; + boolean isWord; + + public TrieNode() { + child = new TrieNode[26]; + isWord = false; + for (int i = 0; i < 26; i++) { + child[i] = null; + } + } + } + + class Trie { + TrieNode root; + + public Trie() { + root = new TrieNode(); + } + + public void insert(String s) { + TrieNode p = root; + for (char c : s.toCharArray()) { + int i = c - 'a'; + if (p.child[i] == null) p.child[i] = new TrieNode(); + p = p.child[i]; + } + p.isWord = true; + } + + public boolean search(String key, boolean prefix) { + TrieNode p = root; + for (char c : key.toCharArray()) { + int i = c - 'a'; + if (p.child[i] == null) return false; + p = p.child[i]; + } + if (!prefix) return p.isWord; + return true; + } + + public boolean startsWith(String prefix) { + return search(prefix, true); + } + + public static void main(String[] args) { + Trie trie = new Trie(); + trie.insert("apple"); + System.out.println(trie.search("apple")); // returns true + System.out.println(trie.search("app")); // returns false + System.out.println(trie.startsWith("app")); // returns true + trie.insert("app"); + System.out.println(trie.search("app")); // returns true + } + } + ``` + + + + ```python + class TrieNode: + def __init__(self): + self.child = [None] * 26 + self.isWord = False + + class Trie: + def __init__(self): + self.root = TrieNode() + + def insert(self, s: str) -> None: + p = self.root + for a in s: + i = ord(a) - ord('a') + if not p.child[i]: + p.child[i] = TrieNode() + p = p.child[i] + p.isWord = True + + def search(self, key: str, prefix: bool = False) -> bool: + p = self.root + for a in key: + i = ord(a) - ord('a') + if not p.child[i]: + return False + p = p.child[i] + if not prefix: + return p.isWord + return True + + def startsWith(self, prefix: str) -> bool: + return self.search(prefix, True) + + # Driver code + if __name__ == "__main__": + trie = Trie() + trie.insert("apple") + print(trie.search("apple")) # returns True + print(trie.search("app")) # returns False + print(trie.startsWith("app")) # returns True + trie.insert("app") + print(trie.search("app")) # returns True + ``` + + + + ```c + #include + #include + #include + + // Definition for TrieNode + typedef struct TrieNode { + struct TrieNode* child[26]; + bool isWord; + } TrieNode; + + // Function to create a new TrieNode + TrieNode* createTrieNode() { + TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode)); + node->isWord = false; + for (int i = 0; i < 26; i++) { + node->child[i] = NULL; + } + return node; + } + + typedef struct Trie { + TrieNode* root; + } Trie; + + // Function to create a new Trie + Trie* createTrie() { + Trie* trie = (Trie*)malloc(sizeof(Trie)); + trie->root = createTrieNode(); + return trie; + } + + // Function to insert a word into the Trie + void insert(Trie* trie, char* s) { + TrieNode* p = trie->root; + while (*s) { + int i = *s - 'a'; + if (!p->child[i]) p->child[i] = createTrieNode(); + p = p->child[i]; + s++; + } + p->isWord = true; + } + + // Function to search for a word or prefix in the Trie + bool search(Trie* trie, char* key, bool prefix) { + TrieNode* p = trie->root; + while (*key) { + int i = *key - 'a'; + if (!p->child[i]) return false; + p = p->child[i]; + key++; + } + if (!prefix) return p->isWord; + return true; + } + + // Function to check if a prefix exists in the Trie + bool startsWith(Trie* trie, char* prefix) { + return search(trie, prefix, true); + } + + // Driver code + int main() { + Trie* trie = createTrie(); + insert(trie, "apple"); + printf("%d\n", search(trie, "apple", false)); // returns 1 (true) + printf("%d\n", search(trie, "app", false)); // returns 0 (false) + printf("%d\n", startsWith(trie, "app")); // returns 1 (true) + insert(trie, "app"); + printf("%d\n", search(trie, "app", false)); // returns 1 (true) + return 0; + } + ``` + + + + +## Video Lecture: + + diff --git a/dsa-solutions/lc-solutions/0200-0299/image-1.png b/dsa-solutions/lc-solutions/0200-0299/image-1.png new file mode 100644 index 000000000..b3e7af253 Binary files /dev/null and b/dsa-solutions/lc-solutions/0200-0299/image-1.png differ diff --git a/dsa-solutions/lc-solutions/0200-0299/image.png b/dsa-solutions/lc-solutions/0200-0299/image.png new file mode 100644 index 000000000..cb917b6d4 Binary files /dev/null and b/dsa-solutions/lc-solutions/0200-0299/image.png differ