🚀🚀🚀 BEATS 100% CODERS 🚀🚀🚀 by PRODONiK (Java, C++, Python, C#, Ruby, GO)
\n\n# Intuition\nThe problem involves counting the number of beams in a bank represented as a 2D array of strings. A beam is defined as a continuous sequence of
![image.png](https://assets.leetcode.com/users/images/226cc31b-e7f4-4a2e-b810-691724339eb7_1704272696.4566343.png)\n\n# Intuition\nThe problem involves counting the number of beams in a bank represented as a 2D array of strings. A beam is defined as a continuous sequence of \'1\'s in a row. The intuition is to iterate through each row, count the number of \'1\'s in each row, and accumulate the result by multiplying the count of \'1\'s in the current row with the count of \'1\'s in the next row where \'1\'s are present.\n\n# Approach\nThe approach involves iterating through each row of the bank, counting the number of \'1\'s in each row using the `countOfOnes` function. The count of \'1\'s in the current row is multiplied by the count of \'1\'s in the next row where \'1\'s are present, and the result is accumulated. The function returns the total count of beams in the bank.\n\n# Complexity\n- Time complexity: $$O(m \\cdot n)$$, where $$m$$ is the number of rows and $$n$$ is the number of columns in the bank. The function iterates through each row and counts the \'1\'s.\n- Space complexity: $$O(1)$$, as the algorithm uses a constant amount of additional space.\n\nThe time complexity is influenced by the iteration through each row, and the space complexity is constant since the algorithm does not use any additional data structures.\n\n\n# Code\n```Java []\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int current = countOfOnes(bank[0], 0), next = 0, result = 0;\n\n        for (int i = 1; i < bank.length; i ++) {\n            next = countOfOnes(bank[i], 0);\n            if (next != 0) {\n                result += current * next;\n                current = next;\n            }\n        }\n        return result;\n    }\n    private int countOfOnes (String row, int result) {\n        for (int i = 0; i < row.length(); i ++) {\n            result += row.charAt(i) - \'0\';\n        }\n        return result;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int numberOfBeams(std::vector<std::string>& bank) {\n        int current = countOfOnes(bank[0], 0), next = 0, result = 0;\n\n        for (int i = 1; i < bank.size(); i++) {\n            next = countOfOnes(bank[i], 0);\n            if (next != 0) {\n                result += current * next;\n                current = next;\n            }\n        }\n        return result;\n    }\n\nprivate:\n    int countOfOnes(const std::string& row, int result) {\n        for (char ch : row) {\n            result += ch - \'0\';\n        }\n        return result;\n    }\n};\n```\n```Python []\nclass Solution:\n    def numberOfBeams(self, bank):\n        current = self.count_of_ones(bank[0], 0)\n        next_count = 0\n        result = 0\n\n        for i in range(1, len(bank)):\n            next_count = self.count_of_ones(bank[i], 0)\n            if next_count != 0:\n                result += current * next_count\n                current = next_count\n\n        return result\n\n    def count_of_ones(self, row, result):\n        for ch in row:\n            result += int(ch)\n        return result\n```\n```Ruby []\nfunc numberOfBeams(bank []string) int {\n    current := countOfOnes(bank[0], 0)\n    var nextCount, result int\n\n    for i := 1; i < len(bank); i++ {\n        nextCount = countOfOnes(bank[i], 0)\n        if nextCount != 0 {\n            result += current * nextCount\n            current = nextCount\n        }\n    }\n\n    return result\n}\n\nfunc countOfOnes(row string, result int) int {\n    for _, ch := range row {\n        result += int(ch - \'0\')\n    }\n    return result\n}\n\nfunc main() {\n    bank := []string{"110", "001", "010"}\n    result := numberOfBeams(bank)\n    fmt.Println(result)\n}\n```\n```Go []\nclass Solution\n  def number_of_beams(bank)\n    current = count_of_ones(bank[0], 0)\n    next_count = 0\n    result = 0\n\n    (1...bank.length).each do |i|\n      next_count = count_of_ones(bank[i], 0)\n      result += current * next_count if next_count != 0\n      current = next_count\n    end\n\n    result\n  end\n\n  private\n\n  def count_of_ones(row, result)\n    row.each_char { |ch| result += ch.to_i }\n    result\n  end\nend\n\n```\n```C# []\npublic class Solution\n{\n    public int NumberOfBeams(string[] bank)\n    {\n        int current = CountOfOnes(bank[0], 0);\n        int nextCount = 0;\n        int result = 0;\n\n        for (int i = 1; i < bank.Length; i++)\n        {\n            nextCount = CountOfOnes(bank[i], 0);\n            if (nextCount != 0)\n            {\n                result += current * nextCount;\n                current = nextCount;\n            }\n        }\n\n        return result;\n    }\n\n    private int CountOfOnes(string row, int result)\n    {\n        foreach (char ch in row)\n        {\n            result += ch - \'0\';\n        }\n        return result;\n    }\n}\n```\n![image.png](https://assets.leetcode.com/users/images/2e9ddb8e-f389-4bb8-a2d2-ed2861e9ad06_1704274020.4845214.png)\n
['C++', 'Java', 'Go', 'Python3', 'Ruby', 'C#']