C++ || Easy Solution Intuitive ✅✅
Intuition\nThe goal of the problem is to count how many \'1\'s appear before each \'0\' in the given binary string s. Each \'0\' contributes to the count of \'1
# Intuition\nThe goal of the problem is to count how many \'1\'s appear before each \'0\' in the given binary string `s`. Each \'0\' contributes to the count of \'1\'s that precede it. By keeping track of the number of \'1\'s encountered as we traverse the string, we can efficiently calculate the desired result.\n\n# Approach\n1. **Initialization**: Create a vector `ones` to store the cumulative count of \'1\'s up to each index in the string. Also, use a map to keep track of the positions of \'1\'s.\n  \n2. **Counting \'1\'s**: Iterate through the string and fill the `ones` vector:\n   - If the current character is \'1\', increment the count from the previous index.\n   - If it\'s \'0\', carry forward the count of \'1\'s from the previous index.\n\n3. **Calculating Contributions from \'0\'s**: After populating the `ones` vector, iterate through the string again:\n   - For each \'0\', add the count of \'1\'s before it (from the `ones` vector) to the total count.\n\n4. **Return Result**: Finally, return the total count of \'1\'s before each \'0\'.\n\n# Complexity\n- Time complexity: $$O(n)$$\n  - The algorithm makes a single pass through the string to count \'1\'s and another pass to sum the contributions from \'0\'s, resulting in linear time complexity.\n\n- Space complexity: $$O(n)$$\n  - The space used is mainly for the `ones` vector, which stores counts for each character in the string.\n\n# Code\n```cpp\n#include <vector>\n#include <map>\n#include <string>\n\nclass Solution {\npublic:\n    long long minimumSteps(std::string s) {\n        int n = s.length();\n        std::vector<int> ones(n);\n        std::map<int, int> mp; // Not needed in final version\n        ones[0] = (s[0] == \'1\') ? 1 : 0;\n        \n        for (int i = 1; i < n; i++) {\n            if (s[i] == \'1\') {\n                ones[i] = ones[i - 1] + 1;\n            } else {\n                ones[i] = ones[i - 1];\n            }\n        }\n\n        long long cnt = 0;\n        for (int i = 0; i < n; i++) {\n            if (s[i] == \'0\') {\n                cnt += ones[i];\n            }\n        }\n        \n        return cnt;\n    }\n};\n
['Two Pointers', 'Greedy', 'Counting', 'C++']