🚀 Day 33 of My DSA Journey – Reverse Vowels of a String (LeetCode) Today I worked on a really interesting problem: Reverse Vowels of a String — and learned how powerful the two-pointer approach can be. 🔍 Problem Understanding We are given a string, and we need to reverse only the vowels while keeping the rest of the characters in the same position. 👉 Example: Input: "hello" Output: "holle" 🐢 Brute Force Approach Extract all vowels into a separate list Reverse that list Traverse the string again and replace vowels one by one ⛔ Time Complexity: O(n) ⛔ Space Complexity: O(n) (extra storage used) ⚡ Optimized Approach (Two Pointer) Instead of extra space, I used two pointers (i, j): Steps: Initialize: i = 0 (start) j = n-1 (end) Move i forward until it finds a vowel Move j backward until it finds a vowel Swap both vowels Repeat until i < j 🧠 Example Walkthrough Input: "leetcode" i → 'e', j → 'e' → swap i → 'e', j → 'o' → swap Final Output: "leotcede" ⏱️ Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) ✅ (in-place) 💡 Key Learning Two-pointer approach is extremely useful for string manipulation problems Always try to reduce extra space usage Clean condition handling (like checking vowels) matters a lot in interviews 🙏 Thanks to LeetCode for such great problems that strengthen problem-solving skills. 📈 Consistency is the key — small steps daily lead to big results. #DSA #Java #LeetCode #ProblemSolving #CodingJourney #100DaysOfCode #InterviewPreparation #SoftwareEngineering
Reverse Vowels of a String LeetCode Solution
More Relevant Posts
-
LeetCode 41 – First Missing Positive. 🧩 When I first read this problem, I thought it would be messy. But once I found the pattern, it turned out to be surprisingly clean. The problem in simple terms: Find the smallest positive integer missing from an unsorted array. The real challenge: O(n) time and O(1) extra space. No sorting, no hash maps, no extra arrays. At first, that feels impossible. But here's the trick that clicked for me: The answer has to be between 1 and n+1 (where n = length of array). Why? Because in the best case, if the array contains 1,2,3,...,n, then the missing number is n+1. Otherwise, there's a gap somewhere between 1 and n. Once I realized that, the solution became straightforward: Place each number in its correct position (number x should be at index x-1) | After rearranging, scan to find the first spot where the number doesn't match the index + 1 That index + 1 is our answer What I liked about this problem: It teaches you to work within tight constraints. Instead of adding more memory, you use the array itself as your workspace. If you're preparing for coding interviews, this is one of those problems worth understanding deeply, not for memorizing the solution but for learning how to think under constraints. Have you run into a problem that looked impossible but turned out to have a simple pattern? Curious to hear your experience. #LeetCode #CodingInterview #ProblemSolving #Java #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 25 of My DSA Journey – Mastering In-Place Array Manipulation Today I solved an interesting problem on LeetCode: 👉 Remove Duplicates from Sorted Array II 🔍 Problem Understanding Given a sorted array, the goal is to remove duplicates in-place such that each element appears at most twice, and return the new length. ⚠️ Constraint: No extra space allowed Modify array in-place 🧠 Brute Force Approach Use extra space (like ArrayList) Track frequency and rebuild array ❌ Not optimal due to O(n) space complexity ⚡ Optimized Approach (Two Pointer Technique) 💡 Key Idea: Since array is sorted, duplicates are adjacent. We can compare current element with the element 2 steps back. 🪜 Steps Start pointer i = 2 (since first 2 elements are always valid) Traverse from j = 2 → n-1 Check: If nums[j] != nums[i-2] → valid element Place it at index i and increment i 🧾 Code Snippet (Java) class Solution { public int removeDuplicates(int[] nums) { int i = 2; for(int j = 2; j < nums.length; j++) { if(nums[j] != nums[i-2]) { nums[i] = nums[j]; i++; } } return i; } } 🧪 Example Walkthrough Input: [1,1,1,2,2,3] Process: Allow only 2 occurrences Final array becomes: [1,1,2,2,3] Output: 5 ⏱️ Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) ✅ (In-place) 💡 Key Learning Sorted array → powerful advantage Two-pointer technique = must-know for interviews Comparing with i-2 is the trick 🔥 🙏 Gratitude Grateful for the consistency and learning curve. Every problem sharpens my logic a bit more! 📈 Consistency Note Showing up daily > being perfect occasionally 💯 #DSA #LeetCode #Java #CodingJourney #100DaysOfCode #SoftwareEngineering #ProblemSolving #InterviewPrep #TechLearning
To view or add a comment, sign in
-
-
Today I solved the problem: "Closest Target in Circular Array" 🔍 Problem Statement: Given an array of words and a starting index, find the shortest distance to a target word in a circular array. 💡 Key Idea: Since the array is circular, we must consider: ➡️ Direct distance ➡️ Circular distance 👉 Final distance = min(|i - startIndex|, n - |i - startIndex|) 🧑💻 My Optimized Java Solution: class Solution { public int closestTarget(String[] words, String target, int startIndex) { int n = words.length; int minDistance = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (words[i].equals(target)) { int dist = Math.abs(i - startIndex); int circularDist = Math.min(dist, n - dist); minDistance = Math.min(minDistance, circularDist); } } return minDistance == Integer.MAX_VALUE ? -1 : minDistance; } } 📈 What I Learned: How to handle circular array problems 🔄 Importance of optimizing brute-force solutions Writing clean and efficient code for interviews 🔥 Consistency is the key — one problem at a time! #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode #DSA
To view or add a comment, sign in
-
-
📅 Day 46/100 – LeetCode DSA Challenge 🧩 Problem: Longest Continuous Increasing Subsequence (674) 💡 Problem Summary Find the length of the longest continuous strictly increasing subarray. The sequence must be continuous, not just increasing elements from anywhere. 💡 My Approach I solved this problem by comparing each element with its next element: Initialized: count = 1 → current increasing sequence max = 1 → maximum length Traversed the array till n-1 to safely check nums[i+1] If nums[i] < nums[i+1] → increment count Else → reset count = 1 Continuously updated max using Math.max() This approach ensures we only consider continuous increasing sequences. 🧾 Code: class Solution { public int findLengthOfLCIS(int[] nums) { if(nums.length == 0) return 0; int count = 1; int max = 1; for(int i = 0; i < nums.length - 1; i++) { if(nums[i] < nums[i+1]) { count++; max = Math.max(count, max); } else { count = 1; } } return max; } } 📚 What I Learned How to safely use i+1 by controlling loop boundary Importance of handling edge cases (nums.length == 0) Difference between continuous vs non-continuous sequences Greedy approach for tracking subarray length Writing optimized O(n) solutions 🚀 Small improvements every day lead to big results! #Day46 #100DaysOfCode #LeetCode #DSA #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 140/360 – This Trick Generates All Subsets Instantly 🚀 Most people solve this using recursion… But today I learned how to generate all subsets using Bit Manipulation👇 📌 Topic: Bit Manipulation + Array 🧩 Problem: Subsets (Power Set) 📝 Problem Statement: Given an integer array, return all possible subsets (the power set). 🔍 Example: Input: [1, 2, 3] Output: [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]] 💡 Approach: Bit Masking (Optimized) ✔ Step 1 – Calculate total subsets = 2ⁿ using (1 << n) ✔ Step 2 – Loop from 0 to 2ⁿ - 1 (each number represents a subset) ✔ Step 3 – Use bits to decide whether to include an element ⚡ Key Idea: Each number (mask) represents a subset in binary form. If a bit is ON → include that element. ⏱ Complexity: Time → O(n × 2ⁿ) Space → O(n × 2ⁿ) 📚 What I Learned: Bit manipulation can replace recursion in subset generation and makes the logic easier to visualize in binary. 🚀 Why This Matters: This concept is widely used in backtracking, DP, and interview problems. #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #BitManipulation #TechJourney #140DaysOfCode
To view or add a comment, sign in
-
-
March wrap. What I built, what I solved, what's almost done. DSA. 35 problems in 30 days Arrays. Strings. Sliding window. Backtracking. Linked Lists. SQL. Backtracking was the hardest week. Generate Parentheses, 3Sum, Letter Combinations, Combination Sum. All in 7 days. The pattern didn't click on problem one. It clicked on problem three. One habit changed everything. Tracing on paper before writing any code. Not after getting stuck. Before. Should've started months ago. Java. Built something that actually runs. OOP, collections, Java 8, then Spring Boot. Built an async report generator. Jobs move through a state machine: queued, processing, done, failed. Thread pool handles the load. The async logic wasn't the hard part. A job kept getting stuck between PROCESSING and DONE and I had no idea why. Took an hour to find it. One missing state transition. Wrote it, broke it, found it. That's how I know I actually understand it now. ContentMorph. Almost there. Next.js 14, Tailwind, shadcn/ui, Tambo AI. Paste anything. Article, email, notes, code. The UI figures out the best way to show it. Architecture took a few sessions to get right. Old responses kept stacking every time a new message came in. Clean now. Dark charcoal and amber gold. Shipping soon. April. Trees start Monday. 40 problems. ContentMorph ships before the 15th. What are you building this month? #BuildingInPublic #DSA #LeetCode #Java #WebDevelopment
To view or add a comment, sign in
-
DSA Series... 🚀 Chapter 3 (Sliding Window Dynamic): LeetCode#424 (Level: Medium) "Longest Repeating Character Replacement" ... --- 🔹Problem You are given a string `s` and an integer `k`. You can replace "at most `k` characters" in the string to make all characters in a substring the same. 👉 Return the "length of the longest possible substring" with repeating characters after replacement. --- 🔹Constraints * `1 <= s.length <= 10^5`. * `s` consists of only uppercase English letters. * `0 <= k <= s.length`. --- 🔹Step-by-Step Procedure ... ✍️ 1⃣ Use Sliding Window (Dynamic) with two pointers: * `left = 0`, * Iterate `right` from 0 to 'n'. 2⃣ Maintain a frequency array/map for characters. 3⃣ Track: * `maxCount` - count of most frequent character in current window. 4⃣ Check window validity: * If `(window size - maxCount) > k` * Too many replacements needed. So, move `left++` and reduce frequency 5⃣ Always update: * `maxLen = max(maxLen, window size)` 6⃣ Continue until end. --- 🔹Key Takeaway ... 🎯 - We don’t actually replace characters. - We just ensure: 👉 "Window Size - Most Frequent Character ≤ k" This guarantees we can convert the whole window into one repeating character. If u have any other possible approach/suggestions, share below ... 👇 Stay tuned with same wibe ... 🔥 #LeetCode #SlidingWindow #Algorithms #DSA #CodingInterview #Java #ProblemSolving #TechLearning #SoftwareEngineering #InterviewPrep
To view or add a comment, sign in
-
-
**Day 110 of #365DaysOfLeetCode Challenge** Today’s problem: **Add Two Numbers II (LeetCode 445)** A very elegant **Linked List + Stack** problem. We are given two numbers stored in linked lists where the **most significant digit comes first**. Example: `7243 + 564 = 7807` Lists: `[7,2,4,3] + [5,6,4] = [7,8,0,7]` 💡 **Core Challenge:** Normally addition starts from the **last digit**… But linked lists are given from the **front**. So how do we add from right to left **without reversing the lists**? 📌 **Approach:** 1️⃣ Push digits of both linked lists into stacks 2️⃣ Pop from stacks → gives digits from right to left 3️⃣ Add with carry 4️⃣ Insert new node at the front of result list This preserves correct order naturally ⚡ **Time Complexity:** O(n + m) ⚡ **Space Complexity:** O(n + m) **What I learned today:** Stacks are incredibly useful when you need to process data in **reverse order**. 💭 **Key Takeaway:** If you can’t move backward easily: 👉 Use a stack 👉 Simulate reverse traversal 👉 Build result from front Clean solution. Smart idea. Great interview problem #LeetCode #DSA #LinkedList #Stack #CodingChallenge #ProblemSolving #Java #TechJourney #Consistency
To view or add a comment, sign in
-
-
I’ve been getting back into solving algorithm problems recently, and I worked through a pretty challenging one: “Delete Columns to Make Sorted III.” At first glance, it looks like a simple string problem. But once you dig in, it becomes a lot more interesting — it’s really about finding a pattern across multiple sequences. 💡 Key idea I learned: Instead of thinking about deleting columns directly, you can reframe the problem as: ➡️ “What is the longest sequence of columns that keeps all strings in sorted order?” Once you see it that way, it turns into a dynamic programming problem — similar to finding a longest increasing subsequence, but across multiple strings. 🧠 What this reinforced for me: Many “hard” problems become easier when you reframe the question Patterns like DP and subsequences show up everywhere Explaining the solution clearly is just as important as solving it I wrote a full breakdown of my approach here: 👉 https://lnkd.in/giN69zAn I’m currently focused on sharpening my problem-solving and backend thinking again. If you’re working through similar problems or preparing for interviews, I’d be interested to hear how you approach these kinds of challenges. #Java #LeetCode #Algorithms #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
🚀 Day 33 of My DSA Journey | Optimized Approach 💡 Today I solved one of the classic problems from LeetCode: Merge Sorted Array 🔥 🧩 Problem Understanding Given two sorted arrays nums1 and nums2, we need to merge them into a single sorted array. Constraint: nums1 already has enough space at the end to accommodate elements of nums2. 🐢 Brute Force Approach My first thought was: Copy all elements of nums2 into nums1 Sort the entire array ⏱️ Time Complexity: O((m+n)²) 👉 Works fine, but not efficient for interviews. ⚡ Optimized Approach (Two Pointer - Reverse Merge) Then I explored the optimal solution: 💡 Key Idea: Start filling from the end of nums1 Compare elements from the back of both arrays Place the larger one at the last index 🔁 Steps: Set 3 pointers: i = m-1 (end of valid nums1) j = n-1 (end of nums2) k = m+n-1 (end of nums1 array) Compare and place elements from the back Handle remaining elements of nums2 if any 📌 Example nums1 = [1,2,3,0,0,0] nums2 = [2,5,6] Output: [1,2,2,3,5,6] ⏱️ Complexity Analysis Time: O(m+n) 🚀 Space: O(1) (In-place) 🎯 Key Learning 👉 When arrays are sorted, always think in terms of two pointers 👉 Filling from the end avoids unnecessary shifting 👉 Interviewers expect optimization, not just working code 🙏 Gratitude Grateful for continuous learning and improvement every single day 💯 🔥 Consistency Note Small steps daily → Big results over time 🚀 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode #SoftwareEngineering #InterviewPrep #DataStructures #Algorithms
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development