LeetCode 657 Robot Return to Origin Easy Solution

LeetCode Daily | Day 69 🔥 LeetCode POTD – 657. Robot Return to Origin (Easy) ✨ 📌 Problem Insight Given a robot starting at (0, 0): ✔ Moves are given as a string of 'U', 'D', 'L', 'R' ✔ Each move shifts position by 1 unit ✔ Need to check → does it come back to origin? 🔍 Initial Thinking – Simulation ✔ 💡 Idea: ✔ Track x and y coordinates ✔ For every move: • 'U' → y-- (or y++) • 'D' → opposite of 'U' • 'R' → x++ • 'L' → x-- ✔ At the end → check (x == 0 && y == 0) ⏱ Complexity: O(n), Space: O(1) ✔ Single pass ✔ No extra data structures needed 💡 Cleaner Insight – Count Balance 🔥 👉 Instead of coordinates: ✔ Count 'U' == 'D' ✔ Count 'L' == 'R' ✔ If both balances hold → back to origin 🧠 Key Learning ✔ Simple simulation problems test clarity, not complexity ✔ Coordinate tracking is a powerful pattern ✔ Balance/count approach simplifies thinking ✔ Always look for symmetry in movement problems A very clean problem where observation > optimization ⚡ #LeetCode #DSA #Algorithms #CPlusPlus #ProblemSolving #CodingJourney #DataStructures

  • text

To view or add a comment, sign in

Explore content categories