✅☑Beats 99.59% || [C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n---\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(Brute Approach)\n\n1. Collecting Counts:\n    - The 
# PLEASE UPVOTE IF IT HELPED\n\n---\n![Screenshot 2024-01-03 054901.png](https://assets.leetcode.com/users/images/24148bba-b996-4252-8046-d528f0592cd5_1704241629.7946181.png)\n\n---\n\n# Approaches\n(Also explained in the code)\n\n#### ***Approach 1(Brute Approach)***\n\n1. **Collecting Counts:**\n    - The code first collects the counts of \'1\'s in each string of the `bank` vector and stores them in the `v` vector. It specifically considers strings that have a count of \'1\'s not equal to zero, discarding strings with no \'1\'s.\n1. **Calculating Consecutive Products:**\n    - By calculating the products of consecutive counts of \'1\'s in the `v` vector, the code might be attempting to capture a relationship or measure a pattern between consecutive counts of \'1\'s in the strings of the `bank` vector.\n\n\n# Complexity\n- Time complexity:\n   $$O(n*m)$$\n    \n\n- Space complexity:\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        int ans = 0;\n\n        vector<int>v;\n        for(auto a: bank){\n            string s = a;\n            int cnt=0;\n            for(int i =0;i<s.size();i++){\n                if(s[i]==\'1\')cnt++;\n            }\n            if(cnt!=0){\n                v.push_back(cnt);\n            }\n            \n        }\n\n        for(int i=1;i<v.size();i++){\n            int t = v[i]*v[i-1];\n            ans += t; \n        }\n        \n        return ans;\n    }\n};\n\n\n\n\n```\n\n\n```Java []\nimport java.util.List;\n\nclass Solution {\n    public int numberOfBeams(List<String> bank) {\n        int ans = 0, temp = 0;\n        for (String str : bank) {\n            int n = str.replaceAll("[^1]", "").length();\n            if (n == 0) continue;\n            ans += temp * n;\n            temp = n;\n        }\n        return ans;\n    }\n}\n\n\n\n\n\n```\n\n```python3 []\nclass Solution:\n    def numberOfBeams(self, bank):\n        ans, temp = 0, 0\n        for s in bank:\n            n = s.count(\'1\')\n            if n == 0:\n                continue\n            ans += temp * n\n            temp = n\n        return ans\n\n\n\n```\n```javascript []\nclass Solution {\n    numberOfBeams(bank) {\n        let ans = 0, temp = 0;\n        for (let i = 0; i < bank.length; i++) {\n            let n = (bank[i].match(/1/g) || []).length;\n            if (n === 0) continue;\n            ans += temp * n;\n            temp = n;\n        }\n        return ans;\n    }\n}\n\n\n\n\n```\n---\n\n\n\n#### ***Approach 2(Greedy)***\n1. **Variable Initialization:**\n\n    - `ans` stores the final result.\n    - `temp` tracks the previous count of \'1\'s encountered in the strings.\n1. Iteration through Strings:\n\n    - Loop through each string in tuhe `bank` vector.\n1. **Counting \'1\'s:**\n\n- Count the occurrences of \'1\' in each string using `count` and store the count in `n`.\n1. **Calculation based on Counts:**\n\n    - If `n` (the count of \'1\'s) is zero, move to the next string.\n    - If `n` is not zero:\n        - Multiply the previous count (`temp`) by the current count (n) and add it to `ans`.\n        - Update `temp` with the current count `n` for the next iteration.\n1. **Return the Final Result:**\n\n    - After iterating through all strings, return the accumulated value stored in `ans`.\n\n\n\n# Complexity\n- Time complexity:\n   $$O(n*m)$$\n    \n\n- Space complexity:\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        int ans = 0, temp = 0;\n        for (int i = 0; i < bank.size(); i++) {\n            int n = count(bank[i].begin(), bank[i].end(), \'1\');\n            if (n == 0) continue;\n            ans += temp * n;;\n            temp = n;\n        }\n        return ans;\n    }\n};\n\n\n```\n\n\n```Java []\n\n\nclass Solution {\n    public int numberOfBeams(List<String> bank) {\n        int ans = 0, temp = 0;\n        for (String str : bank) {\n            int n = str.replaceAll("[^1]", "").length();\n            if (n == 0) continue;\n            ans += temp * n;\n            temp = n;\n        }\n        return ans;\n    }\n}\n\n\n```\n\n```python3 []\nclass Solution:\n    def numberOfBeams(self, bank):\n        ans, temp = 0, 0\n        for s in bank:\n            n = s.count(\'1\')\n            if n == 0:\n                continue\n            ans += temp * n\n            temp = n\n        return ans\n\n\n\n```\n```javascript []\nclass Solution {\n    numberOfBeams(bank) {\n        let ans = 0, temp = 0;\n        for (let i = 0; i < bank.length; i++) {\n            let n = (bank[i].match(/1/g) || []).length;\n            if (n === 0) continue;\n            ans += temp * n;\n            temp = n;\n        }\n        return ans;\n    }\n}\n\n\n\n```\n---\n\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Array', 'Math', 'String', 'Matrix', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']