LeetCode Solution: Check if Strings Can be Made Equal With Operations II

🚀 Day 547 of #750DaysOfCode 🚀 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 🚀 Day 547 of #750DaysOfCode 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 📈 Complexity Time: O(n) Space: O(1) 💬 Key Takeaway This problem is a great example of: 👉 Index grouping + frequency balancing And the optimization shows: 👉 How bit manipulation (i & 1) + offset trick can reduce space & simplify logic 🔥 🔁 Small optimizations → Big impact over time Consistency continues 💯 #LeetCode #Algorithms #DataStructures #Java #ProblemSolving #CodingChallenge #750DaysOfCode #Consistency

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories