[ { "title": "Reverse Vowels of a String", "url": "https://leetcode.com/problems/reverse-vowels-of-a-string/", "difficulty": "easy", "tag": "String", "pattern": [ "Two Pointers" ], "revision": false, "solved": false, "leetcode": true, "geeksforgeeks": false, "interviewbit": false, "count": 1, "top100": false, "lovebabbar": false, "striver": false, "neetcode": true, "blind75": false, "grind75": false, "apnaCollege": false, "algoPrep": false, "arshDSASheet": false, "algoMaster": false, "instabyte": false, "interviewMemoryTrick": "Scan inward, swap only vowels.", "notes": "", "videoSolution": "https://www.youtube.com/results?search_query=Reverse+Vowels+of+a+String", "textSolution": "## 🏆 Two Pointer Approach (Swap Vowels)\n\n### 💡 Idea\n\n- Use two pointers\n- One from start\n- One from end\n- Move inward and swap vowels only\n- Skip consonants\n\n---\n\n### Java Code\n\n```java\nclass Solution {\n public String reverseVowels(String s) {\n\n char[] arr = s.toCharArray();\n int left = 0;\n int right = arr.length - 1;\n\n while (left < right) {\n\n while (left < right && !isVowel(arr[left])) {\n left++;\n }\n\n while (left < right && !isVowel(arr[right])) {\n right--;\n }\n\n char temp = arr[left];\n arr[left] = arr[right];\n arr[right] = temp;\n\n left++;\n right--;\n }\n\n return new String(arr);\n }\n\n private boolean isVowel(char c) {\n c = Character.toLowerCase(c);\n return c == 'a' || c == 'e' || c == 'i'\n || c == 'o' || c == 'u';\n }\n}\n```\n\n---\n\n## 🧠 Line-by-Line Explanation\n\n### 1️⃣ Convert String to Array\n\n- Strings are immutable in Java\n- Convert to char array to allow swapping\n\n### 2️⃣ Initialize Two Pointers\n\n- `left` starts from beginning\n- `right` starts from end\n- We move inward toward center\n\n### 3️⃣ Move Left Pointer\n\n- Skip characters until a vowel is found\n- Ignore consonants\n\n### 4️⃣ Move Right Pointer\n\n- Move backward until a vowel is found\n- Ignore consonants\n\n### 5️⃣ Swap Vowels\n\n- Swap characters at `left` and `right`\n- Ensures vowels are reversed only\n\n### 6️⃣ Move Both Pointers\n\n- Move inward after swap\n- Continue until pointers cross\n\n---\n\n## 🧪 Example Walkthrough\n\n### Input: \"leetcode\"\n\n- Vowels: e, e, o, e\n- Swap first and last vowel\n- Then move inward\n\nResult: \"leotcede\"\n\n---\n\n## ⏱️ Complexity\n\n- Time: O(n) - Each character visited at most once\n- Space: O(n) - Due to char array copy\n\n---\n\n## 🎯 Interview One-Liner\n\n'I use two pointers from both ends, skip consonants, and swap vowels in a single pass to achieve O(n) time complexity.'\n\n---\n\n## ❓ Follow-Up Question\n\n**Q:** Can this be done using a stack instead of two pointers?\n\n**A:** Yes, push vowels into a stack and replace them while traversing again, but that increases space usage.\n\n---\n\n## 🧩 Important Notes\n\n- Handle both uppercase and lowercase vowels\n- Always check `left < right` in inner loops\n- Edge cases:\n - Empty string\n - No vowels\n - Single vowel\n - All vowels\n\n---\n\n## 🧠 Memory Trick\n\n**\"Skip consonants, swap vowels.\"**" }, { "title": "Fibonacci Number", "url": "https://leetcode.com/problems/fibonacci-number/", "difficulty": "easy", "tag": "Dynamic Programming", "pattern": [ "Dynamic Programming", "Memoization" ], "revision": false, "solved": false, "leetcode": true, "geeksforgeeks": true, "interviewbit": false, "count": 1, "top100": false, "lovebabbar": false, "striver": false, "neetcode": true, "blind75": false, "grind75": false, "apnaCollege": false, "algoPrep": false, "arshDSASheet": false, "algoMaster": false, "instabyte": false, "interviewMemoryTrick": "F(n) = F(n-1) + F(n-2),\n Just keep two variables rolling.", "notes": "", "videoSolution": "https://www.youtube.com/results?search_query=Fibonacci+Number+LeetCode", "textSolution": "## 🏆 Iterative Bottom-Up Approach\n\n### 💡 Idea\n\n- Start from base cases F(0) = 0, F(1) = 1\n- Iteratively compute F(n) using two rolling variables\n- No recursion overhead, no extra space\n\n---\n\n### Java Code\n\n```java\nclass Solution {\n public int fib(int n) {\n if (n <= 1) return n;\n\n int prev2 = 0;\n int prev1 = 1;\n\n for (int i = 2; i <= n; i++) {\n int curr = prev1 + prev2;\n prev2 = prev1;\n prev1 = curr;\n }\n\n return prev1;\n }\n}\n```\n\n---\n\n## 🧠 Line-by-Line Explanation\n\n### 1️⃣ Base Case Check\n\n- If n is 0 or 1, return n directly\n- F(0) = 0, F(1) = 1\n\n### 2️⃣ Initialize Two Variables\n\n- `prev2` = F(0) = 0\n- `prev1` = F(1) = 1\n- These represent the two previous Fibonacci numbers\n\n### 3️⃣ Iterate from 2 to n\n\n- Compute current = prev1 + prev2\n- Shift: prev2 becomes prev1, prev1 becomes current\n- This avoids storing the entire array\n\n---\n\n## 🧪 Example Walkthrough\n\n### Input: n = 5\n\n| i | prev2 | prev1 | curr |\n|---|-------|-------|------|\n| 2 | 0 | 1 | 1 |\n| 3 | 1 | 1 | 2 |\n| 4 | 1 | 2 | 3 |\n| 5 | 2 | 3 | 5 |\n\nResult: 5\n\n---\n\n## ⏱️ Complexity\n\n- Time: O(n) - Single pass from 2 to n\n- Space: O(1) - Only two variables used\n\n---\n\n## 🎯 Interview One-Liner\n\n'I use an iterative approach with two rolling variables to compute Fibonacci in O(n) time and O(1) space.'\n\n---\n\n## 🧠 Memory Trick\n\n**\"Two variables rolling forward — no array needed.\"**" }, { "title": "N-th Tribonacci Number", "url": "https://leetcode.com/problems/n-th-tribonacci-number/", "difficulty": "easy", "tag": "Dynamic Programming", "pattern": [ "Dynamic Programming", "Memoization" ], "revision": false, "solved": false, "leetcode": true, "geeksforgeeks": true, "interviewbit": false, "count": 1, "top100": false, "lovebabbar": false, "striver": false, "neetcode": true, "blind75": false, "grind75": false, "apnaCollege": false, "algoPrep": false, "arshDSASheet": false, "algoMaster": false, "instabyte": false, "interviewMemoryTrick": "T(n) = T(n-1) + T(n-2) + T(n-3),\n Three variables rolling.", "notes": "", "videoSolution": "https://www.youtube.com/results?search_query=N-th+Tribonacci+Number+LeetCode", "textSolution": "## 🏆 Iterative Bottom-Up Approach\n\n### 💡 Idea\n\n- Start from base cases T(0) = 0, T(1) = 1, T(2) = 1\n- Iteratively compute T(n) using three rolling variables\n- Similar to Fibonacci but with three terms\n\n---\n\n### Java Code\n\n```java\nclass Solution {\n public int tribonacci(int n) {\n if (n == 0) return 0;\n if (n <= 2) return 1;\n\n int t0 = 0;\n int t1 = 1;\n int t2 = 1;\n\n for (int i = 3; i <= n; i++) {\n int curr = t0 + t1 + t2;\n t0 = t1;\n t1 = t2;\n t2 = curr;\n }\n\n return t2;\n }\n}\n```\n\n---\n\n## 🧠 Line-by-Line Explanation\n\n### 1️⃣ Base Case Check\n\n- T(0) = 0\n- T(1) = 1, T(2) = 1\n- Return directly for these cases\n\n### 2️⃣ Initialize Three Variables\n\n- `t0` = T(0) = 0\n- `t1` = T(1) = 1\n- `t2` = T(2) = 1\n- These represent the three previous Tribonacci numbers\n\n### 3️⃣ Iterate from 3 to n\n\n- Compute current = t0 + t1 + t2\n- Shift: t0 → t1, t1 → t2, t2 → current\n- Rolling window of three values\n\n---\n\n## 🧪 Example Walkthrough\n\n### Input: n = 5\n\n| i | t0 | t1 | t2 | curr |\n|---|----|----|----|------|\n| 3 | 0 | 1 | 1 | 2 |\n| 4 | 1 | 1 | 2 | 4 |\n| 5 | 1 | 2 | 4 | 7 |\n\nResult: 7\n\n---\n\n## ⏱️ Complexity\n\n- Time: O(n) - Single pass from 3 to n\n- Space: O(1) - Only three variables used\n\n---\n\n## 🎯 Interview One-Liner\n\n'I extend the Fibonacci pattern to three terms, using three rolling variables to compute Tribonacci in O(n) time and O(1) space.'\n\n---\n\n## ❓ Follow-Up Question\n\n**Q:** How does this compare to Fibonacci?\n\n**A:** Same approach, but instead of summing 2 previous numbers, we sum 3. The rolling variable technique scales naturally.\n\n---\n\n## 🧠 Memory Trick\n\n**\"Fibonacci's bigger sibling — three terms instead of two.\"**" } ]