question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
number-of-substrings-with-only-1s | Java 1 loop O(n) | java-1-loop-on-by-hobiter-z2vw | \n public int numSub(String s) {\n int res = 0, cnt = 0, mod = 1_000_000_007;\n for (char c : s.toCharArray()) {\n if (c == \'1\') r | hobiter | NORMAL | 2020-07-12T04:01:17.340759+00:00 | 2020-07-12T05:57:57.071641+00:00 | 1,791 | false | ```\n public int numSub(String s) {\n int res = 0, cnt = 0, mod = 1_000_000_007;\n for (char c : s.toCharArray()) {\n if (c == \'1\') res = (res + ++cnt) % mod; // added cnt of subarrays ended with c;\n else cnt = 0;\n }\n return res;\n }\n``` | 18 | 2 | [] | 4 |
number-of-substrings-with-only-1s | [Java/Python 3] Count continuous '1's O(n) ONE liner codes w/ brief explanation and analysis. | javapython-3-count-continuous-1s-on-one-accch | For k continuous 1s, we have 1, 2, ..., k substrings ending at 1st, 2nd, ..., kth 1s, respectively; Therefore, there are k * (k + 1) / 2 substrings for any k c | rock | NORMAL | 2020-07-12T04:26:12.216352+00:00 | 2020-07-13T06:29:59.677250+00:00 | 829 | false | For `k` continuous `1`s, we have `1, 2, ..., k` substrings ending at `1st, 2nd, ..., kth 1`s, respectively; Therefore, there are `k * (k + 1) / 2` substrings for any `k` continuous `1`.\n```java\n public int numSub(String s) {\n int sum = 0;\n for (int i = 0, cnt = 0; i < s.length(); ++i) {\n ... | 9 | 0 | [] | 3 |
number-of-substrings-with-only-1s | [Python] Sum of Arithmetic Progression with explanation **100.00% Faster** | python-sum-of-arithmetic-progression-wit-m2mf | There is a pattern, you will find that the result is the sum of arithmetic progression.\nArithmetic progression: t(n) = a + d(n-1)\nSum of arithmetic progressio | jasper-w | NORMAL | 2020-07-12T04:25:43.226689+00:00 | 2020-07-12T13:31:01.648360+00:00 | 1,135 | false | There is a ***pattern***, you will find that the ***result is the sum of arithmetic progression***.\nArithmetic progression: **t(n) = a + d(n-1)**\nSum of arithmetic progression: **s(n) = (n / 2) * (2a + d(n-1))**\n\nIn this problem, the first term(a) will be the the length of "1" and the number of terms will also be t... | 8 | 0 | ['Python', 'Python3'] | 2 |
number-of-substrings-with-only-1s | [C++] Easy math solution | c-easy-math-solution-by-pankajgupta20-55b1 | \tclass Solution {\n\tpublic:\n\t\tint numSub(string s) {\n\t\t\tlong count = 0;\n\t\t\tlong ans = 0;\n\t\t\tfor(int i = 0; i < s.size(); i++){\n\t\t\t\tif(s[i | pankajgupta20 | NORMAL | 2021-05-13T18:36:03.078935+00:00 | 2021-05-13T18:36:03.078983+00:00 | 814 | false | \tclass Solution {\n\tpublic:\n\t\tint numSub(string s) {\n\t\t\tlong count = 0;\n\t\t\tlong ans = 0;\n\t\t\tfor(int i = 0; i < s.size(); i++){\n\t\t\t\tif(s[i] == \'1\'){\n\t\t\t\t\tcount++;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tans += (count * (count + 1)) / 2;\n\t\t\t\t\tcount = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ... | 6 | 0 | ['Math', 'C', 'C++'] | 0 |
number-of-substrings-with-only-1s | Java, counting, linear time, constant space, explained | java-counting-linear-time-constant-space-4fm5 | Algorithm ideas:\neach next consequent 1 contributes to all unique strings with length 1 to k (where k is number of subsequent 1s), k substrings overall. In oth | gthor10 | NORMAL | 2020-08-11T23:31:56.779214+00:00 | 2020-08-11T23:31:56.779261+00:00 | 739 | false | Algorithm ideas:\neach next consequent 1 contributes to all unique strings with length 1 to k (where k is number of subsequent 1s), k substrings overall. In other words count of possible substrings increased by k\neach 0 should reset the count of 1s\n\nO(n) time - scan every character in S once\nO(1) space - no extra s... | 6 | 0 | ['Counting', 'Java'] | 1 |
number-of-substrings-with-only-1s | c++, O(n) full beginner friendly explanation! | c-on-full-beginner-friendly-explanation-irsae | Please upvote, if you find the solution helpful !\n\nSolution : \nLogic : We count the number of total number of 1\'s in every substring which contain only 1\'s | h98dubey | NORMAL | 2021-06-29T12:34:34.070487+00:00 | 2021-06-29T12:41:17.821595+00:00 | 258 | false | **Please upvote, if you find the solution helpful !**\n\nSolution : \nLogic : We count the number of total number of 1\'s in every substring which contain only 1\'s.\n\n **Wait, Let me explain!**\n Let\'s take an example : suppose our string is **conscOnes** = "111" i.e. comprising of all 1\'s. \n\nNow, \n**How to calc... | 5 | 0 | [] | 2 |
number-of-substrings-with-only-1s | Clean Python 3, counting ones with math O(N) | clean-python-3-counting-ones-with-math-o-owmd | Time: O(N)\nSpace: O(N)\n\nclass Solution:\n def numSub(self, s: str) -> int:\n mod = 10 ** 9 + 7\n seq = (len(ones) for ones in s.split(\'0\') | lenchen1112 | NORMAL | 2020-07-12T04:02:43.348649+00:00 | 2020-07-12T04:04:36.032723+00:00 | 390 | false | Time: `O(N)`\nSpace: `O(N)`\n```\nclass Solution:\n def numSub(self, s: str) -> int:\n mod = 10 ** 9 + 7\n seq = (len(ones) for ones in s.split(\'0\') if ones)\n return sum((ones * (ones + 1)) // 2 for ones in seq) % mod\n``` | 5 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++ O(n) | Find the pattern | c-on-find-the-pattern-by-nitro-fish-xlc7 | \nclass Solution {\npublic:\n int numSub(string s) \n {\n int res=0;\n int i=0;\n while(i<s.size())\n {\n if(s[i]== | nitro-fish | NORMAL | 2020-07-12T04:02:06.519450+00:00 | 2020-07-12T05:35:57.150067+00:00 | 622 | false | ```\nclass Solution {\npublic:\n int numSub(string s) \n {\n int res=0;\n int i=0;\n while(i<s.size())\n {\n if(s[i]==\'1\')\n {\n long long count=0;\n while(s[i]==\'1\')\n {\n count++;\n ... | 5 | 0 | ['Math', 'C', 'Sliding Window'] | 1 |
number-of-substrings-with-only-1s | TC=O(n), SC=O(1) approach | tcon-sco1-approach-by-anishdhomase-ft9s | Intuition\nTraverse the string and find strings of all ones and by using the formula len*(len+1)/2 find number of strings in which all 1\'s are present and keep | AnishDhomase | NORMAL | 2024-03-13T15:18:52.483467+00:00 | 2024-03-13T15:18:52.483509+00:00 | 53 | false | # Intuition\nTraverse the string and find strings of all ones and by using the formula len*(len+1)/2 find number of strings in which all 1\'s are present and keep adding this answer to our final answer\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n ... | 4 | 0 | ['C++'] | 0 |
number-of-substrings-with-only-1s | ONE-LINER || FASTER THAN 98% || SC 90% || | one-liner-faster-than-98-sc-90-by-vatsal-3eby | \n return sum(map(lambda x: ((1+len(x))*len(x)//2)%(10**9+7), s.split(\'0\')))\n\nIF THIS HELP U KINDLY UPVOTE THIS TO HELP OTHERS TO GET THIS SOLUTION\n | vatsalg2002 | NORMAL | 2022-07-20T09:02:20.869679+00:00 | 2022-07-20T09:02:20.869734+00:00 | 494 | false | ```\n return sum(map(lambda x: ((1+len(x))*len(x)//2)%(10**9+7), s.split(\'0\')))\n```\nIF THIS HELP U KINDLY UPVOTE THIS TO HELP OTHERS TO GET THIS SOLUTION\nIF U DONT GET IT KINDLY COMMENT AND FEEL FREE TO ASK\nAND CORRECT MEIF I AM WRONG | 4 | 0 | ['Python', 'Python3'] | 3 |
number-of-substrings-with-only-1s | Easy Java Solution TC:O(n) SC:O(1) | easy-java-solution-tcon-sco1-by-vdb21-x9zv | \n public int numSub(String s) {\n int prev=0;\n int ans=0;\n \n for(int i=0;i<s.length();i++){\n \n if(s.cha | vdb21 | NORMAL | 2020-12-07T23:36:33.950017+00:00 | 2020-12-07T23:40:46.823112+00:00 | 324 | false | ```\n public int numSub(String s) {\n int prev=0;\n int ans=0;\n \n for(int i=0;i<s.length();i++){\n \n if(s.charAt(i)==\'1\'){\n\t\t\t\t// If current element =1\n prev=prev+1;\n }else{\n\t\t\t\t// If current element =0, reset prev=0\n ... | 4 | 0 | ['Java'] | 1 |
number-of-substrings-with-only-1s | [faster than 94%][2 approach][cpp][easy understanding] | faster-than-942-approachcppeasy-understa-fvdm | \n//1.\nclass Solution {\npublic:\n int numSub(string s) {\n int num=0;\n long long count=0;\n for(int i=0;i<s.size();i++){\n | rajat_gupta_ | NORMAL | 2020-09-16T15:02:53.154314+00:00 | 2020-09-16T15:02:53.154360+00:00 | 238 | false | ```\n//1.\nclass Solution {\npublic:\n int numSub(string s) {\n int num=0;\n long long count=0;\n for(int i=0;i<s.size();i++){\n if(s[i]==\'1\')\n count++;\n else{\n\t\t\t // number of all substrings till n = n * (n+1) / 2 \n count=((count*(... | 4 | 0 | ['C', 'C++'] | 1 |
number-of-substrings-with-only-1s | Just a clever sliding window | just-a-clever-sliding-window-by-slowbutf-yiu0 | \nclass Solution {\npublic:\n int numSub(string s) {\n const int m=1000000007;\n int n=s.length();\n int res=0;\n int i=0,j=0;\n | slowbutfast | NORMAL | 2020-07-12T07:39:33.655665+00:00 | 2020-07-12T07:39:33.655716+00:00 | 332 | false | ```\nclass Solution {\npublic:\n int numSub(string s) {\n const int m=1000000007;\n int n=s.length();\n int res=0;\n int i=0,j=0;\n while(j<s.length()){\n if(s[j]==\'1\'){\n while(i<j and s[i]!=\'1\')\n i++;\n res=(res%m+(... | 4 | 1 | [] | 3 |
number-of-substrings-with-only-1s | JAVA | Shortest solution | java-shortest-solution-by-sourin_bruh-hcdr | Please Upvote !!! (\u25E0\u203F\u25E0)\n\nclass Solution {\n public int numSub(String s) {\n int count = 0, total = 0, mod = 1_000_000_007;\n \ | sourin_bruh | NORMAL | 2022-09-25T13:44:51.781065+00:00 | 2022-09-25T13:44:51.781101+00:00 | 883 | false | ### *Please Upvote !!!* **(\u25E0\u203F\u25E0)**\n```\nclass Solution {\n public int numSub(String s) {\n int count = 0, total = 0, mod = 1_000_000_007;\n \n for (char c : s.toCharArray()) {\n count = (c == \'1\') ? count + 1 : 0;\n total = (total + count) % mod;\n }... | 3 | 0 | ['String', 'Java'] | 0 |
number-of-substrings-with-only-1s | Java Solution Checking for consecutive One | java-solution-checking-for-consecutive-o-5y2z | Here , I just counted the number of consecutive 1, and updated a counter varibale whenever we encountered with a 1. This varibale actually keep track of consecu | vrohith | NORMAL | 2020-09-05T04:05:07.868121+00:00 | 2020-09-05T04:05:07.868166+00:00 | 357 | false | Here , I just counted the number of consecutive 1, and updated a counter varibale whenever we encountered with a 1. This varibale actually keep track of consecutive 1.\n\nNow we add this count with our result variable. The moment we see a 0, we reset the counter variable back to 0 and again start counting when we see t... | 3 | 0 | ['Java'] | 0 |
number-of-substrings-with-only-1s | JavaScript 1-line solution | javascript-1-line-solution-by-sporkyy-0txv | Runtime: 80 ms, faster than 100.00% of JavaScript online submissions\n_Memory Usage: 37.7 MB, less than 100.00% of JavaScript online submissions_\n\njavascript\ | sporkyy | NORMAL | 2020-07-13T20:48:29.323167+00:00 | 2020-07-13T20:49:04.600797+00:00 | 236 | false | _Runtime: 80 ms, faster than 100.00% of JavaScript online submissions_\n_Memory Usage: 37.7 MB, less than 100.00% of JavaScript online submissions_\n\n```javascript\nconst numSub = s =>\n s\n .split(\'0\')\n .reduce((cnt, { length: len }) => cnt + (len * (len + 1)) / 2, 0) %\n (10 ** 9 + 7);\n``` | 3 | 1 | ['JavaScript'] | 0 |
number-of-substrings-with-only-1s | C++ | O(n) | c-on-by-ashwinfury1-ne8y | Apporach: \narray = [0 1 1 0 1 1 1] , lengths of subarray are 2 and 3\nfor n -> (n* n+1 )/2\n2 -> 3\n3 -> 6\n6+3 = 9\n\nclass Solution {\npublic:\n int numSu | ashwinfury1 | NORMAL | 2020-07-12T04:19:27.899413+00:00 | 2020-07-12T04:19:27.899445+00:00 | 269 | false | Apporach: \n``array = [0 1 1 0 1 1 1]`` , lengths of subarray are 2 and 3\nfor n -> (n* n+1 )/2\n2 -> 3\n3 -> 6\n6+3 = 9\n```\nclass Solution {\npublic:\n int numSub(string s) {\n long long ans = 0;\n int mp = 1000000007;\n int n = s.length();\n long long count = 0,i=0;\n while(i<n... | 3 | 0 | ['C++'] | 0 |
number-of-substrings-with-only-1s | [Python3] count continuous "1" | python3-count-continuous-1-by-ye15-rrmo | Algo\nCount number of continuous 1 and use formula n*(n+1)//2 to compute the answer. \n\n\nclass Solution:\n def numSub(self, s: str) -> int:\n ans = | ye15 | NORMAL | 2020-07-12T04:05:17.970857+00:00 | 2020-07-12T04:05:17.970902+00:00 | 258 | false | Algo\nCount number of continuous `1` and use formula `n*(n+1)//2` to compute the answer. \n\n```\nclass Solution:\n def numSub(self, s: str) -> int:\n ans = n = 0\n for c in s:\n if c == "0": \n ans = (ans + n*(n+1)//2) % 1_000_000_007\n n = 0\n else:... | 3 | 0 | ['Python3'] | 0 |
number-of-substrings-with-only-1s | ✅ [Javascript / Python3 / C++] ⭐️ Sum of the Series | javascript-python3-c-sum-of-the-series-b-ob7o | Synopsis:\n\nAccumulate the sum of the series for each run of consecutive 1s length.\n\n---\n\nContest 197 - Screenshare: https://www.youtube.com/watch?v=-oXf4I | claytonjwong | NORMAL | 2020-07-12T04:02:54.236807+00:00 | 2020-08-11T00:42:26.875129+00:00 | 173 | false | **Synopsis:**\n\nAccumulate the sum of the series for each `run` of consecutive 1s length.\n\n---\n\n**Contest 197 - Screenshare:** https://www.youtube.com/watch?v=-oXf4Ikou_c&feature=youtu.be\n\n---\n\n**Solutions:**\n\n*Javascript*\n```\nlet numSub = s => s.split(\'0\').map(run => run.length).reduce((sum, n) => (sum ... | 3 | 0 | [] | 2 |
number-of-substrings-with-only-1s | sliding window c++ O(n) | sliding-window-c-on-by-jaychadawar-2ug6 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | JayChadawar | NORMAL | 2024-12-18T05:35:35.311916+00:00 | 2024-12-18T05:35:35.311916+00:00 | 135 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['Sliding Window', 'C++'] | 0 |
number-of-substrings-with-only-1s | Easy Python solution | easy-python-solution-by-chandan_giri-mxp8 | Intuition\n Describe your first thoughts on how to solve this problem. \nCount the number of 1 in each substring of 1s.\nApply (N(N+1))//2 after the end of each | Chandan_Giri | NORMAL | 2024-05-22T11:35:13.811973+00:00 | 2024-05-22T11:35:13.812003+00:00 | 85 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCount the number of 1 in each substring of 1s.\nApply (N(N+1))//2 after the end of each 1s substring.\nExample:\ns = "0110111"\nHere we have 2 substring of 1 in this example. \n1st has 2 ones and 2nd has 3 ones.\nso ans = (2*(2+1))//2 +... | 2 | 0 | ['Python3'] | 0 |
number-of-substrings-with-only-1s | O (1) Space | Intuitive explanation | C++, Python, Java | o-1-space-intuitive-explanation-c-python-n1pt | Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe solution works by keeping track of consecutive \'1\' sequences and calculating th | 1227hariharan | NORMAL | 2024-01-19T12:35:28.302675+00:00 | 2024-01-19T12:35:28.302706+00:00 | 532 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe solution works by keeping track of consecutive \'**1**\' sequences and calculating the count of valid substrings for each sequence. The formula c*(c+1)/2 calculates the sum of an arithmetic series for each sequence. This is based on... | 2 | 0 | ['C++', 'Java', 'Python3'] | 0 |
number-of-substrings-with-only-1s | Simple easy solution, 2 methods ✅✅ | simple-easy-solution-2-methods-by-vaibha-dbbg | \n\n# Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n | vaibhav2112 | NORMAL | 2023-11-09T13:15:41.490013+00:00 | 2023-11-09T13:15:41.490062+00:00 | 581 | false | \n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int numSub(string s) {\n // int i = 0, n = s.length(), ans = 0 , mod = 1e9 + 7;\n ... | 2 | 0 | ['C++'] | 0 |
number-of-substrings-with-only-1s | TypeScript - Math | typescript-math-by-amal-ps-xt0q | Code\n\nfunction numSub(s) {\n let count = 0;\n let permuSum = 0;\n for (let i = 0; i < s.length; i++) {\n if (s[i] == \'1\') {\n cou | amal-ps | NORMAL | 2023-08-16T10:54:40.627428+00:00 | 2023-08-16T10:54:40.627453+00:00 | 6 | false | # Code\n```\nfunction numSub(s) {\n let count = 0;\n let permuSum = 0;\n for (let i = 0; i < s.length; i++) {\n if (s[i] == \'1\') {\n count = count + 1;\n } else {\n permuSum = (permuSum + permutation(count)) % (10 ** 9 + 7);\n count = 0;\n }\n }\n p... | 2 | 0 | ['Math', 'TypeScript'] | 0 |
number-of-substrings-with-only-1s | Beats 90% CPP SOL ✅✅ | beats-90-cpp-sol-by-zenitsu02-xchn | Pls Upvote if you found it Helpful \uD83D\uDE07\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\npublic:\n i | Zenitsu02 | NORMAL | 2023-07-26T14:25:18.544795+00:00 | 2023-07-26T14:25:18.544827+00:00 | 219 | false | # **Pls Upvote if you found it Helpful \uD83D\uDE07**\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution {\npublic:\n int mod = 1e9 + 7;\n int numSub(string s) {\n\n int front = 0;\n int back = 0;\n int ans = 0;\n\n while(front < s.size())... | 2 | 0 | ['Sliding Window', 'Iterator', 'C++'] | 0 |
number-of-substrings-with-only-1s | EASY TO UNDERSTAND C++ SOLUTION | easy-to-understand-c-solution-by-abhay_1-udti | \nclass Solution {\npublic:\n int numSub(string s) {\n long long int ans = 0, i = 0, j= 0, n = s.length(),mod = 1e9+7;\n while(i<n){\n | abhay_12345 | NORMAL | 2022-11-19T07:21:19.154328+00:00 | 2022-11-19T07:21:19.154366+00:00 | 420 | false | ```\nclass Solution {\npublic:\n int numSub(string s) {\n long long int ans = 0, i = 0, j= 0, n = s.length(),mod = 1e9+7;\n while(i<n){\n j = i;\n while(j<n && s[i] == s[j]){\n j++;\n if(s[i]==\'1\'){\n ans = (ans+j-i)%mod;\n ... | 2 | 0 | ['Math', 'C', 'C++'] | 0 |
number-of-substrings-with-only-1s | Sum of N numbers, Python | sum-of-n-numbers-python-by-antarab-wmar | Basically, the no. of consecutions \'1\'s can form n*(n+1)//2 substrings\n11 -> 2 1s, 1 2s = 2+1\n111 -> 3 1s, 2 2s, 1 3s = 3+2+1\n1111 -> 4 1s, 3 2s, 2 3s, 1 4 | antarab | NORMAL | 2022-10-26T22:32:08.799661+00:00 | 2022-10-26T22:32:08.799700+00:00 | 357 | false | Basically, the no. of consecutions \'1\'s can form n*(n+1)//2 substrings\n11 -> 2 1s, 1 2s = 2+1\n111 -> 3 1s, 2 2s, 1 3s = 3+2+1\n1111 -> 4 1s, 3 2s, 2 3s, 1 4s = 4+3+2+1\n\n```\n def numSub(self, s: str) -> int:\n c=0\n ans=[]\n for x in s:\n if x==\'1\':\n c=c+1\n ... | 2 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Go 2ms beats 100% | go-2ms-beats-100-by-tuanbieber-7x1g | \nfunc numSub(s string) int {\n\tif len(s) == 0 {\n\t\treturn 0\n\t}\n\n\tif len(s) == 1 {\n\t\tif s[0] == 49 {\n\t\t\treturn 1\n\t\t}\n\n\t\treturn 0\n\t}\n\n\ | tuanbieber | NORMAL | 2022-06-22T03:03:08.200831+00:00 | 2022-06-22T03:03:08.200874+00:00 | 161 | false | ```\nfunc numSub(s string) int {\n\tif len(s) == 0 {\n\t\treturn 0\n\t}\n\n\tif len(s) == 1 {\n\t\tif s[0] == 49 {\n\t\t\treturn 1\n\t\t}\n\n\t\treturn 0\n\t}\n\n\tleft := 0\n\n for left < len(s) && s[left] != 49 {\n\t\tleft++\n\t}\n\n\tvar res int\n\n\tfor i := left; i < len(s); i++ {\n\t\tif s[i] == s[left] {\n\t\... | 2 | 0 | ['Go'] | 1 |
number-of-substrings-with-only-1s | C++ Simple Solution | c-simple-solution-by-chirags_30-1jdy | \nclass Solution {\npublic:\n int numSub(string s) \n {\n int ans=0, count=0;\n for(auto c:s)\n {\n if(c == \'0\')\n | chirags_30 | NORMAL | 2021-05-30T11:29:11.570499+00:00 | 2021-05-30T11:29:11.570541+00:00 | 101 | false | ```\nclass Solution {\npublic:\n int numSub(string s) \n {\n int ans=0, count=0;\n for(auto c:s)\n {\n if(c == \'0\')\n ans=0;\n \n else\n {\n ans++;\n count = (count+ans)%(1000000007);\n }\n ... | 2 | 2 | ['Math', 'C', 'C++'] | 0 |
number-of-substrings-with-only-1s | [JavaScript] easy to understand solution (61%,83%) | javascript-easy-to-understand-solution-6-k537 | Runtime: 88 ms, faster than 61.70% of JavaScript online submissions for Number of Substrings With Only 1s.\nMemory Usage: 40.2 MB, less than 87.23% of JavaScrip | 0533806 | NORMAL | 2021-05-23T09:59:40.310074+00:00 | 2021-05-23T10:08:16.739152+00:00 | 152 | false | Runtime: 88 ms, faster than 61.70% of JavaScript online submissions for Number of Substrings With Only 1s.\nMemory Usage: 40.2 MB, less than 87.23% of JavaScript online submissions for Number of Substrings With Only 1s.\n```\nvar numSub = function(s) {\n let res = 0;\n let one = 0;\n for(let num of s){\n ... | 2 | 0 | ['JavaScript'] | 0 |
number-of-substrings-with-only-1s | Python solution one-pass | python-solution-one-pass-by-flyingspa-jd5x | class Solution:\n\n def numSub(self, s: str) -> int:\n ans = cum = 0\n for x in s:\n if x == "1":\n cum+=1\n | flyingspa | NORMAL | 2021-03-26T10:50:58.049651+00:00 | 2021-03-26T10:50:58.049677+00:00 | 126 | false | class Solution:\n\n def numSub(self, s: str) -> int:\n ans = cum = 0\n for x in s:\n if x == "1":\n cum+=1\n ans+=cum\n else:\n cum = 0\n return ans%(10**9+7) | 2 | 0 | ['Python'] | 0 |
number-of-substrings-with-only-1s | Python One Liner | python-one-liner-by-ragav_1302-l65h | \nclass Solution:\n def numSub(self, s: str) -> int:\n return sum(map(lambda i:(len(i)*(len(i)+1))//2,re.findall("[1]+",s)))%(10**9+7)\n | ragav_1302 | NORMAL | 2020-11-16T10:13:45.017237+00:00 | 2020-11-16T10:13:45.017271+00:00 | 108 | false | ```\nclass Solution:\n def numSub(self, s: str) -> int:\n return sum(map(lambda i:(len(i)*(len(i)+1))//2,re.findall("[1]+",s)))%(10**9+7)\n``` | 2 | 1 | [] | 0 |
number-of-substrings-with-only-1s | CPP ⭐ 8ms ⭐ Reason for using mod... | cpp-8ms-reason-for-using-mod-by-daranip-pavi | %(mod) 1000000007 is used because it prevents overflow and enables the problem setter to frame his problem for bigger ranges and hence demand a clever algorithm | daranip | NORMAL | 2020-10-17T17:02:31.332020+00:00 | 2020-10-18T08:10:04.227697+00:00 | 134 | false | **%(mod) 1000000007** is used because it prevents overflow and enables the problem setter to frame his problem for bigger ranges and hence demand a clever algorithm.\n\n```\nclass Solution {\npublic:\n long numSub(string s) {\n long i=0,temp = 0,total=0;\n while (i<s.length()){\n if (s[i]==\... | 2 | 1 | [] | 1 |
number-of-substrings-with-only-1s | 8ms faster than 80% submissions. | 8ms-faster-than-80-submissions-by-varnaa-f8lr | \nclass Solution {\n public int numSub(String s) {\n int count = 0;\n String[] ones = s.split("0");\n for(String one : ones){\n | varnaa | NORMAL | 2020-07-28T06:33:45.627660+00:00 | 2020-07-28T06:33:45.627694+00:00 | 102 | false | ```\nclass Solution {\n public int numSub(String s) {\n int count = 0;\n String[] ones = s.split("0");\n for(String one : ones){\n int len = one.length();\n if(len >= 1)\n count += (len*(len+1.0)/2) % 1000000007;\n }\n \n return count;\n ... | 2 | 1 | ['Java'] | 1 |
number-of-substrings-with-only-1s | C++ | Simple O(n) DP Solution | c-simple-on-dp-solution-by-draco7leetcod-389f | DP[i]=for every \'1\' encountered at ith position, the number of substrings that can be formed will be equal to the number of substrings formed at (i-1)th posit | draco7leetcode | NORMAL | 2020-07-14T18:07:41.500718+00:00 | 2020-07-14T18:13:15.786571+00:00 | 67 | false | DP[i]=for every \'1\' encountered at **ith position, the number of substrings that can be formed will be equal to the number of substrings formed at (i-1)th position +1.** \nAt last , the total number of substrings can be found by adding all the values of DP array.\nPS:- I know , this problem can be done without dp app... | 2 | 0 | ['Dynamic Programming', 'C'] | 0 |
number-of-substrings-with-only-1s | Easy count and sum javascript solution | easy-count-and-sum-javascript-solution-b-fy91 | \nvar numSub = function(s) {\n let result = 0;\n let len = 0; \n \n for (let i = 0; i < s.length; ++i) {\n if (s[i] === "1") {\n | eforce | NORMAL | 2020-07-12T07:33:39.358680+00:00 | 2020-07-12T07:33:39.358728+00:00 | 70 | false | ```\nvar numSub = function(s) {\n let result = 0;\n let len = 0; \n \n for (let i = 0; i < s.length; ++i) {\n if (s[i] === "1") {\n len++;\n result += len;\n } else {\n len = 0;\n }\n }\n \n return result % 1000000007;\n};\n``` | 2 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++| Step by Step Explanation | O(n) | c-step-by-step-explanation-on-by-sumanth-bd2p | Find length of each substring containing only 1\'s.\nOnce we find a substring of length l with only 1\'s no. of substrings possible is given by l(l+1)/2 (sum of | sumanthspark10 | NORMAL | 2020-07-12T06:23:56.568034+00:00 | 2020-08-01T06:20:51.066148+00:00 | 161 | false | Find length of each substring containing only 1\'s.\nOnce we find a substring of length l with only 1\'s no. of substrings possible is given by l*(l+1)/2 (sum of numbers from 1 to l)\nExample - 110111\nThere are two substrings containing only 1\'s..i.e., 11 of length 2 and 111 of length 3\nFor 1st substring "11" -\nNo.... | 2 | 1 | ['C', 'C++'] | 0 |
number-of-substrings-with-only-1s | C# Solution | c-solution-by-leonhard_euler-l4w7 | \npublic class Solution \n{\n public int NumSub(string s) \n {\n long prev = -1, result = 0, n = s.Length;;\n for(int i = 0; i <= n; i++)\n | Leonhard_Euler | NORMAL | 2020-07-12T04:49:35.714127+00:00 | 2020-07-12T04:49:35.714160+00:00 | 67 | false | ```\npublic class Solution \n{\n public int NumSub(string s) \n {\n long prev = -1, result = 0, n = s.Length;;\n for(int i = 0; i <= n; i++)\n if(i == n || s[i] == \'0\')\n {\n long len = (i - prev - 1);\n result += (len * (len + 1)) / 2;\n ... | 2 | 0 | [] | 0 |
number-of-substrings-with-only-1s | [Python] Easy understand solution | python-easy-understand-solution-by-too-y-d1jg | \nclass Solution(object):\n def numSub(self, s):\n """\n :type s: str\n :rtype: int\n """\n res = 0\n arr = s.split | too-young-too-naive | NORMAL | 2020-07-12T04:11:14.343544+00:00 | 2020-07-12T04:11:14.343597+00:00 | 59 | false | ```\nclass Solution(object):\n def numSub(self, s):\n """\n :type s: str\n :rtype: int\n """\n res = 0\n arr = s.split("0")\n for i in arr:\n n = len(i)\n if n == 0:\n continue\n res += n*(n+1)/2\n res = res %... | 2 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Single-line solution, beats 100% | single-line-solution-beats-100-by-simole-w9ub | Code | simolekc | NORMAL | 2025-02-14T06:19:46.242261+00:00 | 2025-02-14T06:19:46.242261+00:00 | 27 | false |
# Code
```javascript []
/**
* @param {string} s
* @return {number}
*/
var numSub = function (s) {
const mod = 10 ** 9 + 7;
return (
s
.split("0")
.reduce(
(sum, str) =>
sum + (Boolean(str) ? ((str.length * (str.length + 1)) / 2) % mod : 0... | 1 | 0 | ['JavaScript'] | 0 |
number-of-substrings-with-only-1s | Java || Detailed Explanation | java-formula-for-substrings-by-ritabrata-zqqp | IntuitionFormula for number of substrings of a given string on length n, is n*(n+1)/2ApproachCalculate length of substrings containing only 1 and apply the form | Ritabrata_1080 | NORMAL | 2025-01-09T19:22:39.159223+00:00 | 2025-01-09T19:25:27.336938+00:00 | 100 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Formula for number of substrings of a given string on length n, is n*(n+1)/2
# Approach
<!-- Describe your approach to solving the problem. -->
Calculate length of substrings containing only 1 and apply the formula for finding the number o... | 1 | 0 | ['Java'] | 0 |
number-of-substrings-with-only-1s | JAVA O(N) solution | Count | String | Math | Good Readability | Beginners Friendly | java-on-solution-count-string-math-good-uub36 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | karan_s01 | NORMAL | 2024-08-26T10:08:50.015895+00:00 | 2024-08-26T10:08:50.015919+00:00 | 39 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity : O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Space complexity : O(1)\n<!-- Add your space complexity here, e.g. $... | 1 | 0 | ['Math', 'String', 'Java'] | 0 |
number-of-substrings-with-only-1s | Easy JS Solution with Tips to watch out for | easy-js-solution-with-tips-to-watch-out-a8lnt | Intuition\n Describe your first thoughts on how to solve this problem. \nSimply apply the formula to the no, of occurances on the group.\n# Approach\n Describe | giogokul13 | NORMAL | 2024-03-30T12:49:54.661003+00:00 | 2024-03-30T12:49:54.661061+00:00 | 16 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimply apply the formula to the no, of occurances on the group.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nArears to Watch out;\n* Loop untill the last + 1 element, as we need to calculate all the occurance of o... | 1 | 0 | ['JavaScript'] | 0 |
number-of-substrings-with-only-1s | Simple java code for beginners | simple-java-code-for-beginners-by-arobh-z3st | \n# Complexity\n- \n\n# Code\n\nclass Solution {\n public int numSub(String s) {\n int n=s.length();\n int count=0;\n int mod=1000000007 | Arobh | NORMAL | 2023-12-30T12:15:45.448736+00:00 | 2023-12-30T12:15:45.448814+00:00 | 77 | false | \n# Complexity\n- \n\n# Code\n```\nclass Solution {\n public int numSub(String s) {\n int n=s.length();\n int count=0;\n int mod=1000000007;\n int i=0,j=0;\n while(j<n){... | 1 | 0 | ['Java'] | 0 |
number-of-substrings-with-only-1s | Easy C++ solution in O(N) | easy-c-solution-in-on-by-sachin_kumar_sh-mk5z | Intuition\nJust keep the count of number of 1s in the given string.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nTraverse the gi | Sachin_Kumar_Sharma | NORMAL | 2023-12-09T07:28:22.254611+00:00 | 2023-12-09T07:28:22.254659+00:00 | 2 | false | # Intuition\nJust keep the count of number of 1s in the given string.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTraverse the given string and keep on incrementing the cnt when we encounter 1. \nAnd the update the ans by adding the cnt to it.\n<!-- Describe your approach to solv... | 1 | 0 | ['C++'] | 0 |
number-of-substrings-with-only-1s | C++ || Sliding Window || Easy to Understand | c-sliding-window-easy-to-understand-by-m-74zl | Intuition\n Describe your first thoughts on how to solve this problem. \n1. Initialization: Start and end are initialized to the beginning of the string. totalS | Mohd_Nasir | NORMAL | 2023-11-09T04:50:23.538171+00:00 | 2023-11-09T04:50:23.538193+00:00 | 366 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. **Initialization**: Start and end are initialized to the beginning of the string. `totalSubStrings` is the count of valid substrings, initialized to 0.\n2. **Traversal through the string**: The code iterates through each character of t... | 1 | 0 | ['Math', 'String', 'Sliding Window', 'C++'] | 0 |
number-of-substrings-with-only-1s | Very Easy to understand C++ || Easiest of all time | very-easy-to-understand-c-easiest-of-all-aq81 | \n\n# Code\n\nclass Solution {\npublic:\n const int mod=1e9+7;\n\n int numSub(string s) {\n long long ans=0;\n for(long long i=0;i<s.size(); | Sakettiwari | NORMAL | 2023-10-26T21:41:37.589723+00:00 | 2023-10-26T21:41:37.589748+00:00 | 20 | false | \n\n# Code\n```\nclass Solution {\npublic:\n const int mod=1e9+7;\n\n int numSub(string s) {\n long long ans=0;\n for(long long i=0;i<s.size();i++){\n if(s[i] == \'0\')\n continue;\n else{\n long long j;\n for( j=i;j<s.size();j++){\n ... | 1 | 0 | ['C++'] | 1 |
number-of-substrings-with-only-1s | Simple C++ solution | simple-c-solution-by-gcsingh1629-qsg5 | \n# Code\n\nclass Solution {\npublic:\n int numSub(string s) {\n int left=0,count=0,res=0,mod=1e9+7;\n for(int right=0;right<s.size();right++){ | gcsingh1629 | NORMAL | 2023-10-08T06:54:04.112308+00:00 | 2023-10-08T06:54:04.112328+00:00 | 80 | false | \n# Code\n```\nclass Solution {\npublic:\n int numSub(string s) {\n int left=0,count=0,res=0,mod=1e9+7;\n for(int right=0;right<s.size();right++){\n if(s[right]==\'1\'){\n count++;\n res=(res+count)%mod;\n }\n else{\n count=0... | 1 | 0 | ['C++'] | 1 |
number-of-substrings-with-only-1s | Super Easy Accumulator Approach | super-easy-accumulator-approach-by-sheve-2wfg | Approach\nWe iterate over all chars in string.\nIf character is 1, we increment our accuumulator by 1.\nThen we add accumulator to total result amount.\nThats b | shevernitskiy | NORMAL | 2023-08-22T13:23:31.061085+00:00 | 2023-08-22T13:23:31.061116+00:00 | 8 | false | # Approach\nWe iterate over all chars in string.\nIf character is 1, we increment our accuumulator by 1.\nThen we add accumulator to total result amount.\nThats beacuse in `1` there is 1 variant, then in `11` there are 3 variants (1, 1, 11), but the first `1` we already count on previous step. So just increment accumu... | 1 | 0 | ['TypeScript'] | 0 |
number-of-substrings-with-only-1s | Easy to understand JavaScript solution | easy-to-understand-javascript-solution-b-2t1t | Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n\nvar numSub = function(s) {\n const MODULO = 10 ** 9 + 7;\n let current = re | tzuyi0817 | NORMAL | 2023-08-06T08:01:40.198340+00:00 | 2023-08-06T08:01:40.198365+00:00 | 14 | false | # Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nvar numSub = function(s) {\n const MODULO = 10 ** 9 + 7;\n let current = result = 0;\n\n for (let index = 0; index <= s.length; index++) {\n const value = s[index];\n\n if (value === \'1\') current += 1;\n... | 1 | 0 | ['JavaScript'] | 0 |
number-of-substrings-with-only-1s | ✅✔️Easy Peasy Implementation || C++ Solution ✈️✈️✈️✈️✈️ | easy-peasy-implementation-c-solution-by-upbtj | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | ajay_1134 | NORMAL | 2023-07-13T05:50:12.760940+00:00 | 2023-07-13T05:50:12.760964+00:00 | 186 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['String', 'C++'] | 0 |
number-of-substrings-with-only-1s | Q1513 Accepted C++ ✅ Sliding Win & Counting | Easiest Method | q1513-accepted-c-sliding-win-counting-ea-4356 | CRUX\n1) All single 1\'s are the part of the ans. Inital loop for counting the number of 1\'s in the string s.\n2) Keeping the count as 1 and increment if 1\'s | adityasrathore | NORMAL | 2023-07-07T06:58:15.187206+00:00 | 2023-07-07T06:58:15.187240+00:00 | 347 | false | CRUX\n1) All single 1\'s are the part of the ans. Inital loop for counting the number of 1\'s in the string s.\n2) Keeping the count as 1 and increment if 1\'s are found consecutively.\n3) Otherwise put count as 1 again.\n\nThe code is very readable, Please go through the code and read the above points again for better... | 1 | 0 | ['C', 'Sliding Window', 'Counting'] | 0 |
number-of-substrings-with-only-1s | [C++] || Easy to understand | c-easy-to-understand-by-riteshkhan-bgb5 | \nclass Solution {\npublic:\n int numSub(string s) {\n long long c=0, p=0, t = pow(10,9);\n for(auto& i : s){\n if(i==\'1\'){\n | RiteshKhan | NORMAL | 2022-12-16T08:02:39.853184+00:00 | 2022-12-16T08:02:39.853222+00:00 | 86 | false | ```\nclass Solution {\npublic:\n int numSub(string s) {\n long long c=0, p=0, t = pow(10,9);\n for(auto& i : s){\n if(i==\'1\'){\n ++c;\n }\n else{\n p += c*(c+1)/2;\n c=0;\n }\n }\n p += c*(c+1)/2;\n... | 1 | 0 | ['C'] | 0 |
number-of-substrings-with-only-1s | Beginner Friendly|| Easy C++ Solution | beginner-friendly-easy-c-solution-by-pra-760a | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Prashant-singh | NORMAL | 2022-11-18T12:12:14.331364+00:00 | 2022-11-18T12:12:14.331404+00:00 | 836 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['C++'] | 1 |
number-of-substrings-with-only-1s | Most Optimal Java Solution | most-optimal-java-solution-by-prince077-2y54 | \n public int numSub(String s) {\n long count = 0 , res = 0 , mod = (int)1e9+7 ;\n for(int i = 0 ; i < s.length() ; i++){\n count+=s.ch | prince077 | NORMAL | 2022-11-18T04:43:14.843926+00:00 | 2022-11-18T04:48:26.132716+00:00 | 906 | false | ```\n public int numSub(String s) {\n long count = 0 , res = 0 , mod = (int)1e9+7 ;\n for(int i = 0 ; i < s.length() ; i++){\n count+=s.charAt(i)-\'0\';\n if(s.charAt(i)-\'0\'==0){\n res+=((count*(count+1))/2);\n count = 0 ;\n }\n }\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | c++ | easy to understand | short | c-easy-to-understand-short-by-venomhighs-0dor | \n# Code\n\nclass Solution {\npublic:\n int numSub(string s) {\n vector<long> dp(s.length(), 0);\n if(s[0] == \'1\')\n dp[0] = 1;\n | venomhighs7 | NORMAL | 2022-10-06T04:06:29.034239+00:00 | 2022-10-06T04:06:29.034286+00:00 | 679 | false | \n# Code\n```\nclass Solution {\npublic:\n int numSub(string s) {\n vector<long> dp(s.length(), 0);\n if(s[0] == \'1\')\n dp[0] = 1;\n \n long sum = 0;\n for(int i = 1 ; i < s.length(); i++)\n {\n if(s[i] == \'1\')\n dp[i] = dp[i - 1] + 1... | 1 | 0 | ['C++'] | 0 |
number-of-substrings-with-only-1s | Java Simple Solution | Runtime : 7ms | java-simple-solution-runtime-7ms-by-hars-6vuo | \nclass Solution {\n public int numSub(String s) {\n if(s.indexOf("1") < 0)\n return 0;\n int count = 0,res=0, mod = 1_000_000_007;\ | Harsh_Kode | NORMAL | 2022-09-06T16:08:17.803755+00:00 | 2022-09-06T16:08:17.803804+00:00 | 654 | false | ```\nclass Solution {\n public int numSub(String s) {\n if(s.indexOf("1") < 0)\n return 0;\n int count = 0,res=0, mod = 1_000_000_007;\n for(int i=0 ; i < s.length(); i++){\n count = s.charAt(i) == \'1\' ? count+1 : 0;\n res = (res + count) % mod;\n }\n ... | 1 | 0 | ['Java'] | 1 |
number-of-substrings-with-only-1s | help needed!! sliding window logic. | help-needed-sliding-window-logic-by-deva-vjrz | 54/56 testcases passed!!!\n\n\'\'\'\nclass Solution {\n public int numSub(String s) {\n int i,j;\n \n\t\ti=0;j=0;\n\t\tint ans=0;\n\t\twhile(j<s.le | devanshp | NORMAL | 2022-08-15T17:12:14.929083+00:00 | 2022-08-15T17:12:14.929114+00:00 | 528 | false | **54/56 testcases passed!!!**\n\n\'\'\'\nclass Solution {\n public int numSub(String s) {\n int i,j;\n \n\t\ti=0;j=0;\n\t\tint ans=0;\n\t\twhile(j<s.length())\n\t\t{\n\t\t if(s.charAt(j)==\'0\')\n\t\t {\n\t\t i=j+1;\n\t\t j++;\n\t\t }\n\t\t \n\t\t else if(s.charAt(j)==\'1\')\n\... | 1 | 0 | ['Sliding Window', 'Java'] | 1 |
number-of-substrings-with-only-1s | Run length counting C++ solution (O(n) and no branching) | run-length-counting-c-solution-on-and-no-dysy | We note that the number of sub-strings is just the partial sum of the natural numbers up until the length of the original string, i.e. given a string of 1\'s of | fpj | NORMAL | 2022-07-26T09:33:42.087622+00:00 | 2022-07-26T09:33:42.087651+00:00 | 162 | false | We note that the number of sub-strings is just the partial sum of the natural numbers up until the length of the original string, i.e. given a string of `1`\'s of length *N* we have *(N + (N + 1)) / 2* sub-strings.\n\nLet\'s look at a few examples.\n\n* For the input `1` we have 1 sub-string: `[1]`\n* For the input `11... | 1 | 0 | ['C', 'Iterator'] | 0 |
number-of-substrings-with-only-1s | C++ | Sliding Window || Commented Code || Diagram | c-sliding-window-commented-code-diagram-a3fw0 | \n\n\nclass Solution {\npublic:\n int numSub(string s) {\n int i=0,j=0;\n int n = s.size();\n int res=0;\n for(;j<=n;j++){\n | p3jit | NORMAL | 2022-07-20T08:11:51.190079+00:00 | 2022-07-20T17:44:17.486078+00:00 | 191 | false | \n\n```\nclass Solution {\npublic:\n int numSub(string s) {\n int i=0,j=0;\n int n = s.size();\n int res=0;\n for(;j<=n;j++){\n if(s[j]==\'0\' or j==n){ // the moment o... | 1 | 0 | ['C', 'Sliding Window'] | 0 |
number-of-substrings-with-only-1s | java solution | java-solution-by-somidhroy-wsem | \nclass Solution {\n public int numSub(String s) {\n long count = 0;\n int n = 0;\n long mod = 1000000007;\n for(int i = 0; i < s | somidhroy | NORMAL | 2022-06-20T04:36:16.814711+00:00 | 2022-06-20T04:50:38.223908+00:00 | 342 | false | ```\nclass Solution {\n public int numSub(String s) {\n long count = 0;\n int n = 0;\n long mod = 1000000007;\n for(int i = 0; i < s.length(); i++){\n if(s.charAt(i) == \'1\'){\n count = (count + ++n);\n }\n else{\n n = 0;\n ... | 1 | 0 | ['Java'] | 0 |
number-of-substrings-with-only-1s | C++ || EASY TO UNDERSTAND || Simple Solution | c-easy-to-understand-simple-solution-by-vmwpu | \n#define ll long long int\nll mod=1e9+7;\nclass Solution {\npublic:\n int numSub(string s) {\n int n=s.length();\n int c=1;\n ll ans=0; | aarindey | NORMAL | 2022-06-12T00:17:32.040338+00:00 | 2022-06-12T00:17:32.040375+00:00 | 81 | false | ```\n#define ll long long int\nll mod=1e9+7;\nclass Solution {\npublic:\n int numSub(string s) {\n int n=s.length();\n int c=1;\n ll ans=0;\n int cnt=0;\n for(char ch:s)\n {\n cnt+=(ch-\'0\');\n }\n if(cnt==0)\n return 0;\n s+=\'0\';\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Number of Substrings With Only 1s | C++ Solution | number-of-substrings-with-only-1s-c-solu-kgf3 | class Solution {\npublic:\n int numSub(string s) \n {\n int n=s.length();\n vectordp(n+1,0);\n for(int i=0;i<n;i++)\n {\n | prabhatm580 | NORMAL | 2022-03-08T05:32:23.797176+00:00 | 2022-03-08T05:32:23.797226+00:00 | 137 | false | class Solution {\npublic:\n int numSub(string s) \n {\n int n=s.length();\n vector<int>dp(n+1,0);\n for(int i=0;i<n;i++)\n {\n if(s[i]==\'1\')\n dp[i+1]=dp[i]+1;\n }\n long long int res=0;\n for(int i=0;i<=n;i++)\n res=... | 1 | 0 | ['Dynamic Programming', 'C', 'C++'] | 0 |
number-of-substrings-with-only-1s | Python easy to read and understand | python-easy-to-read-and-understand-by-sa-acgr | ```\nclass Solution:\n def numSub(self, s: str) -> int:\n cnt, ans = 0, 0\n for i in range(len(s)):\n if s[i] == \'0\':\n | sanial2001 | NORMAL | 2022-03-02T19:24:11.309441+00:00 | 2022-03-02T19:24:11.309475+00:00 | 197 | false | ```\nclass Solution:\n def numSub(self, s: str) -> int:\n cnt, ans = 0, 0\n for i in range(len(s)):\n if s[i] == \'0\':\n cnt = 0\n else:\n cnt += 1\n ans += cnt\n return ans % ((10**9)+7) | 1 | 0 | ['Python', 'Python3'] | 1 |
number-of-substrings-with-only-1s | C++ / O(n) time / Simple solution with explanation / count / no math formula | c-on-time-simple-solution-with-explanati-d6e9 | Just use cnt to count # of consecutive 1 and add into res\n\nclass Solution {\npublic:\n int numSub(string s) {\n int n = s.size(), cnt = 0, res = 0, | pat333333 | NORMAL | 2022-02-20T06:02:14.236972+00:00 | 2022-02-20T06:03:45.808419+00:00 | 60 | false | Just use `cnt` to count # of consecutive 1 and add into `res`\n```\nclass Solution {\npublic:\n int numSub(string s) {\n int n = s.size(), cnt = 0, res = 0, mod = 1e9+7;\n for (int i = 0; i < n; ++i) {\n cnt = s[i] == \'1\' ? cnt+1 : 0;\n res = (res+cnt) % mod;\n }\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | O(n) Time O(1) Space Java Solution | on-time-o1-space-java-solution-by-asamog-7741 | Looking at strings of only 1s:\n1 should return 1, 11 should return 3, 111 should return 6, and so on.\nObserve that for a chain of k 1s, the expected result is | asamogh94 | NORMAL | 2021-11-28T06:56:49.014345+00:00 | 2021-11-28T06:57:48.856239+00:00 | 192 | false | Looking at strings of only 1s:\n`1` should return 1, `11` should return 3, `111` should return 6, and so on.\nObserve that for a chain of `k` `1s`, the expected result is `k(k+1)/2`.\n\nKeep track of the length of the current chain of `1s` as you iterate through the string. Suppose the current chain of `1s` is of lengt... | 1 | 0 | ['Java'] | 0 |
number-of-substrings-with-only-1s | O(n): Monotonic Stack | on-monotonic-stack-by-shaunakdas88-194i | For each possible index i of our string s, we can ask the simpler/local question of what is the number of substrings consisting exclusively of 1\'s that end at | shaunakdas88 | NORMAL | 2021-10-27T16:40:58.181811+00:00 | 2021-10-27T16:56:48.623584+00:00 | 78 | false | For each possible index `i` of our string `s`, we can ask the simpler/local question of what is the number of substrings consisting exclusively of `1`\'s that end at index `i`. If we sum these local counts over all possible indices `i = 0, ..., n-1`, we will generate our desired answer.\n\nThere are two cases to consi... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++ || EASY | c-easy-by-easy_coder-yign | ```\nclass Solution {\npublic:\n int numSub(string s) {\n int ans=0;\n int i=0;\n int x=1e9+7;\n while(i<s.size()){\n | Easy_coder | NORMAL | 2021-10-20T07:07:39.943215+00:00 | 2021-10-20T07:07:39.943249+00:00 | 96 | false | ```\nclass Solution {\npublic:\n int numSub(string s) {\n int ans=0;\n int i=0;\n int x=1e9+7;\n while(i<s.size()){\n if(i<s.size() && s[i]==\'1\'){\n int no_of_ones=0;\n while(i<s.size() && s[i]!=\'0\') {no_of_ones++; i++;}\n ans=(... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++ sol with comments | c-sol-with-comments-by-rakeshbodavula200-itf6 | Idea--> find the length of the each substring which consists of 1\'s and let that length be n and\n\t\t\twe add n*(n+1)/2 to the sum which we return at last\n\n | rakeshbodavula2002 | NORMAL | 2021-10-04T06:02:19.308383+00:00 | 2021-10-04T06:02:19.308432+00:00 | 91 | false | Idea--> find the length of the each substring which consists of 1\'s and let that length be n and\n\t\t\twe add n*(n+1)/2 to the sum which we return at last\n```\nint numSub(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n \n long long sum=0,count=0... | 1 | 0 | ['C++'] | 0 |
number-of-substrings-with-only-1s | C# - Simple to understand | c-simple-to-understand-by-keperkjr-mc9n | \npublic class Solution {\n public int NumSub(string s) {\n var result = 0;\n var count = 0;\n var mod = (int)Math.Pow(10, 9) + 7;\n | keperkjr | NORMAL | 2021-09-22T07:30:27.640296+00:00 | 2021-09-22T07:30:27.640333+00:00 | 53 | false | ```\npublic class Solution {\n public int NumSub(string s) {\n var result = 0;\n var count = 0;\n var mod = (int)Math.Pow(10, 9) + 7;\n \n foreach (var item in s) {\n if (item == \'1\') {\n ++count;\n } else {\n count = 0;\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++ | c-by-priyanka1230-kjk7 | \n\npublic:\n int numSub(string s) {\n long long int i,f=0,sum=0;\n for(i=0;i<s.length();i++)\n {\n f=0;\n while(i | priyanka1230 | NORMAL | 2021-07-25T10:44:02.220667+00:00 | 2021-07-25T10:44:02.220697+00:00 | 79 | false | ```\n\n```public:\n int numSub(string s) {\n long long int i,f=0,sum=0;\n for(i=0;i<s.length();i++)\n {\n f=0;\n while(i<s.length()&&s[i]==\'1\')\n {\n f++;\n i++;\n }\n sum=sum+((f*(f+1))/2)%1000000007;\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Java easy trick | java-easy-trick-by-yash_patil-k5je | ```\nclass Solution {\n public int numSub(String s) {\n int mod=(int)1e9+7;\n int count=0,res=0;\n for(int i=0;i<s.length();i++)\n | yash_patil | NORMAL | 2021-07-12T11:58:22.535244+00:00 | 2021-07-12T11:58:22.535286+00:00 | 74 | false | ```\nclass Solution {\n public int numSub(String s) {\n int mod=(int)1e9+7;\n int count=0,res=0;\n for(int i=0;i<s.length();i++)\n {\n if(s.charAt(i)==\'1\')\n {\n count=(count+1)%mod;\n }\n else\n {\n co... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Python3 } One Pass | 30% | | python3-one-pass-30-by-idbasketball08-206v | \nclass Solution:\n def numSub(self, s: str) -> int:\n #strategy: One Pass\n left = right = -1\n answer, mod = 0, 10 ** 9 + 7\n f | idbasketball08 | NORMAL | 2021-06-17T20:56:49.330959+00:00 | 2021-06-17T20:56:49.330985+00:00 | 47 | false | ```\nclass Solution:\n def numSub(self, s: str) -> int:\n #strategy: One Pass\n left = right = -1\n answer, mod = 0, 10 ** 9 + 7\n for i in range(len(s)):\n #create new interval\n if s[i] == \'0\':\n left = right = i\n #update right side to ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Simple C++ O(N) Solution | simple-c-on-solution-by-ranjan_1997-bddq | \nclass Solution {\npublic:\n int numSub(string s) {\n int i = 0;\n int n = s.length();\n int result = 0;\n while(i<n)\n { | ranjan_1997 | NORMAL | 2021-05-30T11:48:58.780542+00:00 | 2021-05-30T11:48:58.780573+00:00 | 61 | false | ```\nclass Solution {\npublic:\n int numSub(string s) {\n int i = 0;\n int n = s.length();\n int result = 0;\n while(i<n)\n {\n int tot = 0;\n if(s[i] == \'1\')\n {\n while(s[i] == \'1\' and i<n)\n {\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++ | O(n) 99.8% faster| with comments | c-on-998-faster-with-comments-by-shantan-zulx | \n#define MOD 1000000007\n\nclass Solution {\npublic:\n int numSub(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n lo | shantanu199 | NORMAL | 2021-05-26T22:06:25.862083+00:00 | 2021-05-26T22:06:25.862114+00:00 | 55 | false | ```\n#define MOD 1000000007\n\nclass Solution {\npublic:\n int numSub(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n long long int sum=0;\n int n=s.size();\n long long int count = s[0]==\'1\'? 1: 0;\n \n\t\t/*\n\t\tSubstring of 1s start: s[0]==\'1\' || s[... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Simplest Java Solution One Pass, No Formula With Explanation | simplest-java-solution-one-pass-no-formu-0636 | Intuition: The bruteforce way is to generate all the substrings and count all the substrings will all Ones. Time complexity for this would be :- O(n^3), which | amanthapliyal3 | NORMAL | 2021-05-15T15:49:58.055540+00:00 | 2021-05-15T15:49:58.055568+00:00 | 83 | false | **Intuition:** The bruteforce way is to generate all the substrings and count all the substrings will all Ones. Time complexity for this would be :- O(n^3), which could be optimised to O(n^2) if we count while creating substrings.\n\nBut from here we can observe that at each index we only want to know all the substrin... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Java | Clean Solution | java-clean-solution-by-user8540kj-co06 | This solution is not mine but i found this solution very interesting as it is beginner friendly\nSolution by : Nidhi_2125\nThe Idea is very simple you can count | user8540kj | NORMAL | 2021-04-12T14:33:48.717056+00:00 | 2022-08-01T05:01:14.620790+00:00 | 153 | false | **This solution is not mine but i found this solution very interesting as it is beginner friendly**\n*Solution by : Nidhi_2125*\n*The Idea is very simple you can count the number of ones and keep on checking the segment of one\'s (arr[i]==arr[i+1] ) if it is equal you can add this substring into your original substring... | 1 | 0 | [] | 1 |
number-of-substrings-with-only-1s | C++ Easy Solution beats 99% | c-easy-solution-beats-99-by-vibhu172000-6xpj | \nclass Solution {\npublic:\n int numSub(string s) {\n int cv= 1e9+7;\n long long count_ones=0;\n long long ans=0;\n for(int i=0; | vibhu172000 | NORMAL | 2021-03-30T13:00:38.440631+00:00 | 2021-03-30T13:00:38.440661+00:00 | 76 | false | ```\nclass Solution {\npublic:\n int numSub(string s) {\n int cv= 1e9+7;\n long long count_ones=0;\n long long ans=0;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==\'1\')\n {\n count_ones++;\n if(i+1==s.size() || s[i+1]!=\'1\')\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Easiest C++ Solution | 4 lines of code ! | easiest-c-solution-4-lines-of-code-by-ra-w0sg | \nclass Solution {\npublic:\n int m=1000000007;\n int numSub(string s,long long ans=0,long long c=0) {\n for(int i=0; i<s.size(); i++) {\n | rac101ran | NORMAL | 2021-03-19T00:31:21.475561+00:00 | 2021-03-19T00:31:21.475588+00:00 | 71 | false | ```\nclass Solution {\npublic:\n int m=1000000007;\n int numSub(string s,long long ans=0,long long c=0) {\n for(int i=0; i<s.size(); i++) {\n s[i]==\'0\'?ans+=((c*(c+1))/2)%m:c++; // add sum of length of substring in ans => (n*(n+1))/2\n c=s[i]==\'0\'?0:c; // if a 0 occurs just set... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | [C++]Really hate the extreme test samples | creally-hate-the-extreme-test-samples-by-ytia | with 1 \'1\' we got 1 substring\nwith 2 \'1\' we got 3 (2 + 1)\nwith 3 \'1\' we got 6 (3+ 2 + 1)\n...\nwith n \'1\' we got n*(n+1)/2\n\n int numSub(strin | wilbertgui | NORMAL | 2021-02-20T09:35:16.536128+00:00 | 2021-02-20T09:36:56.344101+00:00 | 99 | false | with 1 \'1\' we got 1 substring\nwith 2 \'1\' we got 3 (2 + 1)\nwith 3 \'1\' we got 6 (3+ 2 + 1)\n...\nwith n \'1\' we got n*(n+1)/2\n```\n int numSub(string s) {\n long cnt = 0; \n for(int i=0; i<s.size(); i++){\n long c1 = 0;\n while(s[i] == \'1\'){\n ++... | 1 | 0 | [] | 1 |
number-of-substrings-with-only-1s | Python O(n) time, O(1) space, straight forward solution | python-on-time-o1-space-straight-forward-vzbp | \nclass Solution:\n def numSub(self, s: str) -> int:\n res=0\n count=0\n for ch in s:\n if ch == \'1\':\n coun | philipphil | NORMAL | 2021-01-08T23:23:35.328284+00:00 | 2021-01-08T23:23:35.328316+00:00 | 76 | false | ```\nclass Solution:\n def numSub(self, s: str) -> int:\n res=0\n count=0\n for ch in s:\n if ch == \'1\':\n count+=1\n res+=count\n else:\n count=0\n \n return res % (10**9+7)\n``` | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Java easy solution | java-easy-solution-by-suryakant31-l886 | class Solution {\n public int numSub(String s) {\n long count = 0;\n\n long result = 0;\n for(int i = 0; i< s.length() ; i++ ){\n\n | suryakant31 | NORMAL | 2021-01-02T16:20:00.992513+00:00 | 2021-01-02T16:20:00.992537+00:00 | 86 | false | class Solution {\n public int numSub(String s) {\n long count = 0;\n\n long result = 0;\n for(int i = 0; i< s.length() ; i++ ){\n\n if(s.charAt(i) == \'1\'){\n count++;\n }\n\n if(s.charAt(i) == \'0\' || i == s.length()-1){\n res... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | easy-peasy cpp solution | easy-peasy-cpp-solution-by-bitrish-28an | \nclass Solution {\npublic:\n int numSub(string s) {\n long ans = 0;\n for(int i=0;i<s.length();i++)\n { long c=0;\n while( | bitrish | NORMAL | 2020-09-24T16:42:42.761820+00:00 | 2020-09-24T16:42:42.761862+00:00 | 138 | false | ```\nclass Solution {\npublic:\n int numSub(string s) {\n long ans = 0;\n for(int i=0;i<s.length();i++)\n { long c=0;\n while(s[i]==\'1\')\n {\n c++;\n i++;\n }\n ans += c*(c+1)/2;\n }\n return ans%10000000... | 1 | 0 | ['Math', 'C', 'C++'] | 0 |
number-of-substrings-with-only-1s | Simple Java Solution | simple-java-solution-by-jyotiprakashrout-axhe | \nclass Solution {\n public int numSub(String s) {\n int count = 0;\n String [] ones = s.split("0");\n \n for(String one : ones){ | jyotiprakashrout434 | NORMAL | 2020-08-06T21:50:34.597151+00:00 | 2020-08-06T21:50:34.597196+00:00 | 262 | false | ```\nclass Solution {\n public int numSub(String s) {\n int count = 0;\n String [] ones = s.split("0");\n \n for(String one : ones){\n int n = one.length();\n count += (n * (n + 1.0) / 2) % 1000_000_007;\n }\n return count; \n }\n}\n``` | 1 | 0 | ['Math', 'Java'] | 0 |
number-of-substrings-with-only-1s | [JS] | O(N) solution | simple math | With explanation | js-on-solution-simple-math-with-explanat-fuqi | 111 possible substrings :- 1, 1, 1, 11, 11, 111\n\n\n\nFor consective n 1 the number of substrings possible is n * ( n + 1 ) / 2. Using this principal we can l | vivekjain202 | NORMAL | 2020-07-30T17:39:02.383704+00:00 | 2020-07-30T17:42:24.944236+00:00 | 89 | false | `111` possible substrings :- `1, 1, 1, 11, 11, 111`\n\n\n\nFor consective n `1` the number of substrings possible is `n * ( n + 1 ) / 2`. Using this principal we can loop over the string and keep counting the consective one\'s once the current element is `0` we add it to `total += current * (current + 1) / 2` and res... | 1 | 1 | [] | 0 |
number-of-substrings-with-only-1s | CPP SIMPLE EASY SOLUTION | cpp-simple-easy-solution-by-chase_master-ctiq | \nclass Solution {\npublic:\n long long m=pow(10,9)+7;\n int numSub(string s) {\n long long i=0,j=0;\n int c=0;\n while(j<s.length()& | chase_master_kohli | NORMAL | 2020-07-28T13:23:51.953203+00:00 | 2020-07-28T13:23:51.953253+00:00 | 93 | false | ```\nclass Solution {\npublic:\n long long m=pow(10,9)+7;\n int numSub(string s) {\n long long i=0,j=0;\n int c=0;\n while(j<s.length()&&i<s.length()){\n while(i<s.length()&&s[i]==\'0\')\n i++;\n j=i;\n while(j<s.length()&&s[j]==\'1\')\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Easy counting (Python) | easy-counting-python-by-jinghuayao-ljcu | \nclass Solution:\n def numSub(self, s: str) -> int:\n res, MOD = 0, 10 ** 9 + 7\n for string in s.split(\'0\'):\n if string:\n | jinghuayao | NORMAL | 2020-07-21T01:35:29.574011+00:00 | 2020-07-21T01:35:29.574057+00:00 | 64 | false | ```\nclass Solution:\n def numSub(self, s: str) -> int:\n res, MOD = 0, 10 ** 9 + 7\n for string in s.split(\'0\'):\n if string:\n num = len(string)\n res += (1 + num) * num // 2\n return res % MOD\n``` | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Python O(n) by sliding window [w/ Comment] | python-on-by-sliding-window-w-comment-by-fn3c | Python O(n) sol by sliding window\n\n---\n\nImplementation\n\n\nclass Solution:\n def numSub(self, s: str) -> int:\n \n # padding one dummy zer | brianchiang_tw | NORMAL | 2020-07-18T07:30:22.631525+00:00 | 2021-05-09T03:25:27.507330+00:00 | 168 | false | Python O(n) sol by sliding window\n\n---\n\n**Implementation**\n\n```\nclass Solution:\n def numSub(self, s: str) -> int:\n \n # padding one dummy zero as ending symbol\n s = s + \'0\'\n \n idx, first_1_idx, last_1_idx = 0, None, None\n prev = None\n size = len(s)\n ... | 1 | 0 | ['Sliding Window', 'Iterator', 'Python', 'Python3'] | 0 |
number-of-substrings-with-only-1s | Python3 44ms/14.7MB, beats 99/100% | python3-44ms147mb-beats-99100-by-dreamsm-14ez | The one-liner was slower.\n\n\nclass Solution:\n def numSub(self, s: str) -> int:\n vals = list(map(len, s.split("0"))) \n p = 0\n for i | dreamsmasher | NORMAL | 2020-07-17T02:57:11.047670+00:00 | 2020-07-17T02:57:11.047700+00:00 | 53 | false | The one-liner was slower.\n\n```\nclass Solution:\n def numSub(self, s: str) -> int:\n vals = list(map(len, s.split("0"))) \n p = 0\n for i in vals:\n p += (i * (i+1) // 2)\n return p % (1000000007) \n\t\t``` | 1 | 1 | [] | 1 |
number-of-substrings-with-only-1s | C# One-Liner | c-one-liner-by-mhorskaya-ra8o | \npublic int NumSub(string s) =>\n\t(int)(s.Split(\'0\')\n\t\t.Select(x => (long)x.Length * (x.Length + 1) / 2)\n\t\t.Sum() % (1_000_000_000 + 7));\n | mhorskaya | NORMAL | 2020-07-16T06:30:51.522101+00:00 | 2020-07-16T06:31:22.234352+00:00 | 78 | false | ```\npublic int NumSub(string s) =>\n\t(int)(s.Split(\'0\')\n\t\t.Select(x => (long)x.Length * (x.Length + 1) / 2)\n\t\t.Sum() % (1_000_000_000 + 7));\n``` | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | Python: faster / less memory than 100% (with explanation) | python-faster-less-memory-than-100-with-i7ltj | Solution:\nSplit the given string by the character \'0\'. \nYou get a list of strings, ie [\'\', \'1\', \'\', \'111\']\nAnd for each string, count the number of | catters | NORMAL | 2020-07-15T00:26:39.913580+00:00 | 2020-07-15T00:26:39.913623+00:00 | 131 | false | Solution:\nSplit the given string by the character \'0\'. \nYou get a list of strings, ie [\'\', \'1\', \'\', \'111\']\nAnd for each string, count the number of substrings it yields:\nFor the string \'\', there are 0 substrings.\nFor the string \'1\', there are 1 substrings, \'1\'.\nFor the string \'11\', there are 2 s... | 1 | 1 | ['Python', 'Python3'] | 1 |
number-of-substrings-with-only-1s | python, head-on, explained with some tips | python-head-on-explained-with-some-tips-2yjeo | \nclass Solution:\n def numSub(self, s: str) -> int:\n d,l = defaultdict(int),0\n for i,v in enumerate(s):\n if v==\'1\':\n | rmoskalenko | NORMAL | 2020-07-13T20:35:55.783970+00:00 | 2020-07-13T20:40:23.445744+00:00 | 58 | false | ```\nclass Solution:\n def numSub(self, s: str) -> int:\n d,l = defaultdict(int),0\n for i,v in enumerate(s):\n if v==\'1\':\n l+=1\n else:\n d[l]+=1\n l=0\n d[l]+=1\n\n return sum( v*((k+1)*k//2) for k,v in d.items() ) % ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++ Sliding Window | c-sliding-window-by-oleksam-cp6g | \nint numSub(string s) {\n\tint res = 0;\n\tlong long curOnes = 0;\n\tint mod = 1000000007;\n\tfor (char c : s) {\n\t\tif (c == \'1\')\n\t\t\tcurOnes++;\n\t\tel | oleksam | NORMAL | 2020-07-13T18:35:35.858211+00:00 | 2020-07-13T18:36:40.900232+00:00 | 147 | false | ```\nint numSub(string s) {\n\tint res = 0;\n\tlong long curOnes = 0;\n\tint mod = 1000000007;\n\tfor (char c : s) {\n\t\tif (c == \'1\')\n\t\t\tcurOnes++;\n\t\telse {\n\t\t\tres += (curOnes * (curOnes + 1) / 2) % mod;\n\t\t\tcurOnes = 0;\n\t\t}\n\t}\n\tif (curOnes)\n\t\tres += (curOnes * (curOnes + 1) / 2) % mod;\n\tr... | 1 | 0 | ['C', 'Sliding Window', 'C++'] | 0 |
number-of-substrings-with-only-1s | C++ O(n) simple solution,40ms time | c-on-simple-solution40ms-time-by-rishabh-e9ax | \n int mod=1e9+7;\n int numSub(string s) {\n vector<long>ans;\n long count=0,sum=0;\n \n for(int i=0;i<s.length();i++)\n { | rishabhsp98 | NORMAL | 2020-07-13T06:26:14.336145+00:00 | 2020-07-13T06:26:14.336195+00:00 | 49 | false | ```\n int mod=1e9+7;\n int numSub(string s) {\n vector<long>ans;\n long count=0,sum=0;\n \n for(int i=0;i<s.length();i++)\n {\n char ch =s[i];\n count=0;\n if(ch==\'1\')\n {\n count++;\n i++;\n ... | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | C++ with some math | c-with-some-math-by-shkvorets-fxbq | \nlong res=0, n=0, len=s.length();\n for(int i=0; i<len; i++){\n if(s[i]==\'1\'){ \n n++;\n if(i==len-1 || s[i+1 | shkvorets | NORMAL | 2020-07-13T04:16:06.213668+00:00 | 2020-07-13T04:17:30.070296+00:00 | 58 | false | ```\nlong res=0, n=0, len=s.length();\n for(int i=0; i<len; i++){\n if(s[i]==\'1\'){ \n n++;\n if(i==len-1 || s[i+1]==\'0\'){\n res+=n*(n+1)/2;\n n=0;\n }\n }\n }\n return res % 1000000007;\n``` | 1 | 0 | [] | 0 |
number-of-substrings-with-only-1s | [Python] Sliding window | python-sliding-window-by-locphan2207-szom | python\ndef numSub(self, s: str) -> int:\n start = 0\n result = 0\n for i in range(len(s)):\n if s[i] == "0": start=i+1\n | locphan2207 | NORMAL | 2020-07-12T19:29:15.797902+00:00 | 2020-07-12T19:29:15.797949+00:00 | 50 | false | ```python\ndef numSub(self, s: str) -> int:\n start = 0\n result = 0\n for i in range(len(s)):\n if s[i] == "0": start=i+1\n else: result += i-start+1 \n return result%(10**9+7)\n\n``` | 1 | 0 | [] | 0 |
maximum-building-height | [Python/C++] greedy solution with visual explanation - O(MlogM) | pythonc-greedy-solution-with-visual-expl-d702 | Idea\n\nFirst of all, let\'s think about a simple scenario when there are only two restrictions. We can easily get the maximum height of the buildings between t | alanlzl | NORMAL | 2021-04-25T04:01:37.352928+00:00 | 2021-04-25T04:32:12.591021+00:00 | 5,809 | false | **Idea**\n\nFirst of all, let\'s think about a simple scenario when there are only two restrictions. We can easily get the maximum height of the buildings between these two restrictions. \n\nThe height is either bounded by two sides:\n\n<img src="https://assets.leetcode.com/users/images/836de0df-4fc7-41d5-9685-39564ec6... | 168 | 1 | [] | 22 |
maximum-building-height | C++ with picture, 2 passes | c-with-picture-2-passes-by-votrubac-dqpf | Intuition\nn could be quite large, but r.size() is limited to 100,000. So we should analyze restrictions, and compute the maximum height we can reach between ad | votrubac | NORMAL | 2021-04-25T05:10:42.462535+00:00 | 2021-04-27T17:11:44.637047+00:00 | 4,251 | false | #### Intuition\n`n` could be quite large, but `r.size()` is limited to 100,000. So we should analyze restrictions, and compute the maximum height we can reach between adjacent restrictions.\n\n#### Solution\nFirst, we sort our restrictions. Then, going from left to right, we compute the maximum possible height `h` betw... | 71 | 2 | [] | 11 |
maximum-building-height | Java solution with easy to understand explanation | java-solution-with-easy-to-understand-ex-u6wz | The height of building i will depend on maximum 3 factors:\n1. The restriction on i th building (if there is any)\n2. The height of left building.\n3. The heigh | divyagarg2601 | NORMAL | 2021-04-30T04:55:55.838482+00:00 | 2021-04-30T04:57:17.609234+00:00 | 1,303 | false | The height of building `i` will depend on maximum 3 factors:\n1. The restriction on `i th` building (if there is any)\n2. The height of left building.\n3. The height of right building.\n\nLet\'s understand it with the help of example.\nSuppose we have restrictions as: `[ [ 2, 1 ], [ 5, 6 ], [ 7, 1 ] ]`\n with explanation - 3 pass or 2 pass | python-on-with-explanation-3-pass-or-2-p-bgia | sort restriction (A) by first, then if A does not start from [1, 0], add or change the first element in A to [1, 0]\nThen go from left to right, check the pair | kai-leetcode | NORMAL | 2021-04-25T04:44:45.661143+00:00 | 2021-04-25T18:06:22.311056+00:00 | 1,048 | false | sort restriction (A) by first, then if A does not start from [1, 0], add or change the first element in A to [1, 0]\nThen go from left to right, check the pair of A[i], A[i+1] to see if A[i+1][1] is achievable. If not, update A[i+1][1] to the maximum achievable number by the rule. That\'s my first loop.\n\nThen go from... | 12 | 1 | [] | 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.