🚀 Day 57 of #100DaysOfCode Today’s problem was a clean and classic two-pointer exercise — 🔁 LeetCode 344: Reverse String Simple on the surface, but a great reminder of in-place algorithms and pointer manipulation. 📌 Problem Summary You’re given a character array s. Your task is to reverse the array in-place, using O(1) extra space. 🧠 Approach Used: Two Pointers ✔️ Initialize: left = 0 right = s.length - 1 ✔️ Swap characters while left < right, then move pointers inward. This ensures: No extra memory Linear traversal Clean and readable logic ⚙️ Complexity Analysis ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) (in-place) ✔️ 477 / 477 test cases passed 🚀 Runtime: 0 ms (Beats 100%) 🔥 Key Learning Even the simplest problems reinforce core fundamentals: Two-pointer technique In-place operations Space optimization Mastering basics = dominating harder problems later 💪 Onward to Day 58 🚀 #100DaysOfCode #LeetCode #ReverseString #TwoPointers #Java #DSA #ProblemSolving #CodingJourney #Consistency
Reversing a String in-place with Two Pointers
More Relevant Posts
-
🚀 Solved LeetCode 338: Counting Bits 🚀 I recently worked on the Counting Bits problem, where the task is: 👉 Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i. 🔹 My Approach: Initialize an array of size n+1. For each number from 1 to n: Copy the number into a temporary variable. Use a loop to repeatedly divide by 2, counting the remainder (num % 2) each time. Store the total count of 1’s in the array. Return the final array. 📊 Complexity Analysis: Time Complexity: O(n log n) Each number requires about log(i) steps to process its binary digits. Space Complexity: O(n) Only the result array of size n+1 is used. ✨ Key Learning: This problem strengthened my understanding of binary representation and iterative problem solving. It was a great reminder that even simple bitwise operations can unlock powerful solutions. #LeetCode #Java #ProblemSolving #BackendDevelopment #CodingJourney #BitManipulation
To view or add a comment, sign in
-
-
🚀Day 1 of #100 Days of Code Solved: Minimum Limit of Balls in a Bag (LeetCode) Just wrapped up this interesting problem that combines Binary Search + Greedy thinking. Sharing a quick breakdown of the approach I used 👇 Key Idea :- We are not distributing balls directly — instead, we binary search on the answer (the maximum balls allowed in any bag). ✅ Steps followed in the code: 1️⃣ Search Space Setup left = 1 → minimum possible size of a bag right = max(nums) → worst case, no split at all 2️⃣ Binary Search on the Answer For a guessed value mid, assume no bag can have more than mid balls 3️⃣ Calculate Required Operations For each bag with n balls, operations needed = (n - 1) / mid This formula tells how many splits are required to keep each part ≤ mid 4️⃣ Decision Making If total operations > maxOperations → mid is too small → move left = mid + 1 Else → mid is valid → try smaller value → right = mid 5️⃣ Final Answer When left == right, we’ve found the minimum possible maximum bag size ⏱️ Complexity Time: O(n log max(nums)) Space: O(1) This problem is a great example of how binary search isn’t just for sorted arrays — it’s powerful when applied to answer optimization problems. #LeetCode #BinarySearch #DSA #ProblemSolving #Java #CodingJourney 🚀
To view or add a comment, sign in
-
-
🚀 Day 77 of #100DaysOfCode Today’s challenge was a classic two-pointer optimization problem — LeetCode: Container With Most Water 💧 📌 Problem Summary You’re given an array where each element represents the height of a vertical line. The task is to find two lines that together with the x-axis form a container that holds the maximum amount of water. 🧠 Approach Used: Two Pointers Instead of checking all possible pairs (which would be inefficient), I used a two-pointer strategy: Start with pointers at both ends of the array Calculate the area using: area = min(height[left], height[right]) × (right − left) Move the pointer pointing to the smaller height, because that’s the only way to possibly increase the area This greedy logic drastically reduces unnecessary comparisons. ⚙️ Complexity ⏱ Time: O(n) 💾 Space: O(1) 🔥 Key Learnings Brute force isn’t always the answer — patterns like two pointers can cut complexity instantly Greedy decisions work well when backed by solid reasoning Pointer movement logic is just as important as the formula itself Another strong problem solved 💪 On to Day 78 🚀 #100DaysOfCode #LeetCode #TwoPointers #Java #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 17 – LeetCode 1004 | Sliding Window Done Right (O(n) Solution) Day 17 of solving problems consistently. Solved LeetCode 1004 – Max Consecutive Ones III. Problem: Given a binary array, you can flip at most k zeros. Return the maximum number of consecutive 1s possible. Brute force checks every subarray → O(n²) → wasteful. A better approach is Sliding Window + Two Pointers → O(n). Core logic: • Expand window with right pointer • Count zeros • If zeros exceed k → move left pointer • Track max window length This problem is a good reminder that: Constraints usually hint the optimal technique. If you see “subarray + max/min + linear time” → think Sliding Window first. Consistency builds skill. 17 days. No breaks. Code + explanation in the video 👇 #LeetCode #DSA #Algorithms #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingInterview #SoftwareDeveloper #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
Day 16 – LeetCode 1004 | Sliding Window Done Right (O(n) Solution) Day 16 of solving problems consistently. Solved LeetCode 1004 – Max Consecutive Ones III. Problem: Given a binary array, you can flip at most k zeros. Return the maximum number of consecutive 1s possible. Brute force checks every subarray → O(n²) → wasteful. A better approach is Sliding Window + Two Pointers → O(n). Core logic: • Expand window with right pointer • Count zeros • If zeros exceed k → move left pointer • Track max window length This problem is a good reminder that: Constraints usually hint the optimal technique. If you see “subarray + max/min + linear time” → think Sliding Window first. Consistency builds skill. 16 days. No breaks. Code + explanation in the video 👇 #LeetCode #DSA #Algorithms #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingInterview #SoftwareDeveloper #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
Day 7/60 – LeetCode Challenge 🔥 Problem: Container With Most Water Today’s concept: Two Pointers + Greedy 💡 The question is: Given an array of heights, find two lines that together hold the maximum water. Brute force approach? Compare every pair → O(n²) ❌ Better approach? Use Two Pointers. Intuition: Start with two pointers: • i at the beginning • j at the end Calculate area using: Area = (j - i) × min(height[i], height[j]) Important observation: • Width keeps decreasing as we move inward • To increase area, we must try increasing the smaller height So the strategy becomes: • Calculate current area • Update maxArea • Move the pointer pointing to the smaller height Why move the smaller one? Because the smaller height limits the water. Moving the taller one won’t help — the limiting height stays the same while width decreases. Time Complexity: O(n) Space Complexity: O(1) Key Learning: When the question involves: • Maximizing something between two ends • Comparing elements from both sides • Eliminating impossible choices logically Think → Two Pointers Day 7 completed ✅ On to Day 8 🚀 #LeetCode #DSA #TwoPointers #Java #CodingChallenge #PlacementPreparation
To view or add a comment, sign in
-
-
LeetCode Grinding 🚀 Today was focused on strengthening Two Pointers mastery and improving problem-solving speed through practice. 🔹 DSA & LeetCode Practice Solved multiple problems by applying the Two Pointers pattern: LeetCode 11 — Container With Most Water Learned how pointer movement helps maximize area efficiently LeetCode 125 — Valid Palindrome Applied two pointers for string validation after preprocessing LeetCode 2824 — Count Pairs Whose Sum is Less than Target Understood how sorted arrays + two pointers help count pairs efficiently LeetCode 26 — Remove Duplicates from Sorted Array Strengthened in-place array manipulation using pointer technique 📌 Key takeaway: The Two Pointers pattern is powerful for: Sorted arrays Pair problems String validation Space-optimized solutions The more I practice patterns, the faster I recognize where to apply them. #DailyGrind #DSA #LeetCode #TwoPointers #ProblemSolving #Java #Consistency #LearningInPublic #InterviewPrep
To view or add a comment, sign in
-
🚀 Day 488 of #500DaysOfCode 📌 LeetCode Problem: 2976. Minimum Cost to Convert String I Today’s challenge was about minimizing the cost to convert one string into another using a set of given character transformation rules — each with an associated cost. 💡 Key Idea: Model this as a graph problem where each lowercase character is a node, and every transformation is a directed edge with a cost. Then use Floyd–Warshall to compute the minimum conversion cost between all pairs of characters. 🔍 Approach Summary: ✔ Build a 26×26 cost matrix for all character conversions ✔ Use Floyd–Warshall to find the cheapest paths ✔ For each position in the string, sum the cost of converting source[i] → target[i] ✔ Return the total cost — or -1 if impossible ⚙️ Time Complexity: 26³ for preprocessing + O(n) for string traversal, which is efficient even for large inputs. 💬 What I learned today: Graph applications go beyond nodes and edges — even string transformations can be mapped beautifully as a shortest path problem! 🔁 Keep coding. Keep growing. #java #leetcode #codingchallenge #graphalgorithms #floydwarshall
To view or add a comment, sign in
-
-
🚀 Day 74 of #100DaysOfCode Today’s challenge was a clean and efficient array problem — LeetCode: Merge Sorted Array ✅ 📌 Problem Summary You’re given two sorted arrays, where the first array has extra space at the end. The task is to merge the second array into the first in-place, keeping everything sorted. 🧠 My Approach Instead of shifting elements forward (which is costly), I used a reverse two-pointer technique: Start comparing elements from the end of both arrays Place the larger element at the last available position Move pointers accordingly until all elements are merged This avoids extra space and keeps the solution optimal. ⚙️ Complexity ⏱ Time: O(m + n) 💾 Space: O(1) (in-place merge) 🔥 Key Learning Working from the back can simplify in-place array problems Two-pointer strategies are incredibly powerful for sorted data Another day, another solid problem solved 💪 Onward to Day 75 🚀 #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 35 of 50 Days of Code on LeetCode 🚀 Today I worked on a problem that highlighted the importance of ordering data correctly to uncover optimal solutions—finding all pairs with the minimum absolute difference in an array of distinct integers. 🔍 Approach: • Sorted the array to ensure proper ordering • Compared adjacent elements to identify the minimum absolute difference • Collected all pairs matching this minimum difference • Returned the pairs in ascending order automatically 💡 Key Learnings: • The closest values in a dataset often appear after sorting • Minimum absolute differences are found between adjacent elements • Preprocessing steps like sorting are crucial for correctness • Clean logic improves both efficiency and readability ⚙️ Complexity: Time: O(n log n) Space: O(1) (excluding output) This problem reinforced how a simple preprocessing step can transform a problem into an elegant and interview-ready solution. Continuing to build strong DSA fundamentals in Java 🚀 #Day35 #50DaysOfCode #LeetCode #Java #DSA #ProblemSolving #Algorithms #CodingJourney #PlacementPreparation #LearningByDoing
To view or add a comment, sign in
-
Explore related topics
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