✅Beats 99.01% | Java Solution | Iterate and Count | Maths
Intuition\n Describe your first thoughts on how to solve this problem. \n- We are identifying the presence of security devices in each row. This information is 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- We are identifying the presence of security devices in each row. This information is crucial because laser beams are formed between two security devices on different rows.\n\n- We are only interested in rows that have at least one security device. This filtering step ensures that laser beams can potentially be formed between non-empty rows.\n\n- The calculation of the product of consecutive non-zero row counts reflects the possible combinations of laser beams between these rows. The sum aggregates the total number of independent laser beams.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Iterate through each string in the bank array.\n- For each string, iterates through its characters.\n- Counts the occurrences of \'1\' in the string.\n- If the count for a string is not zero, proceed to next step else start from calcultaing \'1\' in next string of bank array.\n- Calculate the sum of the previous row and current row and assign current row to previous row.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    n = length of bank array\n    m = length of string in bank array\n\n    Complexity: O(n * m)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n# Code\n```\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int prevRowCount = 0;\n        int currRowCount = 0;\n        int totalCount = 0;\n\n        for(int i = 0; i < bank.length; i++){\n            currRowCount = securityAlarmCount(bank[i]);\n            if(currRowCount == 0) continue;\n            totalCount += currRowCount * prevRowCount;\n            prevRowCount = currRowCount;\n        }\n        return totalCount;\n    }\n\n    private int securityAlarmCount(String a){\n        int count = 0;\n        for(int i = 0; i < a.length(); i++){\n            if(a.charAt(i) == \'1\'){\n                count += 1;\n            }\n        }\n        return count;\n    }\n}\n```\n\nPlease upvote :)
['Array', 'Math', 'String', 'Matrix', 'Java']