TypeScript Two Pointers Approach ✅
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition is to use two pointers: one (left) to track how many \'0\'s have been enc
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition is to use two pointers: one (left) to track how many \'0\'s have been encountered so far, and another (right) to iterate through the string. When a \'0\' is found at right, the difference between the right and left pointers tells us how many swaps are needed to bring that \'0\' to the left side (`Partitioning`). We accumulate these differences to get the total number of swaps.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n`left` keeps track of the number of \'0\'s that have been encountered.\n`total` accumulates the number of swaps needed.\n\n**For each character in the string (right pointer):**\n- If it\u2019s a `\'0\'`, calculate the distance it needs to travel to the left `(right - left)`.\n- Add this value to total and increment `left` (to track the next correct position for a `\'0\'`).\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```typescript []\nconst minimumSteps = (s: string): number => {\n  let left: number = 0\n  let total: number = 0\n\n  for (let right = 0; right < s.length; right++) {\n    if (s.charAt(right) === \'0\') {\n      total += right - left\n      left++\n    }\n  }\n\n  return total\n}\n```
['TypeScript']