Java ✅ || Easy Solution ✅ || 100% beats ✅ || Fully Explained ✅
Approach\nThe given problem involves finding the total number of laser beams in a bank, where the bank\'s floor plan is represented by a 0-indexed binary string
# Approach\nThe given problem involves finding the total number of laser beams in a bank, where the bank\'s floor plan is represented by a 0-indexed binary string array. Each row in the array corresponds to a row in the bank, and each character in the row can either be \'0\' (empty cell) or \'1\' (security device).\n\nTo solve the problem, the provided code implements the following approach:\n\n1. Initialize variables: The code starts by initializing two variables, "count" and "prev," to 0. The "count" variable will keep track of the total number of laser beams found, and "prev" will store the count of security devices in the previous row.\n\n2. Loop through each row: The code iterates through each row (floor) in the bank using a for-each loop.\n\n3. Count ones in the current row: For each row, the "countOnes" function is called to count the number of security devices (\'1\'s) in that row.\n\n4. Calculate the beams: If the number of security devices in the current row is not zero, it means there is at least one potential laser beam between the current row and the previous row. The code then updates the "count" variable by adding the product of the number of security devices in the current row and the number of security devices in the previous row ("ones * prev"). The "prev" variable is then updated to the number of security devices in the current row to be used in the next iteration.\n\n5. Return the total count: After processing all rows, the function returns the final "count," which represents the total number of laser beams in the bank.\n\nLogic behind the approach:\n- The approach leverages the fact that to form a laser beam, there must be security devices on two different rows, and no other rows in between them should have any security devices.\n- By counting the number of security devices in each row, the code identifies potential beam pairs between consecutive rows. The number of beams between each pair is given by the product of the number of security devices in the current row and the previous row. This is because each security device in the current row can potentially form a beam with each security device in the previous row.\n- The "prev" variable is used to keep track of the number of security devices in the previous row, allowing the code to calculate the beams for each consecutive pair of rows.\n\nOverall, this approach efficiently calculates the total number of laser beams in the bank by iterating through the rows only once and counting the security devices in each row. The time complexity of this solution is O(m * n), where "m" is the number of rows and "n" is the number of columns in the bank matrix. The space complexity is O(1) since it uses a constant amount of additional space regardless of the input size.\n\n# Complexity\n- Time complexity: $$O(m * n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int count = 0;\n        int prev = 0;\n\n        for (String floor : bank) {\n            int ones = countOnes(floor);\n            if (ones != 0) {\n                count += ones * prev;\n                prev = ones;\n            }\n        }\n\n        return count;\n    }\n\n    private int countOnes(String floor) {\n        int ones = 0;\n        for (char c : floor.toCharArray()) {\n            if (c == \'1\') {\n                ones++;\n            }\n        }\n        return ones;\n    }\n}\n```
['Math', 'String', 'Matrix', 'Java', 'C#']