Intuitive || Detailed Explanation || Beats 97% || [Java/Python3/C++/JavaScript]
Intuition\n1. We know the laser beam are independednt, but they will go to the device present in next row.\n2. Every laser beam will go the all the security dev
# Intuition\n1. We know the laser beam are independednt, but they will go to the device present in next row.\n2. Every laser beam will go the all the security devices of next row.\n3. If 3 devices is present in 1st row and 2 in second row, then total beams will be 6.\n4. We have to count the total beam.\n\n# Approach\n1. Initialize `prev` and `ans` to `0`. `prev` is used to keep track of the number of `1s` in the previous string, and ans will store the final result.\n2. Iterate through each string `str` in the bank array.\n3. For each string, iterate through its characters using a for-each loop. If the character is `1`, increment the `curr` variable.\n4. Check if `curr` (the number of `1s` in the current string) is greater than `0`. If true, update ans by adding the product of `prev` and `curr` to it. Update `prev` to be the current value of `curr`.\n5. Continue this process for all strings in the bank array.\n6. Finally, return the calculated ans.\n> Note: `Curr` holds the number of device in current row, `prev` holds the number of device in previous row.\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n*m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int prev = 0;\n        int ans = 0;\n        for(String str : bank) {\n            int curr = 0;\n            for(char ch : str.toCharArray()) {\n                if(ch==\'1\') {\n                    curr++;\n                }\n            }\n            if(curr > 0) {\n                ans += prev * curr;\n                prev = curr;\n            }\n        }\n        return ans;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int numberOfBeams(std::vector<std::string>& bank) {\n        int prev = 0;\n        int ans = 0;\n        \n        for (const std::string& str : bank) {\n            int curr = 0;\n            for (char ch : str) {\n                if (ch == \'1\') {\n                    curr++;\n                }\n            }\n            if (curr > 0) {\n                ans += prev * curr;\n                prev = curr;\n            }\n        }\n        \n        return ans;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def numberOfBeams(self, bank: List[str]) -> int:\n        prev = 0\n        ans = 0\n\n        for str in bank:\n            curr = 0\n            for ch in str:\n                if ch == \'1\':\n                    curr += 1\n            if curr > 0:\n                ans += prev * curr\n                prev = curr\n\n        return ans\n```\n```JavaScript []\n/**\n * @param {string[]} bank\n * @return {number}\n */\nvar numberOfBeams = function(bank) {\n    let prev = 0;\n    let ans = 0;\n\n    for (const str of bank) {\n        let curr = 0;\n        for (const ch of str) {\n            if (ch === \'1\') {\n                curr++;\n            }\n        }\n        if (curr > 0) {\n            ans += prev * curr;\n            prev = curr;\n        }\n    }\n\n    return ans;\n};\n```\n
['C++', 'Java', 'Python3', 'JavaScript']