Day41 - LeetCode Journey Solved LeetCode 821: Shortest Distance to a Character in Java ✅ Today’s problem was a great reminder that even simple-looking string questions can test how clearly you think. The goal was to find the shortest distance from every character in a string to a given target character. Sounds straightforward, but doing it efficiently is the real challenge. Instead of checking every position again and again, I used a smart two-pass approach. First pass from left to right to track the last seen occurrence. Second pass from right to left to ensure we capture the closest distance from both directions. Taking the minimum from both sides gave a clean and optimal solution. What I liked about this problem is how it trains you to think beyond brute force and focus on pattern recognition. Key takeaways: • Importance of multi-pass traversal in strings • Precomputing values to avoid unnecessary comparisons • Writing clean and readable logic • Thinking in terms of distance and optimization ✅ All test cases passed ✅ Improved confidence in string traversal patterns Small optimizations like these make a big difference in interviews. Staying consistent and sharpening fundamentals every day 💪 #LeetCode #DSA #Java #Strings #Algorithms #ProblemSolving #CodingJourney #InterviewPreparation #Consistency
LeetCode 821: Shortest Distance to a Character in Java
More Relevant Posts
-
Day 44 - LeetCode Journey Solved LeetCode 917: Reverse Only Letters in Java ✅ A clean two-pointer problem that really tests your understanding of string manipulation. The goal was simple: reverse only the letters while keeping all non-letter characters in their original positions. Instead of creating extra data structures, I used two pointers from both ends. If a character is not a letter, we skip it. When both pointers land on letters, we swap them. This continues until the pointers meet. What I liked about this problem is how it teaches you to control movement smartly rather than brute forcing the solution. Key takeaways: • Two-pointer technique for efficient traversal • Handling edge cases (symbols, numbers) • Writing in-place logic without extra space • Clean and readable code ✅ All test cases passed ✅ 100% runtime performance Problems like this improve your thinking for real interview scenarios where optimization matters. #LeetCode #DSA #Java #Strings #TwoPointers #Algorithms #ProblemSolving #CodingJourney #InterviewPrep #Consistency
To view or add a comment, sign in
-
-
🚀 Day 89 of My 100 Days LeetCode Challenge | Java Today’s problem was an interesting mix of string manipulation, sliding window, and pattern matching. The challenge was to determine the minimum number of flips required to convert a binary string into an alternating sequence, considering that the string can also be rotated. At first glance it feels like a simple flipping problem, but rotation adds another layer of complexity. The key idea was to simulate all rotations efficiently by doubling the string and using a sliding window to track mismatches against alternating patterns. Instead of checking every rotation individually, this approach allows us to compute the answer in linear time. ✅ Problem Solved: Minimum Number of Flips to Make the Binary String Alternating ✔️ All test cases passed (65/65) ⏱️ Runtime: 19 ms 🧠 Approach: Sliding Window + Pattern Matching 🧩 Key Learnings: ● Rotations can often be simulated by doubling the string. ● Sliding window techniques help avoid recomputation. ● Comparing with expected patterns simplifies mismatch counting. ● Efficient string handling is crucial for optimization problems. ● Sometimes the trick is not the operation itself, but how you simulate it efficiently. This problem was a great reminder that smart transformations can turn complex problems into linear-time solutions. 🔥 Day 89 complete — improving my understanding of sliding window techniques and string optimizations. #LeetCode #100DaysOfCode #Java #SlidingWindow #Strings #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfLeetCode Today I solved "Container With Most Water" problem on LeetCode. 🔹 Difficulty: Medium 🔹 Concept Used: Two Pointer Technique 🔹 Language: Java 📌 Problem Summary: Given an array representing heights of vertical lines, the goal is to find two lines that together with the x-axis can contain the maximum amount of water. 💡 Approach: Instead of checking all possible pairs (O(n²)), I used the Two Pointer approach which reduces the time complexity to O(n). Steps: 1️⃣ Start with two pointers at the beginning and end of the array. 2️⃣ Calculate the area using the smaller height. 3️⃣ Move the pointer with the smaller height inward. 4️⃣ Track the maximum area during each step. 📊 Result: ✅ Runtime: 5 ms (Beats 81.86%) ✅ Memory: Beats 95.54% This problem was a great exercise to understand optimization using two pointers instead of brute force. Consistency is the key — moving forward one problem at a time! 💪 #LeetCode #100DaysOfCode #Java #DSA #CodingChallenge #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 6/100 – LeetCode Challenge 🚀 Problem: #189 Rotate Array Difficulty: Medium Language: Java Approach: Array Reversal Technique Time Complexity: O(n) Space Complexity: O(1) 🔍 Key Insight: Instead of shifting elements one by one, the array can be rotated efficiently using a three-step reversal strategy. Steps: 1️⃣ Reverse the entire array 2️⃣ Reverse the first k elements 3️⃣ Reverse the remaining elements This achieves the required rotation in-place with constant extra space. 🧠 Solution Brief: Calculated k % n to handle cases where k is greater than array length. Reversed the entire array first. Then reversed the first k elements and the remaining n-k elements. This sequence correctly rotates the array to the right. 📌 What I Learned: Understanding patterns like array reversal can simplify problems that initially seem complex. Optimizing from brute force shifting to an in-place O(n) solution improves efficiency. #LeetCode #Day6 #100DaysOfCode #Java #DSA #Arrays #RotateArray #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 24 of #100DaysOfLeetCode 💻✅ Solved #35. Search Insert Position on LeetCode using Java. Approach: • Applied Binary Search to achieve O(log n) time complexity • Initialized two pointers: left and right • Calculated mid using left + (right - left) / 2 to avoid overflow • Compared mid element with target to adjust search space • If target not found, returned left as the correct insert position Performance: ✓ Runtime: 0 ms (Beats 100% submissions) ✓ Memory: 43.2 MB Key Learning: ✓ Strengthened understanding of Binary Search pattern ✓ Learned how to determine insert position when element is absent ✓ Improved confidence in handling sorted array problems efficiently Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinarySearch #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
LeetCode Problem || Find Unique Binary String (1980)🚀 Today I solved the problem "Find Unique Binary String" using Java. 🔹 Problem: We are given an array of n binary strings, each of length n. The goal is to return a binary string of length n that does not exist in the array. 🔹 Approach (Diagonal Flip Technique): The idea is simple but powerful: Traverse the array using index i. Look at the i-th character of the i-th string (nums[i][i]). Flip the bit (0 → 1, 1 → 0). Append it to a result string. 💡 Time Complexity: O(n) Practicing problems like this strengthens logical thinking and problem-solving skills. #LeetCode #Java #CodingPractice #ProblemSolving #DSA
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 35 📌 LeetCode : Minimum Depth of Binary Tree Today I solved the Minimum Depth of Binary Tree problem using Java. The goal was to find the shortest path from the root node to the nearest leaf node. I applied a recursive approach while carefully handling edge cases where one subtree is null. Instead of directly taking the minimum of both sides, I ensured the solution correctly skips null paths to avoid incorrect depth calculations. 📌 Key takeaways: 🔹 Understood the difference between minimum and maximum depth logic 🔹 Learned the importance of handling null child nodes 🔹 Strengthened recursion and tree traversal concepts 🔹 Improved problem-solving accuracy in edge cases This problem helped me think more clearly about tree structures and reinforced the importance of precise base conditions in recursion. #Java #LeetCode #DSA #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 46 - LeetCode Journey Solved LeetCode 412: Fizz Buzz in Java ✅ A classic problem, but a great reminder that even simple logic needs clarity and clean implementation. The task was to print numbers from 1 to n, but with a twist based on divisibility rules. If a number is divisible by both 3 and 5, we print "FizzBuzz". If only divisible by 3, print "Fizz". If only divisible by 5, print "Buzz". Otherwise, print the number itself. The key here was handling conditions in the correct order. Checking for 15 first avoids missing the "FizzBuzz" case. Key takeaways: • Importance of condition ordering • Writing clean and readable logic • Handling edge cases properly • Strong basics matter in interviews ✅ All test cases passed ✅ Clean and efficient implementation Simple problems like this help build a strong base. Mastering basics is what makes complex problems easier later 💪 #LeetCode #DSA #Java #ProgrammingBasics #Algorithms #ProblemSolving #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
Day 77 of 100: LeetCode Challenge – One Step Closer! 🚀 🎯 Problem: Combination Sum II 💡 Difficulty: Medium 🛠️ Language: Java 📌 Today’s win: Solved "Combination Sum II" — a follow-up to the previous combination problem, but this time with duplicates in the input and each number used only once per combination. 🧠 Approach: Sorted the array to handle duplicates easily Used backtracking with a "start" index to avoid reusing the same element Skipped duplicates during recursion to prevent repeated combinations 📊 Runtime: 8 ms (Beats 16.56%) 📦 Memory: 50.53 MB (Beats 5.73%) ✅ Passed all 176 test cases! 🔍 Key Takeaways: Sorting + backtracking is a classic combo for subset/combination problems Skipping duplicates in recursion is essential to avoid redundant results The "remain < 0" condition helps prune the search tree early 📈 74 days down, 26 to go — consistency is the real win! #LeetCode #100DaysOfCode #Java #CodingChallenge #Backtracking #CombinationSumII #ProblemSolving #CodeNewbie #DevCommunity #WomenWhoCode #LearnInPublic #DSA
To view or add a comment, sign in
-
-
Day 49 - LeetCode Journey Solved LeetCode 1021: Remove Outermost Parentheses in Java ✅ A neat stack-depth problem that tests your understanding of valid parentheses and string building. Problem idea: Given a valid parentheses string, remove the outermost parentheses of every primitive substring. Approach: Used a counter to track the depth of parentheses. • For '(' → add only if depth > 0, then increase depth • For ')' → decrease depth first, then add only if depth > 0 This way, we skip the outermost layer of each primitive. Key takeaways: • Understanding primitive decomposition of strings • Using counters instead of stacks for optimization • Writing efficient string manipulation logic • Clean handling of conditions without extra space Time Complexity: O(n) Space Complexity: O(n) ✅ All test cases passed ✅ Efficient and optimized approach Sometimes, tracking depth is all you need instead of a full stack 😄 #LeetCode #DSA #Java #Strings #Stack #ProblemSolving #CodingJourney #InterviewPrep #Consistency
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