✅ Beats 100% | Counting is simple 😉
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n All we need to do is to apply the multiplication rule which infers that if we ha
![image.png](https://assets.leetcode.com/users/images/b224e7d0-f5fc-4a45-acab-d70a683056be_1704284548.7757397.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* All we need to do is to apply the multiplication rule which infers that if we have n choices and each choice have m other choices, so the total number of choices are nXm\n* ie if we have 2 choices and each choice have 3 other choices, so we can name the first two choices **a1,a2** and the second three choices as **b1,b2,b3**\n* so all possible choices are as follows:\n    * a1b1\n    * a1b2\n    * a1b3\n    * a2b1\n    * a2b2\n    * a2b3\n* total number of choices are 6.  \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. we need two pointers\n    1. one pointing at the last visited floor with laser devices (**first_floor**).\n    2. other pointing at the current visited floor (**current floor**). \n2. initialize the **first_floor** with 0.\n3. initialize a counter variable in which we will store the result with 0. \n4. iterate over all the given strings.\n    1. if the **first_floor** was 0, this mean that we can not find any matching in the previous floor, so just update it with the number of laser devices in the current floor, and go for the next floor.\n    2. to get the number of laser devices, we just need to count the number of ones in each string, so we can do this in two main ways:\n        1.  either manually by iterating over the strig and count the number of 1.\n        2. using the built in method count, by giving it a pointer to the begin of the string, and the end of the string, and the item to search for.    \n    3. get the number of ones in the **current_floor**\n    4. if it was zero, just skip this floor\n    5. else:\n        1. multiply both if the previous floor, with the current floor.\n        2. add the result to the accumlative **res**.\n        3. update the previous floor value with the current value.\n5. return the result.   \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n1. O(n * m), where n is the number of strings in the vector, and m is the length of the longest given string.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n1. O(1) -> no extra space is needed.\n# Code\n```\n    #include <bits/stdc++.h>\n#pragma GCC optimize("O3")\n#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops", "no-stack-protector")\n#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c")\nstatic const auto DPSolver = []()\n{ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return \'c\'; }();\nusing namespace std;\nclass Solution {\npublic:\n\n    int numberOfBeams(vector<string> &bank)\n    {\n        DPSolver;\n        int first_floor = 0;\n        int res = 0;\n        for (string s : bank)\n        {\n            if (!first_floor)\n            {\n                first_floor = count(s.begin(), s.end(), \'1\');\n                continue;\n            }\n            // now first floor must have value.\n            int current_floor = count(s.begin(), s.end(), \'1\');\n            if (current_floor)\n            {\n                res += first_floor * current_floor;\n                first_floor = current_floor;\n            }\n        }\n        return res; \n    }\n};\n```
['Array', 'Math', 'String', 'Matrix', 'Counting', 'Number Theory', 'C++']