LeetCode 821: Shortest Distance to a Character in Java

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

  • text

To view or add a comment, sign in

Explore content categories