📌 Day 16/100 - Reverse String (LeetCode 344) 🔹 Problem: Reverse a given string in-place — meaning you must modify the original array of characters without using extra space. 🔹 Approach: Used the two-pointer technique — one starting at the beginning and one at the end of the array. Swap characters at both pointers, then move them closer until they meet. Efficient, clean, and runs in linear time without additional memory allocation. 🔹 Key Learnings: In-place algorithms optimize space complexity significantly. The two-pointer pattern is a versatile tool for many array and string problems. Understanding mutable vs immutable structures in Java is crucial for memory efficiency. Sometimes, the simplest logic beats the most complex one. 🧠 “True efficiency lies in simplicity, not complexity.” #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #TwoPointers
Reversing a string in-place with two pointers in Java
More Relevant Posts
-
Solved: LeetCode Problem 922 – Sort Array By Parity II 📌 Difficulty Level: Easy 💻 Language Used: Java 🧠 Topics: Arrays, Index Manipulation, Two-Pointer 🧩 Problem Overview: Rearrange the array so that elements at even indices are even numbers and elements at odd indices are odd numbers. 🎯 Key Learnings: ✅ Practiced separate pointer movement for even/odd indices ✅ Learned how to skip indices in steps of 2 for efficiency ✅ Strengthened control flow in condition-based pointer increment ✅ Designed an O(n) solution without extra space 🛠 Skills Practiced: Index Management Array Rearrangement Two-Pointer Optimization In-place Logic Building ⚡️ Problems like this sharpen thinking about index constraints and help in mastering array partitioning & arrangement patterns. #LeetCode #Java #RotateArray #ProblemSolving #Arrays #LogicBuilding #100DaysOfCode #CodeNewbie #TechCareers #WomenInTech #LearningInPublic #BuildInPublic #DeveloperJourney #CleanCode #DSA #AlgorithmPractice #CodingInterviewPrep #SoftwareDevelopment #AfrozCodes #DailyCoding #CrackTheCodingInterview #TechForAll
To view or add a comment, sign in
-
-
🔥 LeetCode Day--- 4 | “Median of Two Sorted Arrays” (Hard, Java) Today’s challenge was one of those that really test your logic, patience, and understanding of binary search. This problem wasn’t about just merging two sorted arrays — it was about thinking smarter 🧠. Instead of brute-forcing through both arrays (O(m+n)), I implemented a binary partition approach to achieve O(log(min(m, n))) efficiency 💡 What I learned today: Always choose the smaller array for binary search — it makes the partition logic simpler. Handle boundaries carefully with Integer.MIN_VALUE and Integer.MAX_VALUE. The goal is to find the perfect partition where: Left half ≤ Right half Elements are balanced across both arrays Once that’s done → median can be easily calculated! ✅ Result: Accepted | Runtime: 0 ms 🚀 Hard problem turned into a logic puzzle that was actually fun to solve! 🧩 Concepts Strengthened: Binary Search Partitioning Logic Edge Case Handling Mathematical Thinking #LeetCode #Day4 #Java #BinarySearch #ProblemSolving #CodingChallenge #DataStructures #Algorithms #CodeEveryday #DeveloperJourney #TechLearning #LeetCodeHard #CodingCommunity
To view or add a comment, sign in
-
-
🔢 Today I worked on a classic matrix problem in Java — Diagonal Sum. The goal was simple: Calculate the sum of both the primary and secondary diagonals of a square matrix. Initially, I used a nested loop approach with O(n²) complexity. But then I optimized it to a clean O(n) solution by directly accessing diagonal indexes. While optimizing, I made an interesting mistake: ❌ I wrote if (i != matrix[i][matrix.length - 1 - i]) Here I accidentally compared an index with an element value. ✔ Correct approach: if (i != matrix.length - 1 - i) This ensures the center element in odd-sized matrices isn’t counted twice. 🧠 Key Learning: Indexes and values may look similar, but mixing them can break logic silently. Optimization is not just about speed — it’s about accuracy. #Java #leetcode #Coding #DSA #ProblemSolving #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 89 of #100DaysOfCode Solved Find Numbers with Even Number of Digits in Java 🔢 Approach The goal of this problem was to count how many integers in a given array have an even number of digits. I implemented two methods: findNumbers(int[] nums): This is the main function that iterates through every number in the input array nums. isEvenOrOdd(int n): This helper function takes an integer and determines if its digit count is even or odd. Inside the helper function, I used a while loop to count the digits: I initialized a count to 0. The loop continues as long as $n > 0$. In each iteration, I increment count and then perform integer division by 10 (n = n / 10) to remove the least significant digit. Finally, I return true if the total count of digits is even (count \% 2 == 0). This efficient, digit-by-digit checking approach resulted in a strong performance, beating 98.90% of other submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Array #ProblemSolving #Optimization
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Day 85 of #100DaysOfCode Solved Range Sum Query - Immutable (NumArray) in Java ➕ Approach Today's problem was a classic that demonstrates the power of pre-computation: finding the sum of a range in an array many times. The optimal solution is the Prefix Sum technique. Pre-computation: In the constructor, I built a prefixSum array where prefixSum[i] holds the sum of all elements from index 0 up to index $i-1$ in the original array. This takes $O(N)$ time. $O(1)$ Query: The magic happens in the sumRange(left, right) method. The sum of any range $[left, right]$ is found instantly by calculating prefixSum[right + 1] - prefixSum[left]. The cost of a single $O(N)$ setup is outweighed by the ability to perform every subsequent query in $O(1)$ time! #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #PrefixSum #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
🎯 LeetCode 394 – Decode String Today I solved a really interesting Stack + String based problem! 💡 Problem Summary: We are given an encoded string containing patterns like 3[a2[c]] where: Numbers represent how many times the substring should repeat. Nested patterns are allowed. The goal is to decode the string correctly. 🧠 Approach: I used two stacks: One to store counts (how many times to repeat) One to store previously formed strings We iterate through the string: Build numbers when digits appear When [ appears, push state to stacks When ] appears, pop and reconstruct substring Else, just append characters normally ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(n) #LeetCode #Java #DSA #Stack #StringManipulation #CodingJourney #LearnEveryday #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
💻 Day 45 of #LeetCode Journey 🚀 ✅ Problem: Count and Say 📘 Language: Java 🔹 Status: Accepted (30/30 test cases passed) 🔹 Runtime: 4 ms | Beats 55.38% 🔹 Memory: 41.86 MB 🧠 Concept: This problem is about generating the n-th term in the “count and say” sequence. Each term is built by describing the previous term — count the number of digits and say them in order. 🧩 Approach: Start with "1". For each iteration, use a StringBuilder to construct the next sequence. Track consecutive digits using a counter. Append the count and digit when the sequence changes. 💡 Efficient string manipulation and iteration give optimal performance. 🔥 Every solved problem builds confidence. One step closer to mastering patterns in strings! #Day45 #LeetCode #Java #CodingChallenge #ProblemSolving #CountAndSay #50DaysOfCode
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 67 Reverse Words in a String Problem: – Reverse Words in a String Task: – Given a string, reverse the order of the words and remove extra spaces. Example: Input: s = " the sky is blue " → Output: "blue is sky the" My Approach: Used trim() to remove leading and trailing spaces. Split the string using split("\\s+") to handle multiple spaces. Reversed the array and joined the words with a single space. Time Complexity: O(N) | Space Complexity: O(N) Even simple string problems can teach the importance of clean code and efficient use of built-in methods. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #GeeksForGeeks #CodeNewbie #StringManipulation
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