✅✅Beats 100% Users - C++ || With explanation ||✅✅✅
Intuition\nThis code seems to count the number of "beams" in a collection of strings. A "beam" is defined as a sequence of consecutive \'1\'s within a string.\n
# Intuition\nThis code seems to count the number of "beams" in a collection of strings. A "beam" is defined as a sequence of consecutive \'1\'s within a string.\n# Approach\nHere\'s a step-by-step analysis of the approach used in the code:\n\n1. Initializing Variables: ans and pre are initialized to 0. ans will store the final count of beams, while pre keeps track of the count of \'1\'s encountered in the previous string.\n2. Iterating through Strings: The code loops through each string in the bank.\n3. Counting \'1\'s: For each string, it counts the occurrences of \'1\'s and stores the count in the variable count.\n4. Calculating Beam Count: If count is not zero (meaning there are \'1\'s in the string), it updates the ans by adding the product of pre (the count of \'1\'s in the previous string) and count (the count of \'1\'s in the current string). Then, it updates pre to the current count for the next iteration.\n\n# Complexity\n- Time complexity:\nThe code iterates through each character of each string once. Therefore, the time complexity appears to be O(n * m), where n is the number of strings in bank and m is the average length of the strings.\n- Space complexity:\nThe code uses a few integer variables regardless of the input size. Hence, the space complexity is O(1) or constant space.\n# Code\n```\nclass Solution\n{\npublic:\n Solution(){\n    ios_base::sync_with_stdio(false);\n    cin.tie(NULL);\n    cout.tie(NULL);\n}\n\n    int numberOfBeams(vector<string> &bank)\n    {\n        int ans = 0, pre = 0;\n        for (auto &&s : bank)\n        {\n            int count = 0;\n            for (auto &&i : s)\n            {\n                if (i == \'1\')\n                {\n                    count++;\n                }\n            }\n\n            if (count != 0)\n            {\n                ans += (pre * count);\n                pre = count;\n            }\n        }\n        return ans;\n    }\n};\n\n```
['Array', 'Math', 'String', 'Matrix', 'C++']