Kshitij Gupta’s Post

🧠 LeetCode POTD — Simple Problem, One Small Trick 2515. Shortest Distance to Target String in a Circular Array At first glance, this feels very straightforward. 👉 Find all positions of target 👉 Compute distance from startIndex (from both directions) 👉 Take the minimum Done. But then I thought… Why am I finding all positions first? We're allowed to move left or right, one step at a time. ━━━━━━━━━━━━━━━━━━━ 💡 So instead, I started from startIndex and expanded in both directions simultaneously. 📌 The moment we hit the target → that distance is already the minimum. No need to check anything else. ━━━━━━━━━━━━━━━━━━━ The only tricky part? Handling the circular nature of the array. 💡 Clean trick — both directions in the same loop: → Right: (start + i) % n → Left: (start - i + n) % n That's it. ━━━━━━━━━━━━━━━━━━━ 📌 What I liked about this problem: Not about complexity. Not about fancy algorithms. Just about finding a clean way to move in both directions. ✅ Sometimes the hardest part isn't solving… ✅ It's writing a simple idea cleanly. Curious if someone approached this differently 👀 #LeetCode #ProblemSolving #SoftwareEngineering #DSA #C++ #Java #SDE

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories