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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
maximum-candies-allocated-to-k-children | Binary Search, C++ With Explanation ✅✅ | binary-search-c-with-explanation-by-deep-3j1t | \n# Approach\n Describe your approach to solving the problem. \nHere i apply Binary Search Algorithm to find out Maximum Number of candies.\n\nMaximum Number of | Deepak_5910 | NORMAL | 2023-06-05T16:45:51.170565+00:00 | 2023-06-05T16:47:29.299886+00:00 | 129 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere i apply **Binary Search Algorithm** to find out Maximum Number of candies.\n\nMaximum Number of Candies Always Belong to range **[1 , Maximum of array]** , number of Maximum Candies can be any Element from this sorted array that satisfy the given condition-\n**[1,2,3,4,5,6,7,8,9...........Maximum of given array]**\n\nFirst = 1 \nLast = Maximum\n\nFind Index That Satisfy the Given Condition(allocate piles of candies to k children such that each child gets the same number of candies) ?\n\n**Note:-Here I also apply the same approach, have a look at these problems and try to solve with the same approach.**\n\n**Problem:-** https://leetcode.com/problems/koko-eating-bananas/description/\n**Solution:-** https://leetcode.com/problems/koko-eating-bananas/solutions/3601174/binary-search-c-with-explanation/\n\n\n\n\n# Complexity\n- Time complexity:O(N*Log(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 bool check(vector<int> arr,long long k,int n)\n {\n long long count = 0;\n for(int i = 0;i<arr.size();i++)\n count+=(arr[i]/n);\n \n if(count>=k)\n return true;\n\n return false;\n }\n int maximumCandies(vector<int>& arr, long long k) {\n \n int n = arr.size(),ans = 0;\n int first = 1,last = INT_MIN;\n\n for(int i = 0;i<n;i++)\n last = max(last,arr[i])+1;\n \n while(first<last)\n {\n int mid = (first+last)/2;\n\n if(check(arr,k,mid))\n {\n first = mid+1;\n ans = max(ans,mid);\n }\n else\n last = mid;\n }\n \n return ans;\n }\n};\n```\n\n | 2 | 0 | ['Binary Search', 'C++'] | 0 |
maximum-candies-allocated-to-k-children | using binary search on answer , explanation | using-binary-search-on-answer-explanatio-e3jt | instead of applying binary search directly on array we will apply binary search on the range of the answer.\n\nlow = 1 because there can be minimum 1 candy in t | AbhayDutt | NORMAL | 2023-04-01T09:55:27.451852+00:00 | 2023-04-01T09:55:27.451892+00:00 | 983 | false | instead of applying binary search directly on array we will apply binary search on the range of the answer.\n\nlow = 1 because there can be minimum 1 candy in the array\nhigh = maximum in the array because that is the maaximum amount of candies that can be present in the pile\n\n\nnow we want to validate each mid that we will get \n# Code\n```\nclass Solution {\n public int maximumCandies (int[] candies, long k) {\n int max = 0;\n for (int i : candies) {\n max = (max > i) ? max : i;\n }\n int start = 1, end = max, mid = 0, res = 0;\n while (start <= end) {\n mid = start + (end - start) / 2;\n if (isValid(candies, k, mid)) {\n res = mid;\n start = mid + 1;\n } else {\n end = mid - 1;\n }\n }\n return res;\n }\n private boolean isValid(int[] candies, long k, int mid) {\n long totalPiles = 0;\n for (int i : candies) {\n int toAdd = i / mid;\n totalPiles += toAdd;\n }\n return totalPiles >= k;\n }\n\n}\n```\nyou can check out my github repository where i am uploading famous interview questions topic wise with solutions.\nlink-- https://github.com/Abhaydutt2003/DataStructureAndAlgoPractice \nkindly upvote if you like my solution. you can ask doubts below.\n | 2 | 0 | ['Binary Search', 'Java'] | 1 |
maximum-candies-allocated-to-k-children | C++ || Binary Search on Search Space || Easy Solution | c-binary-search-on-search-space-easy-sol-6npp | Time Complexity : O(n*logm) , where m is the maximum number of candies in the single pile.\n\nclass Solution {\nprivate:\n bool isValid(vector<int>& candies, | aniketk2k | NORMAL | 2023-03-08T10:45:20.341807+00:00 | 2023-03-08T11:32:47.681824+00:00 | 872 | false | **Time Complexity : O(n*logm)** , where m is the maximum number of candies in the single pile.\n```\nclass Solution {\nprivate:\n bool isValid(vector<int>& candies, long long k, int max_candies){\n long long child_cnt = 0;\n \n for(auto candies: candies)\n child_cnt += candies/max_candies;\n \n return child_cnt >= k;\n }\npublic:\n int maximumCandies(vector<int>& candies, long long k) {\n int start = 1;\n int end = *max_element(candies.begin(), candies.end());\n int res = 0;\n\t\t\n while(start <= end){\n int mid = start+(end-start)/2;\n if(isValid(candies, k, mid)){\n res = mid;\n start = mid+1;\n }\n else\n end = mid-1;\n }\n \n return res;\n }\n};\n```\nUpvote if this helps.\nThanks! | 2 | 0 | ['C', 'Binary Tree', 'C++'] | 1 |
maximum-candies-allocated-to-k-children | ✅✅Easy Java code|| Beats 99%||✅✅ | easy-java-code-beats-99-by-princebharti-zfmp | \n# Approach\n Describe your approach to solving the problem. \nFirst find the range of candies to be distributed then,\nUse Binary Search to find the maximum n | PRINCEBharti | NORMAL | 2023-03-07T14:55:04.602965+00:00 | 2023-03-07T14:55:04.603011+00:00 | 566 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst find the range of candies to be distributed then,\nUse Binary Search to find the maximum number of candies distributed.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(nlogn)$$\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 maximumCandies(int[] candies, long k) {\n \n long sum = 0;\n for(int i = 0 ; i < candies.length; i++){\n sum+= candies[i];\n }\n int left = 1;\n int right = (int)(sum/k);\n // System.out.println(sum+" "+right);\n \n while(left <= right){\n int mid = (right - left) / 2 + left;\n long count = 0;\n \n for(int i = 0 ; i < candies.length; i++){\n count+= candies[i]/mid;\n }\n if(count >= k){\n left = mid+1;\n }\n else{\n right = mid-1;\n }\n }\n return right;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
maximum-candies-allocated-to-k-children | Java Sol || Easy || Binary Search || Simple | java-sol-easy-binary-search-simple-by-hi-d2ru | Code\n\nclass Solution {\n public int maximumCandies(int[] candies, long k) {\n int low = 1;\n int high = Arrays.stream(candies).max().getAsInt | himanshu_gupta_ | NORMAL | 2023-02-07T20:45:06.704385+00:00 | 2023-02-07T20:45:06.704412+00:00 | 624 | false | # Code\n```\nclass Solution {\n public int maximumCandies(int[] candies, long k) {\n int low = 1;\n int high = Arrays.stream(candies).max().getAsInt();\n int ans = 0;\n while (low<=high) {\n int mid = low + (high-low)/2;\n long currAns = 0;\n for (int i : candies) {\n currAns += i/mid;\n }\n if (currAns >= k) {\n low = mid+1;\n ans = Math.max(ans, mid);\n }\n else {\n high = mid-1;\n }\n }\n return ans;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
maximum-candies-allocated-to-k-children | Binary Search method | binary-search-method-by-optimal_ns65-3v91 | class Solution {\npublic:\n bool allocation(vector& candies,long long k,long long mid){\n if(mid==0) return true;\n \n long long q=0;\n | Optimal_NS65 | NORMAL | 2022-04-03T18:29:07.665170+00:00 | 2022-04-03T18:29:07.665219+00:00 | 24 | false | class Solution {\npublic:\n bool allocation(vector<int>& candies,long long k,long long mid){\n if(mid==0) return true;\n \n long long q=0;\n long long count=0;\n for(int i=0;i<candies.size();i++){\n int q=candies[i]/mid;\n count+=q;\n if(count>=k){\n return true;\n }\n }\n \n return false;\n \n }\n \n int maximumCandies(vector<int>& candies, long long k) {\n \n int ans=0;\n for(int i=0;i<candies.size();i++){\n ans=max(ans,candies[i]);\n } \n long long low=0,high=ans;\n while(low<high){\n long long mid=(low+high)/2;\n \n if(allocation(candies,k,mid)){\n low=mid+1;\n }else{\n high=mid;\n }\n }\n \n return allocation(candies,k,low)?low:low-1;\n \n \n \n }\n}; | 2 | 0 | [] | 0 |
maximum-candies-allocated-to-k-children | C++ | Simple Binary Search Solution O(n*log(max(candies))) | c-simple-binary-search-solution-onlogmax-xcre | \nclass Solution {\npublic:\n bool check(vector<int>& arr,int mid, long long k){\n long long count = 0;\n for(int i : arr){\n count | LastStyleBender | NORMAL | 2022-04-03T16:13:07.879198+00:00 | 2022-04-03T16:13:07.879243+00:00 | 74 | false | ```\nclass Solution {\npublic:\n bool check(vector<int>& arr,int mid, long long k){\n long long count = 0;\n for(int i : arr){\n count += (i/mid);\n }\n \n return (count >= k);\n }\n \n int maximumCandies(vector<int>& arr, long long k) {\n int lo = 1, hi = *max_element(arr.begin(),arr.end()),ans = 0; \n while(lo <= hi){\n int mid = lo + (hi - lo)/2;\n if(check(arr,mid,k)){\n ans = mid;\n lo = mid + 1;\n }else{\n hi = mid - 1;\n }\n }\n return ans;\n }\n};\n``` | 2 | 0 | ['C', 'Binary Tree'] | 1 |
maximum-candies-allocated-to-k-children | C++ || Binary Search | c-binary-search-by-kr_sk_01_in-u8z5 | \n bool is_possible(vector<int>& candies, int mid, long long k)\n {\n long long count = 0;\n \n for(int i = 0; i < candies.size(); i++) | KR_SK_01_In | NORMAL | 2022-04-03T07:27:30.530943+00:00 | 2022-04-03T07:27:30.530972+00:00 | 160 | false | ```\n bool is_possible(vector<int>& candies, int mid, long long k)\n {\n long long count = 0;\n \n for(int i = 0; i < candies.size(); i++)\n {\n count += candies[i] / mid;\n }\n \n if(count >= k)\n return true;\n \n return false;\n }\n \n int maximumCandies(vector<int>& candies, long long k) {\n \n int n = candies.size();\n \n int low = 1;\n \n int high = *max_element(candies.begin(), candies.end());\n \n int ans = 0;\n \n while(low <= high)\n {\n int mid = low + (high - low) / 2;\n \n if(is_possible(candies, mid, k))\n {\n ans = mid;\n \n low = mid + 1;\n }\n else\n {\n high = mid - 1;\n }\n }\n \n return ans;\n }\n \n \n``` | 2 | 0 | ['C', 'Binary Tree', 'C++'] | 0 |
maximum-candies-allocated-to-k-children | C++ Binary Search Solution | c-binary-search-solution-by-mayanksamadh-2bno | Please Upvote If It Helps\n\nclass Solution {\npublic:\n bool valid(int mid,vector<int>& candies,long long k)\n {\n // it will containg the count t | mayanksamadhiya12345 | NORMAL | 2022-04-03T06:41:35.741340+00:00 | 2022-04-03T06:42:00.450260+00:00 | 56 | false | **Please Upvote If It Helps**\n```\nclass Solution {\npublic:\n bool valid(int mid,vector<int>& candies,long long k)\n {\n // it will containg the count that will tell we can assign or not assign candies to children \n long sum = 0;\n for(auto it : candies)\n {\n sum += (it/mid);\n \n // if any time our sum is reches to k childrens it means we can assign so return true\n if(sum>=k)\n return true;\n }\n return false;\n }\n \n int maximumCandies(vector<int>& candies, long long k) \n {\n // initializing two pointers onr for min one for max\n // our answer lies between both of them it is final\n long left = 1;\n long right = *max_element(begin(candies), end(candies));\n \n // applyting the binary search for searching that element\n while(left<=right)\n {\n // deriving mid value\n long mid = left+(right-left)/2;\n \n // if we can assign mid candies to k childrens successfully \n // then can check for next bigger value\n if(valid(mid,candies,k))\n {\n left = mid+1;\n }\n \n // if we can not assign mid candies to k childrens successfully\n // then can check for lesser value\n else\n {\n right = mid-1;\n }\n }\n return right;\n }\n};\n``` | 2 | 0 | [] | 0 |
maximum-candies-allocated-to-k-children | Java | Binary Search | Easy To Understand | java-binary-search-easy-to-understand-by-zrap | Here I am discussing my approach to this problem\nApproach:\n1. First calculate total number of candies. Let it be sum.\n2. If sum < k, return 0 we have more ch | SudhanshuMallick | NORMAL | 2022-04-03T04:17:21.909428+00:00 | 2022-04-03T04:17:21.909472+00:00 | 181 | false | **Here I am discussing my approach to this problem**\n**Approach:**\n1. First calculate **total number** of candies. Let it be `sum`.\n2. If `sum < k`, **return 0** we have **more children than candies**.\n3. If `sum = k`, **return 1** as we have** as many children as candies**.\n4. Else, perform **binary search** on **number of candies** in the range where `low = 1`, and `high = max of candies`.\n5. Check for `mid`, if we can distribute mid number of candies to each children, then we can move `low to mid + 1`, else move` high to mid - 1.`\n6. Store the **last satisfied mid value in `ans`**.\n7. Return `ans`.\n\n**Source Code:**\n```\nclass Solution {\n public int maximumCandies(int[] candies, long k) {\n long sum = 0;\n long max = 0;\n \n for(int v : candies) {\n sum += v;\n max = Math.max(v, max);\n }\n \n if(sum < k)\n return 0;\n \n if(sum == k)\n return 1;\n \n long x = 1, y = max, ans = 1;\n \n while(x <= y) {\n long mid = (x + y) >> 1;\n long cur = 0;\n \n for(int v : candies) {\n cur += v / mid;\n }\n \n if(cur>= k) {\n ans = mid;\n x = mid + 1;\n }\n else\n y = mid - 1;\n }\n \n return (int) (ans);\n \n }\n}\n```\n\n**Complexity Analysis:**\n```\nTime Complexity: O(n * log(max)) // n = number of candy piles, max = max value of candy pile\nSpace Complexity : O(1)\n``` | 2 | 0 | ['Binary Tree', 'Java'] | 0 |
maximum-candies-allocated-to-k-children | 🎁C++ Binary Search Easy Explaination Code | c-binary-search-easy-explaination-code-b-6hnt | \n\nclass Solution\n{\npublic:\n long long int maxPiles(vector<int> candies, long long int k)\n {\n long long int l = 1; // We can select an minimu | omhardaha1 | NORMAL | 2022-04-03T04:13:28.672021+00:00 | 2022-04-03T04:15:57.211359+00:00 | 66 | false | ```\n\nclass Solution\n{\npublic:\n long long int maxPiles(vector<int> candies, long long int k)\n {\n long long int l = 1; // We can select an minimum 1 element\n long long int h = 0;\n for (auto i : candies)\n {\n h += i;\n }\n if (k > h)\n {\n return 0;\n }\n h /= k; // at most selection\n // this ans for storing mid value\n int ans = 0;\n while (l <= h)\n {\n long long mid = (l + h) / 2;\n long long int count = 0;\n for (auto i : candies)\n {\n int tt = (i / mid);\n count += tt;\n }\n if (count >= k)\n {\n ans = mid; // updating mid\n l = mid + 1;\n }\n else\n {\n h = mid - 1;\n }\n }\n return ans;\n }\n int maximumCandies(vector<int> &candies, long long k)\n {\n return maxPiles(candies, k);\n }\n};\n``` | 2 | 0 | ['C', 'Binary Tree', 'C++'] | 0 |
maximum-candies-allocated-to-k-children | [CPP] Binary Search || Easy to Understand | cpp-binary-search-easy-to-understand-by-74725 | \nclass Solution {\npublic:\n //checks if each children can get C candies\n bool isPossible(vector<int>& candies, long long C, long long k) {\n lon | arsator | NORMAL | 2022-04-03T04:02:04.812294+00:00 | 2022-04-03T04:02:04.812341+00:00 | 67 | false | ```\nclass Solution {\npublic:\n //checks if each children can get C candies\n bool isPossible(vector<int>& candies, long long C, long long k) {\n long long cnt = 0;\n for(auto c : candies) {\n cnt += (long long)(c/C);\n }\n \n return cnt >= k;\n }\n \n //Binary Search on ans(no. of candies each can get)\n int maximumCandies(vector<int>& candies, long long k) {\n long long sum = 0;\n for(auto c : candies) sum += (long long)c;\n \n long long end = sum / k + 1;\n long long beg = 0;\n while(beg + 1 < end) {\n long long mid = (beg + end) / 2;\n if(isGood(candies, mid, k)) beg = mid;\n else end = mid;\n }\n \n return (int)beg;\n }\n};\n``` | 2 | 0 | [] | 0 |
maximum-candies-allocated-to-k-children | 🧠 Greedy Candy Splitting: The Binary Search Trick Everyone Misses! | greedy-candy-splitting-the-binary-search-yqp6 | IntuitionWe want to split piles of candies into exactly k children — each getting the maximum equal amount possible.
Trying all possible values is too slow. But | loginov-kirill | NORMAL | 2025-03-30T07:01:14.353214+00:00 | 2025-04-01T18:55:04.384759+00:00 | 1,140 | false |
### Intuition
We want to split piles of candies into exactly `k` children — each getting the **maximum equal amount possible**.
Trying all possible values is too slow. But what if we reverse the problem and ask:
**“Can we give each child `x` candies?”**
This naturally leads us to **binary search**!
---
### Approach

1. Use binary search over the answer: the amount of candies per child can range from `1` to `max(candies)`.
2. For each `mid`, check if it's possible to make at least `k` piles, where each pile has exactly `mid` candies.
- Do this by summing `floor(c / mid)` for each pile.
3. If it's possible, store `mid` as a candidate and try to go higher.
4. If not, reduce the upper bound.
The result is the **maximum number of candies per child** we can guarantee across `k` kids.
---
### Complexity
**Time Complexity:**
( O(n log m) ) where `n = len(candies)` and `m = max(candies)`
**Space Complexity:**
( O(1) ) — only a few variables used.
---
### Code
```python []
class Solution(object):
def maximumCandies(self, candies, k):
l, r, ans = 1, max(candies), 0
while l <= r:
mid = (l + r) // 2
if sum(c // mid for c in candies) >= k:
ans = mid
l = mid + 1
else:
r = mid - 1
return ans
```
```javascript []
var maximumCandies = function(candies, k) {
let left = 1, right = Math.max(...candies), ans = 0;
const canDistribute = (mid) => {
let count = 0;
for (let c of candies) {
count += Math.floor(c / mid);
}
return count >= k;
};
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (canDistribute(mid)) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
};
```
<img src="https://assets.leetcode.com/users/images/b91de711-43d7-4838-aabe-07852c019a4d_1743313174.0180438.webp" width="270"/>
| 1 | 0 | ['Array', 'Math', 'Binary Search', 'Heap (Priority Queue)', 'Python', 'JavaScript'] | 1 |
count-prefixes-of-a-given-string | [Java/C++/Python] Starts With | javacpython-starts-with-by-lee215-lw6y | Explanation\nfor each word w in words list,\ncheck if word w startsWith the string s\n\n\n# Complexity\nTime O(NS)\nSpace O(1)\n\n\nJava\njava\n public int c | lee215 | NORMAL | 2022-04-30T16:16:13.275791+00:00 | 2022-05-03T06:43:22.216403+00:00 | 6,395 | false | # **Explanation**\nfor each word `w` in `words` list,\ncheck if word `w` `startsWith` the string `s`\n<br>\n\n# **Complexity**\nTime `O(NS)`\nSpace `O(1)`\n<br>\n\n**Java**\n```java\n public int countPrefixes(String[] words, String s) {\n int res = 0;\n for (String w : words)\n if (s.startsWith(w))\n res++;\n return res;\n }\n```\n\n**C++**\nTime O(NWS)\n```cpp\n int countPrefixes(vector<string>& words, string s) {\n int res = 0;\n for (auto& w : words)\n res += s.find(w) < 1;\n return res; \n }\n```\n\n**C++**\nSuggested by @mzchen\n```cpp\n int countPrefixes(vector<string>& words, string s) {\n int res = 0;\n for (auto& w : words)\n res += !s.compare(0, w.size(), w);\n return res;\n }\n```\n\n**Python**\n```py\n def countPrefixes(self, words, s):\n return sum(map(s.startswith, words))\n```\n | 46 | 0 | ['C', 'Python', 'Java'] | 12 |
count-prefixes-of-a-given-string | C++ || Easy O(NxS) solution || String | c-easy-onxs-solution-string-by-yash2arma-3lb0 | \nclass Solution {\npublic:\n //iterate over each words[i] and take the sub-string str from s of size equals to words[i].\n //and compare str with words[i | Yash2arma | NORMAL | 2022-04-30T16:13:17.949594+00:00 | 2022-05-01T07:29:15.665748+00:00 | 2,487 | false | ```\nclass Solution {\npublic:\n //iterate over each words[i] and take the sub-string str from s of size equals to words[i].\n //and compare str with words[i] if both are equal increase count by 1.\n int countPrefixes(vector<string>& words, string s) \n {\n int count=0;\n for(auto it: words)\n {\n string str = s.substr(0, it.size());\n if(str == it) count++; \n }\n return count; \n }\n};\n``` | 28 | 0 | ['String', 'C', 'Iterator', 'C++'] | 3 |
count-prefixes-of-a-given-string | Java 1-Liner | java-1-liner-by-fllght-z8l9 | java\npublic int countPrefixes(String[] words, String s) {\n return (int) Arrays.stream(words).filter(s::startsWith).count();\n }\n\n\nMy repositories | FLlGHT | NORMAL | 2022-05-01T08:12:39.278135+00:00 | 2023-05-12T05:22:01.753813+00:00 | 1,724 | false | ```java\npublic int countPrefixes(String[] words, String s) {\n return (int) Arrays.stream(words).filter(s::startsWith).count();\n }\n```\n\nMy repositories with leetcode problems solving - [Java](https://github.com/FLlGHT/algorithms/tree/master/j-algorithms/src/main/java), [C++](https://github.com/FLlGHT/algorithms/tree/master/c-algorithms/src/main/c%2B%2B) | 27 | 0 | ['Java'] | 1 |
count-prefixes-of-a-given-string | Easy python solution | easy-python-solution-by-tusharkhanna575-rxrk | \nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n count=0\n for i in words:\n if (s[:len(i)]==i):\n | tusharkhanna575 | NORMAL | 2022-05-26T14:42:38.412749+00:00 | 2022-05-26T14:42:38.412777+00:00 | 1,521 | false | ```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n count=0\n for i in words:\n if (s[:len(i)]==i):\n count+=1\n return count\n``` | 15 | 0 | ['Python', 'Python3'] | 1 |
count-prefixes-of-a-given-string | C++ || EASY TO UNDERSTAND || Simple Solution | c-easy-to-understand-simple-solution-by-drkqu | \nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) \n {\n int counter = 0;\n \n for(int i=0;i<words.siz | mayanksamadhiya12345 | NORMAL | 2022-04-30T16:10:41.260290+00:00 | 2022-05-01T16:15:53.055127+00:00 | 1,085 | false | ```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) \n {\n int counter = 0;\n \n for(int i=0;i<words.size();i++)\n {\n if(words[i] == s.substr(0,words[i].size())) \n counter++;\n }\n\n return counter;\n }\n};\n``` | 11 | 0 | [] | 2 |
count-prefixes-of-a-given-string | count_if | count_if-by-votrubac-cb8s | C++\ncpp\nint countPrefixes(vector<string>& words, string s) {\n return count_if(begin(words), end(words), [&](const auto &w){ return s.compare(0, w.size(), | votrubac | NORMAL | 2022-05-01T22:06:35.443879+00:00 | 2022-05-03T03:55:36.094024+00:00 | 798 | false | **C++**\n```cpp\nint countPrefixes(vector<string>& words, string s) {\n return count_if(begin(words), end(words), [&](const auto &w){ return s.compare(0, w.size(), w) == 0; });\n}\n``` | 8 | 0 | ['C'] | 3 |
count-prefixes-of-a-given-string | Python simple solution | python-simple-solution-by-tovam-xyjb | Approach\nCheck for each word if is prefix of s.\n\n# Code\n\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n res = 0\n\ | TovAm | NORMAL | 2023-01-05T15:28:38.090957+00:00 | 2023-01-05T15:28:38.090995+00:00 | 1,139 | false | # Approach\nCheck for each word if is prefix of s.\n\n# Code\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n res = 0\n\n for word in words:\n if s.startswith(word):\n res += 1\n\n return res\n```\n\nLike it? Please upvote! | 7 | 0 | ['Python3'] | 1 |
count-prefixes-of-a-given-string | Java Easy to Understand Solution | java-easy-to-understand-solution-by-janh-wflx | \nclass Solution {\n public int countPrefixes(String[] words, String s) {\n \n int result = 0;\n for (int i=0; i<=s.length(); i++) { | Janhvi__28 | NORMAL | 2022-10-20T18:06:56.439131+00:00 | 2022-10-20T18:06:56.439175+00:00 | 814 | false | ```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n \n int result = 0;\n for (int i=0; i<=s.length(); i++) {\n String x = s.substring(0, i);\n for (int j = 0; j<words.length; j++) {\n if (x.equals(words[j]))\n result++;\n }\n }\n return result;\n }\n}\n``` | 7 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | ✅✅✅Straightforward Javascript Solution || Faster than 96.67% | straightforward-javascript-solution-fast-95qp | \nvar countPrefixes = function(words, s) {\n let count = 0;\n for (let word of words) {\n if (s.startsWith(word)) count++\n } return count;\n};\ | a-fr0stbite | NORMAL | 2022-05-28T17:24:05.888742+00:00 | 2022-08-02T00:42:34.923627+00:00 | 459 | false | ```\nvar countPrefixes = function(words, s) {\n let count = 0;\n for (let word of words) {\n if (s.startsWith(word)) count++\n } return count;\n};\n```\n\n | 7 | 0 | ['JavaScript'] | 1 |
count-prefixes-of-a-given-string | ✅ 2 Lines || C++ / java | 2-lines-c-java-by-xxvvpp-g6gk | Apply find() in case of c++ and indexof() in case of java on every word and check if return position is 0.\nReturn the count of such strings.\n# C++\n int co | xxvvpp | NORMAL | 2022-04-30T16:15:05.509693+00:00 | 2022-04-30T16:21:12.974176+00:00 | 975 | false | Apply **find()** in case of c++ and **indexof()** in case of java on every word and check if return position is 0.\nReturn the count of such strings.\n# C++\n int countPrefixes(vector<string>& words, string s, int cnt=0){\n for(auto i:words) if(s.find(i)==0) cnt++;\n return cnt;\n }\n\t\n# Java\n public int countPrefixes(String[] words, String s) {\n int cnt=0;\n for(var i:words) if(s.indexOf(i)==0) cnt++;\n return cnt;\n } | 7 | 0 | ['C', 'Java'] | 3 |
count-prefixes-of-a-given-string | C++ || Basic string understanding | c-basic-string-understanding-by-shm_47-4r2k | \nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int cnt = 0;\n int i, j;\n for(i=0; i<words.size(); | shm_47 | NORMAL | 2022-04-30T16:11:13.336732+00:00 | 2022-04-30T16:11:13.336771+00:00 | 555 | false | ```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int cnt = 0;\n int i, j;\n for(i=0; i<words.size(); i++){\n j = 0;\n while(j< words[i].size()){\n if(words[i][j] != s[j])\n break;\n j++;\n }\n if(j == words[i].size())\n cnt++;\n }\n \n return cnt;\n }\n};\n``` | 7 | 0 | ['C'] | 0 |
count-prefixes-of-a-given-string | C++ Easy Solution using Only One For loop and One If Condition | c-easy-solution-using-only-one-for-loop-djv3c | Intuition\nThe intuition for the problem is to iterate through each word in the words array, extract a substring from s that matches the length of the word, and | _sxrthakk | NORMAL | 2024-06-29T12:16:30.243764+00:00 | 2024-06-29T12:16:30.243794+00:00 | 280 | false | # Intuition\nThe intuition for the problem is to iterate through each word in the words array, extract a substring from s that matches the length of the word, and compare it with the word itself. Increment a counter each time a match is found, returning the final count of matching prefixes.\n\n# Approach\n**Initialize Counter :** Start by initializing a counter c to zero. This will keep track of the number of words that are prefixes of s.\n\n**Iterate through Words :** Loop through each word in the words array.\n\n**Check Prefix :**\n- For each word, determine its length n.\n- Extract the first n characters of the string s using substr(0, n).\n- Compare this substring with the word. If they are equal, increment the counter c.\n\n**Return Result :** After checking all the words, return the counter c as the final result.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int c=0;\n for(int i=0;i<words.size();i++){\n int n=words[i].size();\n string a=s.substr(0,n);\n if (words[i]==a) c++;\n }\n return c;\n \n }\n};\n``` | 6 | 0 | ['String', 'C++'] | 1 |
count-prefixes-of-a-given-string | 3 different fastest approaches with javascript - including one-liner | 3-different-fastest-approaches-with-java-6yk0 | // approach 1 \n\n\nvar countPrefixes = function(words, s) {\n let counter = 0\n for (let i = 0; i < words.length; i++) {\n if (words[i]) === s. | Apoorv-123 | NORMAL | 2022-08-22T00:15:17.242709+00:00 | 2022-08-22T00:22:25.549321+00:00 | 679 | false | // approach 1 \n\n```\nvar countPrefixes = function(words, s) {\n let counter = 0\n for (let i = 0; i < words.length; i++) {\n if (words[i]) === s.slice(0,words[i].length)) {\n counter++\n }\n }\n return counter\n};\n```\n\n// one liner approach - 2\n\n```\nvar countPrefixes = function(words, s) {\n return words.filter((word) => word === s.slice(0,word.length)).length;\n};\n```\n\n// approach 3\n\n```\nvar countPrefixes = function(words, s) {\n return words.filter(data=> s.indexOf(data) == 0).length;\n};\n``` | 6 | 0 | ['JavaScript'] | 1 |
count-prefixes-of-a-given-string | JS easiest way | js-easiest-way-by-andrezzz-acee | \nvar countPrefixes = function(words, s) {\n \n let cont = 0;\n \n for(i = 0; i < words.length; i++){\n for(j = 1; j <= s.length; j++){\n | andrezzz | NORMAL | 2022-05-04T18:10:02.585610+00:00 | 2022-05-04T18:10:02.585649+00:00 | 547 | false | ```\nvar countPrefixes = function(words, s) {\n \n let cont = 0;\n \n for(i = 0; i < words.length; i++){\n for(j = 1; j <= s.length; j++){\n if(words[i] == s.slice(0, j)){\n cont++;\n }\n } \n }\n return cont;\n \n};\n``` | 6 | 0 | ['JavaScript'] | 0 |
count-prefixes-of-a-given-string | Python straightforward with startswith. | python-straightforward-with-startswith-b-uw5f | \ndef countPrefixes(self, words: List[str], s: str) -> int:\n\n c = 0\n for w in words:\n if s.startswith(w):\n c += 1\n\n return c\n | vineeth_moturu | NORMAL | 2022-04-30T16:08:57.925966+00:00 | 2022-04-30T16:08:57.926001+00:00 | 588 | false | ```\ndef countPrefixes(self, words: List[str], s: str) -> int:\n\n c = 0\n for w in words:\n if s.startswith(w):\n c += 1\n\n return c\n``` | 6 | 0 | ['Python'] | 3 |
count-prefixes-of-a-given-string | Beats 100% of users with C++ || Step By Step Explain || Easy to Understand || | beats-100-of-users-with-c-step-by-step-e-bcxc | Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it....\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this problem | abhirajpratapsingh | NORMAL | 2024-02-05T15:29:05.835690+00:00 | 2024-02-05T15:29:05.835725+00:00 | 229 | false | # Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it....\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem involves counting the number of words in a vector that have a specific prefix equal to a given string s.\n\n---\n\n\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n- The class Solution contains a member function countPrefixes.\n- It iterates through each word in the vector.\n- Compares characters at corresponding positions in the word and the given string s.\n- If a mismatch is found or the end of the word is reached, move to the next word.\n- If the end of the word is reached, increment the count, indicating a match.\n\n\n---\n\n# Complexity\n\n- **Time complexity:** O(n*m), where n is the number of words and m is the length of the strings.\n\n- **Space complexity:** O(1), as the algorithm uses a constant amount of space regardless of the input size.\n\n\n---\n\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s)\n {\n string w = "" ;\n int count = 0 ;\n for(int i=0 ; i<words.size() ; i++)\n {\n w = words[i] ;\n int j ;\n for(j=0 ; j<w.size() ; j++)\n {\n if(w[j]!=s[j])\n break ;\n }\n if(j==w.size())\n count++ ;\n } \n return count ;\n }\n};\n```\n\n---\n# if you like the solution please UPVOTE it \n\n\n\n\n\n----\n---\n | 5 | 0 | ['Array', 'String', 'String Matching', 'C++'] | 1 |
count-prefixes-of-a-given-string | Java || 100% beats || 2 solutions || ❤️❤️ | java-100-beats-2-solutions-by-saurabh_ku-kjc1 | 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 | saurabh_kumar1 | NORMAL | 2023-08-24T18:56:09.555301+00:00 | 2023-08-24T18:56:09.555328+00:00 | 479 | 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:0(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count = 0;\n for (String i : words)\n if (s.startsWith(i)) count++;\n return count;\n\n\n// Another solution\n\n // int count = 0;\n // int low = 0;\n // for(int i=1; i<=s.length(); i++){\n // for(int j=0; j<words.length; j++){\n // if(words[j].equals(s.substring(low,i))){\n // count++;\n // } \n // }\n // }\n // return count;\n } \n}\n``` | 5 | 0 | ['String', 'String Matching', 'Java'] | 0 |
count-prefixes-of-a-given-string | C++ | 3-Line solution | c-3-line-solution-by-vartika2207-wlj1 | \tclass Solution {\n\tpublic:\n\t\t//time:O(n), space: O(1)\n\t\tint countPrefixes(vector& words, string s) {\n\t\t\tint count = 0;\n\t\t\tfor(auto wd : words)\ | vartika2207 | NORMAL | 2022-11-30T22:07:23.519381+00:00 | 2022-11-30T22:07:23.519415+00:00 | 697 | false | \tclass Solution {\n\tpublic:\n\t\t//time:O(n), space: O(1)\n\t\tint countPrefixes(vector<string>& words, string s) {\n\t\t\tint count = 0;\n\t\t\tfor(auto wd : words)\n\t\t\t\tif(s.find(wd) < 1) count++; //if first occurrence of sub-string is in the specified string\n\t\t\treturn count;\n\t\t}\n\t}; | 5 | 0 | ['C', 'C++'] | 2 |
count-prefixes-of-a-given-string | C++ Simple Solution using substr() | c-simple-solution-using-substr-by-prasad-uxjd | \nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int n = words.size();\n int cnt = 0;\n for(int i=0; | Prasad264 | NORMAL | 2022-04-30T16:35:11.347911+00:00 | 2022-04-30T16:35:11.347949+00:00 | 426 | false | ```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int n = words.size();\n int cnt = 0;\n for(int i=0; i<n; i++) {\n string str = words[i];\n if(str == s.substr(0, str.size())) cnt++;\n }\n return cnt;\n }\n};\n``` | 5 | 0 | ['String', 'C'] | 3 |
count-prefixes-of-a-given-string | Java || Easy Appraoch | java-easy-appraoch-by-ashwinj_984-orxi | \nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int i = 0;\n int j = 0;\n int count = 0;\n for(int k | ashwinj_984 | NORMAL | 2022-04-30T16:08:54.194604+00:00 | 2022-04-30T16:08:54.194656+00:00 | 768 | false | ```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int i = 0;\n int j = 0;\n int count = 0;\n for(int k = 0; k < words.length; k++){\n if(words[k].length() > s.length()){\n continue;\n }\n \n while(i < words[k].length() && words[k].charAt(i) == s.charAt(j)){\n i++;\n j++;\n }\n if(i == words[k].length()){\n count++;\n }\n i = 0;\n j = 0;\n }\n return count;\n }\n}\n``` | 5 | 0 | ['String', 'Java'] | 0 |
count-prefixes-of-a-given-string | Best Solution Using set || Easy to Understand | best-solution-using-set-easy-to-understa-d8fu | Approach\nStep 1 : Define a map and empty stirng\nStep2 : insert prefixes in map\nStep3 : check how many array of words element is present in map\n\n\n\nclass S | ParthBabbar | NORMAL | 2022-04-30T16:06:30.606400+00:00 | 2022-04-30T16:15:36.091331+00:00 | 283 | false | Approach\nStep 1 : Define a map and empty stirng\nStep2 : insert prefixes in map\nStep3 : check how many array of words element is present in map\n\n\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n unordered_set<string> mp;\n string st = "";\n for(int i=0;i<s.length();i++){\n st += s[i];\n mp.insert(st);\n }\n int k=0;\n for(auto it:words){\n if(mp.count(it)){\n k++;\n }\n }\n return k;\n }\n};\n```\n\n# ****PLEASE UPVOTE | 5 | 0 | ['String', 'C'] | 0 |
count-prefixes-of-a-given-string | python3 code | python3-code-by-blackhole_tiub-c62q | Code\n\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n return len([word for word in words if s.startswith(word)])\n\n\n | Little_Tiub | NORMAL | 2022-12-28T23:53:42.204520+00:00 | 2023-05-02T05:51:06.502800+00:00 | 271 | false | # Code\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n return len([word for word in words if s.startswith(word)])\n```\n\n\n | 4 | 1 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | 1 line of code🔥100% faster code 🔥Beginner friendly🔥 | 1-line-of-code100-faster-code-beginner-f-2vf7 | Please UPVOTE if helps.\n# Complexity\n- Time complexity: O(n^2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space comple | diyordev | NORMAL | 2023-12-07T16:10:53.721069+00:00 | 2023-12-07T16:10:53.721101+00:00 | 191 | false | # Please UPVOTE if helps.\n# Complexity\n- Time complexity: $$O(n^2)$$\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 {\n public int countPrefixes(String[] words, String s) {;\n int count = 0; for (String w: words) if (s.startsWith(w)) count++;\n return count; \n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | Java Solution|| Beats 100%💯 || 3 line code🔥 | java-solution-beats-100-3-line-code-by-v-cqx1 | \n# Approach\n Describe your approach to solving the problem. \n- The startsWith method returns true if the string it is called on starts with the provided pref | Vishu6403 | NORMAL | 2023-10-09T09:11:03.521445+00:00 | 2023-10-09T09:11:03.521471+00:00 | 151 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\n- The startsWith method returns true if the string it is called on starts with the provided prefix (string s), otherwise it returns 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 {\n public int countPrefixes(String[] words, String s) {\n int count=0;\n for(int i=0;i<words.length;i++){\n if(s.startsWith(words[i])){\n count++;\n }\n }\n return count;\n }\n}\n``` | 3 | 0 | ['Array', 'String', 'Java'] | 1 |
count-prefixes-of-a-given-string | one line solution | one-line-solution-by-yuliia_anatoliivna-nvsv | \n\n# Code\n\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n return sum(1 for x in words if s.startswith(x))\n | Yuliia_Anatoliivna | NORMAL | 2023-05-31T17:17:11.986423+00:00 | 2023-05-31T17:17:11.986471+00:00 | 298 | false | \n\n# Code\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n return sum(1 for x in words if s.startswith(x))\n``` | 3 | 0 | ['Python3'] | 1 |
count-prefixes-of-a-given-string | Simple | Easy | (100%) JAVA Solution | simple-easy-100-java-solution-by-pranavi-cwy0 | \n\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count=0;\n for(String word: words){\n if(s.startsW | pranavibhute | NORMAL | 2023-05-17T12:05:54.210279+00:00 | 2023-05-17T13:13:31.425017+00:00 | 558 | false | \n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count=0;\n for(String word: words){\n if(s.startsWith(word)) count++;\n }\n return count;\n }\n}\n``` | 3 | 0 | ['String', 'Counting', 'Java'] | 0 |
count-prefixes-of-a-given-string | Simple Using Substr | C++ | simple-using-substr-c-by-mankaran_07-g3ym | Please Upvote If you Like the Solution !!!\n# Code\n\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int ans = 0;\ | mankaran_07 | NORMAL | 2023-04-22T10:16:10.736757+00:00 | 2023-04-22T10:16:10.736804+00:00 | 400 | false | `Please Upvote If you Like the Solution !!!`\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int ans = 0;\n for(auto &w : words) {\n int n = w.size();\n if(s.substr(0,n) == w) ans++;\n }\n return ans;\n }\n};\n``` | 3 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | ✅ Easy and Fast C++ Solution | easy-and-fast-c-solution-by-adarsh_shukl-1kdx | Approach\nUsing s.find(words[i]) function which returns the index of words[i] if it is present in the string s , and to check whether words[i] occurs as a prefi | Adarsh_Shukla | NORMAL | 2022-12-22T12:55:35.040102+00:00 | 2022-12-22T12:56:40.386175+00:00 | 605 | false | # Approach\nUsing **s.find(words[i])** function which returns the index of words[i] if it is present in the string s , and to check whether words[i] occurs as a prefix use **s.find(words[i])==0** ( since prefix always occurs at index 0).\n\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n\n int countPrefix = 0;\n\n for(int i=0;i<words.size();++i){\n if(s.find(words[i])!=string::npos && s.find(words[i])==0){\n countPrefix++;\n }\n } \n return countPrefix;\n }\n};\n``` | 3 | 0 | ['C++'] | 1 |
count-prefixes-of-a-given-string | Easy O(N) | Maintain a Set | easy-on-maintain-a-set-by-vkadu68-klwj | We maintian a set called seen and add all the prefixes in the set and then simply check if the word in word list is present in the set or not\n\nLeave an upvote | vkadu68 | NORMAL | 2022-11-01T21:29:24.471481+00:00 | 2022-11-01T21:29:24.471515+00:00 | 388 | false | We maintian a set called seen and add all the prefixes in the set and then simply check if the word in word list is present in the set or not\n\n***Leave an upvote if this helps!!!***\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n seen=set()\n count=0\n for i in range(1,len(s)+1):\n seen.add(s[:i])\n for i in words:\n if i in seen:\n count+=1\n return count\n``` | 3 | 0 | ['Ordered Set', 'Python'] | 0 |
count-prefixes-of-a-given-string | Easy Python solution | easy-python-solution-by-vistrit-igdd | \ndef countPrefixes(self, words: List[str], s: str) -> int:\n c=0\n for i in words:\n l=len(i)\n if i==s[:l]:\n | vistrit | NORMAL | 2022-05-17T18:53:59.607539+00:00 | 2022-05-17T18:53:59.607581+00:00 | 389 | false | ```\ndef countPrefixes(self, words: List[str], s: str) -> int:\n c=0\n for i in words:\n l=len(i)\n if i==s[:l]:\n c+=1\n return c\n``` | 3 | 0 | ['Python', 'Python3'] | 0 |
count-prefixes-of-a-given-string | C++ online with std::count_if and string::rfind | c-online-with-stdcount_if-and-stringrfin-jv4m | This stackover flow post descrives how to you string::rfind to implement startsWith, which leads to following over all code to solve to problem.\n\n\nint countP | heder | NORMAL | 2022-04-30T18:26:54.995781+00:00 | 2022-08-10T18:24:35.411643+00:00 | 93 | false | This [stackover flow post](https://stackoverflow.com/questions/1878001/how-do-i-check-if-a-c-stdstring-starts-with-a-certain-string-and-convert-a) descrives how to you ```string::rfind``` to implement ```startsWith```, which leads to following over all code to solve to problem.\n\n```\nint countPrefixes(const vector<string>& ws, const string& s) {\n return count_if(begin(ws), end(ws), [&](const string& w) { return s.rfind(w, 0) == 0; });\n}\n```\n\nOne might was well could use ```string::compare``` to solve the problem as well:\n\n```\nint countPrefixes(const vector<string>& ws, const string& s) {\n return count_if(begin(ws), end(ws), [&](const string& w) {\n return s.compare(0, size(w), w) == 0;\n });\n}\n``` | 3 | 0 | ['C'] | 0 |
count-prefixes-of-a-given-string | C++ || 5 Lines Code || Very Easy O(N) Solution | c-5-lines-code-very-easy-on-solution-by-cwkyx | \nint countPrefixes(vector<string>& words, string s) {\n int c=0;\n for(string str:words){\n int len=str.length();\n string sub | Gagan-Chaudhary | NORMAL | 2022-04-30T17:53:41.019322+00:00 | 2022-04-30T17:53:41.019365+00:00 | 130 | false | ```\nint countPrefixes(vector<string>& words, string s) {\n int c=0;\n for(string str:words){\n int len=str.length();\n string subs=s.substr(0,len);\n if(str==subs){\n c++;\n }\n } \n return c;\n }\n``` | 3 | 0 | ['String', 'C', 'Iterator', 'C++'] | 1 |
count-prefixes-of-a-given-string | java | java-by-sakshi2602-c4cs | int ans=0;\n HashMap l=new HashMap<>();\n for(String str:words)\n l.put(str,l.getOrDefault(str,0)+1);\n for(int i=0;i<=s.length( | sakshi2602 | NORMAL | 2022-04-30T16:13:44.527942+00:00 | 2022-04-30T16:15:48.564339+00:00 | 288 | false | int ans=0;\n HashMap<String,Integer> l=new HashMap<>();\n for(String str:words)\n l.put(str,l.getOrDefault(str,0)+1);\n for(int i=0;i<=s.length();i++)\n {\n String st=s.substring(0,i);\n if(l.containsKey(st))\n ans=ans+l.get(st);\n }\n return ans; | 3 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | C++ || EASY TO UNDERSTAND || Simple Solution | c-easy-to-understand-simple-solution-by-xz3qm | \nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int c=0;\n for(auto word:words)\n {\n | aarindey | NORMAL | 2022-04-30T16:02:53.460367+00:00 | 2022-04-30T16:02:53.460401+00:00 | 135 | false | ```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int c=0;\n for(auto word:words)\n {\n string str=word;\n int l=word.size();\n string s2=s.substr(0,l);\n if(s2==word)\n c++;\n }\n return c;\n }\n};\n``` | 3 | 1 | [] | 0 |
count-prefixes-of-a-given-string | 7 Liner javaScript Easy Solution | 7-liner-javascript-easy-solution-by-ali-6tsvl | Code\njavascript []\n/**\n * @param {string[]} words\n * @param {string} s\n * @return {number}\n */\nvar countPrefixes = function (words, s) {\n let count = 0 | ALI-786 | NORMAL | 2024-08-27T12:29:58.729179+00:00 | 2024-08-27T12:29:58.729206+00:00 | 49 | false | # Code\n```javascript []\n/**\n * @param {string[]} words\n * @param {string} s\n * @return {number}\n */\nvar countPrefixes = function (words, s) {\n let count = 0;\n for (let i = 0; i < words.length; i++) {\n if (s.startsWith(words[i])) {\n count++;\n }\n }\n return count;\n};\n``` | 2 | 0 | ['JavaScript'] | 0 |
count-prefixes-of-a-given-string | simplest one PYTHON | simplest-one-python-by-justingeorge-2hv1 | 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 | justingeorge | NORMAL | 2024-03-05T08:18:29.839533+00:00 | 2024-03-05T08:18:29.839560+00:00 | 216 | 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)$$ -->\n\n# Code\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n \n count=0\n for i in words:\n l=len(i)\n if s[:l] == i:\n count=count+1\n return count\n``` | 2 | 0 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | Very easy logic , for beginners !! | very-easy-logic-for-beginners-by-user120-2rdh | Intuition\nInitialize a variable count to store the count of prefixes found in the string s.\nIterate through each word in the words array.\nFor each word encou | user1203oJ | NORMAL | 2024-02-24T06:22:53.272079+00:00 | 2024-02-24T06:22:53.272139+00:00 | 520 | false | # Intuition\nInitialize a variable count to store the count of prefixes found in the string s.\nIterate through each word in the words array.\nFor each word encountered, check if the string s starts with that word using the startsWith method.\nIf the word is found to be a prefix of s, increment the count.\nAfter iterating through all words in the words array, count holds the total number of prefixes found in the string s.\nReturn the count as the result.\n# Code\n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count=0;\n for(int i=0;i<words.length;i++){\n if(s.startsWith(words[i])){\n count++;\n }\n }return count;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | Simple Solution | simple-solution-by-anshika_73-tgte | \n\n# Code\n\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int count =0;\n for(auto str : words){\n | Anshika_73_ | NORMAL | 2023-12-04T13:21:28.114548+00:00 | 2023-12-04T13:21:28.114588+00:00 | 92 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int count =0;\n for(auto str : words){\n int len = str.length();\n auto subs = s.substr(0,len);\n if(str == subs){\n count++;\n }\n }\n return count;\n }\n};\n``` | 2 | 0 | ['C++'] | 2 |
count-prefixes-of-a-given-string | Easy one-liner C# | easy-one-liner-c-by-rxcore-v57d | \n# Approach\nUsing Count and StartsWith\n\n# Code\n\npublic class Solution {\n public int CountPrefixes(string[] words, string s) {\n return words.Co | rxcore | NORMAL | 2023-09-26T17:11:18.965491+00:00 | 2023-09-26T17:11:18.965518+00:00 | 38 | false | \n# Approach\nUsing Count and StartsWith\n\n# Code\n```\npublic class Solution {\n public int CountPrefixes(string[] words, string s) {\n return words.Count(i => s.StartsWith(i));\n }\n}\n``` | 2 | 0 | ['C#'] | 0 |
count-prefixes-of-a-given-string | Javascript easy one line solution | javascript-easy-one-line-solution-by-bri-5ylw | Complexity\n- Time complexity: O(n*m)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n) \n\n# | brijesh178 | NORMAL | 2023-08-28T16:53:55.875454+00:00 | 2023-08-28T16:53:55.875475+00:00 | 209 | false | # Complexity\n- Time complexity: O(n*m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * @param {string[]} words\n * @param {string} s\n * @return {number}\n */\nvar countPrefixes = function (words, s) {\n return words.filter((item) => s.startsWith(item)).length\n}\n``` | 2 | 0 | ['JavaScript'] | 0 |
count-prefixes-of-a-given-string | Go easy | go-easy-by-ganesh_raveendran-kw1n | \n\n# Code\n\nfunc countPrefixes(words []string, s string) int {\n\tvar count int\n\n\tfor _, v := range words {\n\t\tif strings.HasPrefix(s, v) {\n\t\t\tcount+ | ganesh_raveendran | NORMAL | 2023-08-16T11:56:07.177539+00:00 | 2023-08-16T11:56:07.177556+00:00 | 16 | false | \n\n# Code\n```\nfunc countPrefixes(words []string, s string) int {\n\tvar count int\n\n\tfor _, v := range words {\n\t\tif strings.HasPrefix(s, v) {\n\t\t\tcount++\n\t\t}\n\t}\n\n\treturn count\n}\n``` | 2 | 0 | ['Go'] | 0 |
count-prefixes-of-a-given-string | Python one line solution | python-one-line-solution-by-azazellooo-blf6 | \n\n# Code\n\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n return len([i for i in words if s.startswith(i)])\n | azazellooo | NORMAL | 2023-07-28T07:01:57.409952+00:00 | 2023-07-28T07:01:57.409975+00:00 | 205 | false | \n\n# Code\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n return len([i for i in words if s.startswith(i)])\n``` | 2 | 0 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | Easy to understand | easy-to-understand-by-bhaskarkumar07-wius | \n\n# Approach\n Describe your approach to solving the problem. \n if it starts with element present in array count++;\n# Complexity\n- Time complexity:O | bhaskarkumar07 | NORMAL | 2023-04-24T15:26:33.969584+00:00 | 2023-04-24T15:26:33.969610+00:00 | 690 | false | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n if it starts with element present in array count++;\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 {\n public int countPrefixes(String[] words, String s) {\n int count=0;\n\n for(String word: words){\n if(s.startsWith(word)) count++;\n }\n return count;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | simple java 3 line code | simple-java-3-line-code-by-bhupendra0820-j0pm | 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 | bhupendra082002 | NORMAL | 2023-03-31T16:28:23.926044+00:00 | 2023-03-31T16:28:23.926078+00:00 | 311 | 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)$$ -->\n\n# Code\n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count = 0;\n for(int i =0; i<words.length; i++){\n if(s.startsWith(words[i])) count++;\n }\n return count;\n \n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | Esay Java Solution | esay-java-solution-by-md_ashik786-epfi | 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 | MD_Ashik786 | NORMAL | 2023-03-25T01:52:40.291810+00:00 | 2023-03-25T01:52:40.291850+00:00 | 593 | 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)$$ -->\n\n# Code\n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int c=0;\n while(s.length()!=0){\n for(String y:words){\n if(y.equals(s))\n c++;\n }\n s=s.substring(0,s.length()-1); //Decreasing the length by 1\n }\n return c;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | C++ || Easy || Simple | c-easy-simple-by-naoman-l9zb | \n\n# Code\n\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int count = 0;\n for (string word : words)\n | Naoman | NORMAL | 2023-03-10T05:38:08.079838+00:00 | 2023-03-10T05:38:08.079884+00:00 | 725 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int count = 0;\n for (string word : words)\n {\n if(s.find(word) == 0) \n { \n count++;\n }\n }\n return count;\n }\n};\n\n``` | 2 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | Pyhton3 oneliner using startswith() | pyhton3-oneliner-using-startswith-by-ami-hnjc | \n\n# Code\n\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n count = 0\n for i in words:\n if s.start | amitpandit03 | NORMAL | 2023-03-03T01:46:14.379198+00:00 | 2023-03-03T01:46:14.379236+00:00 | 59 | false | \n\n# Code\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n count = 0\n for i in words:\n if s.startswith(i):\n count += 1\n return count\n\n #One Liner\n\n return sum(1 for i in words if s.startswith(i))\n``` | 2 | 0 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | ✔Simple C++ code || Easy || beats 100% ✌ | simple-c-code-easy-beats-100-by-kraken_0-8ae4 | \n\nRuntime: Beats 100%\n\n# Code\n\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int c=0;\n for(auto j:w | Kraken_02 | NORMAL | 2023-02-12T16:02:40.108526+00:00 | 2023-02-12T16:02:40.108564+00:00 | 545 | false | \n\nRuntime: Beats 100%\n\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int c=0;\n for(auto j:words){\n int b=1;\n for(int i=0;i<j.size();i++){\n if(j[i]!=s[i]){\n b=0;\n break;\n }\n }\n if(b==1){\n c++;\n }\n }\n return c;\n }\n};\n```\nHope you liked the implementation of the code, if you like it feel free to upvote \uD83D\uDC4D | 2 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | Pooping solution :) | pooping-solution-by-kaushikp0908-tlra | Approach\n Describe your approach to solving the problem. \nPoop the loop\n\n# Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n | kaushikp0908 | NORMAL | 2023-02-06T07:16:53.484396+00:00 | 2023-02-06T07:16:53.484436+00:00 | 152 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nPoop the loop\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n c = 0\n for i in words:\n if i in s[:len(i)]:\n c+=1\n return c \n``` | 2 | 0 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | Easy solution | easy-solution-by-shubhanjalisingh_001-o4nk | 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 | ShubhanjaliSingh_001 | NORMAL | 2022-12-02T04:38:44.407793+00:00 | 2022-12-02T04:38:44.407830+00:00 | 307 | 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)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s)\n {\n string w = "" ;\n int count = 0 ;\n for(int i=0 ; i<words.size() ; i++)\n {\n w = words[i] ;\n int j ;\n for(j=0 ; j<w.size() ; j++)\n {\n if(w[j]!=s[j])\n {\n break ;\n }\n }\n if(j==w.size())\n {\n count++ ;\n }\n } \n return count ;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | C++|| Easy Approach || 93% Faster | c-easy-approach-93-faster-by-spects-acul-jdd8 | \nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) { \n int ans = 0;\n for(int i = 0 ; i<words.size() ; i++){\n | spects-acular | NORMAL | 2022-10-14T06:51:36.663268+00:00 | 2022-10-14T06:51:36.663310+00:00 | 309 | false | ```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) { \n int ans = 0;\n for(int i = 0 ; i<words.size() ; i++){\n string element = words[i];\n int len = element.length();\n \n for(int j = 0 ; j<len ; j++){\n \n if(element[j] != s[j]){\n break; \n } \n if(j==len-1){\n ans++;\n }\n }\n }\n return ans;\n }\n};\n``` | 2 | 0 | [] | 1 |
count-prefixes-of-a-given-string | ✅ 'startsWith()' in Java | Java | startswith-in-java-java-by-sayakmondal-5z5x | \nif(you like)\n\tplease upvote;\n\n\n\n# Java Code\n\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int cnt = 0;\n | Sayakmondal | NORMAL | 2022-10-02T16:18:18.031088+00:00 | 2022-10-02T16:23:48.320824+00:00 | 311 | false | ```\nif(you like)\n\tplease upvote;\n```\n\n\n# Java Code\n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int cnt = 0;\n for(String str:words) \n {\n if(s.startsWith(str) == true)\n cnt++;\n }\n return cnt;\n }\n}\n``` | 2 | 0 | ['String', 'Java'] | 0 |
count-prefixes-of-a-given-string | python solution | python-solution-by-dugu0607-mfk9 | \tclass Solution:\n\t\tdef countPrefixes(self, words: List[str], s: str) -> int:\n\t\t\tres = 0\n\t\t\tfor word in words:\n\t\t\t\tif s[:len(word)] == word: res | Dugu0607 | NORMAL | 2022-10-02T06:20:02.825221+00:00 | 2022-10-02T06:20:02.825265+00:00 | 371 | false | \tclass Solution:\n\t\tdef countPrefixes(self, words: List[str], s: str) -> int:\n\t\t\tres = 0\n\t\t\tfor word in words:\n\t\t\t\tif s[:len(word)] == word: res += 1\n\t\t\treturn res | 2 | 0 | [] | 0 |
count-prefixes-of-a-given-string | python ||O(N) | python-on-by-sneh713-aiva | Time Complexcity O(N)\nspace Complexcity O(1)\n\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n count=0\n for wo | Sneh713 | NORMAL | 2022-09-26T12:53:29.158577+00:00 | 2022-09-26T12:53:29.158614+00:00 | 905 | false | Time Complexcity O(N)\nspace Complexcity O(1)\n```\nclass Solution:\n def countPrefixes(self, words: List[str], s: str) -> int:\n count=0\n for word in words:\n n=len(word)\n if s[0:n]==word:\n count+=1\n return count\n \n``` | 2 | 0 | ['String', 'Python3'] | 1 |
count-prefixes-of-a-given-string | Python straight forward one-liner | python-straight-forward-one-liner-by-trp-k5e4 | Here is what I did:\n\npython\n def countPrefixes(self, words: List[str], s: str) -> int:\n return sum(s.startswith(word) for word in words)\n | trpaslik | NORMAL | 2022-09-12T10:52:52.174389+00:00 | 2022-09-12T10:52:52.174434+00:00 | 330 | false | Here is what I did:\n\n```python\n def countPrefixes(self, words: List[str], s: str) -> int:\n return sum(s.startswith(word) for word in words)\n``` | 2 | 0 | ['Python'] | 1 |
count-prefixes-of-a-given-string | ✅ [JavaScript] Straightforward Solution || Simple & Understandable || Fast | javascript-straightforward-solution-simp-wt1s | Runtime: 68 ms, faster than 91.85% of JavaScript online submissions for Count Prefixes of a Given String.\nMemory Usage: 43 MB, less than 40.00% of JavaScript o | PoeticIvy302543 | NORMAL | 2022-07-03T00:48:15.488151+00:00 | 2022-07-25T21:32:49.484404+00:00 | 121 | false | **Runtime: 68 ms, faster than 91.85% of JavaScript online submissions for Count Prefixes of a Given String.\nMemory Usage: 43 MB, less than 40.00% of JavaScript online submissions for Count Prefixes of a Given String.**\n\n```\nvar countPrefixes = function(words, s) {\n let count = 0\n for (i of words) {\n if (s.startsWith(i)) {\n count++\n }\n } \n return count\n};\n``` | 2 | 0 | ['String', 'JavaScript'] | 0 |
count-prefixes-of-a-given-string | C# LINQ One-line | c-linq-one-line-by-aip-s5vd | \n\tpublic int CountPrefixes(string[] words, string s) \n => words.Count(w => s.StartsWith(w)); \n | AIP | NORMAL | 2022-05-04T08:59:50.653397+00:00 | 2022-05-04T08:59:50.653429+00:00 | 76 | false | ```\n\tpublic int CountPrefixes(string[] words, string s) \n => words.Count(w => s.StartsWith(w)); \n``` | 2 | 0 | [] | 0 |
count-prefixes-of-a-given-string | Rust solutions | rust-solutions-by-bigmih-3tuw | Simle use of starts_with:\n\nimpl Solution {\n pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {\n words.iter().filter(|w| s.starts_with(w | BigMih | NORMAL | 2022-05-01T09:25:16.219701+00:00 | 2022-05-01T09:25:16.219732+00:00 | 64 | false | 1. Simle use of `starts_with`:\n```\nimpl Solution {\n pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {\n words.iter().filter(|w| s.starts_with(w.as_str())).count() as _\n }\n}\n```\n2. Slice comparison\n```\nimpl Solution {\n pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {\n words\n .iter()\n .filter(|w| w.len() <= s.len() && w.as_str() == &s[..w.len()])\n .count() as _\n }\n}\n```\n3. Functional solution with `zip`:\n```\nimpl Solution {\n pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {\n words\n .iter()\n .filter(|w| w.len() <= s.len() && w.chars().zip(s.chars()).all(|(a, b)| a == b))\n .count() as _\n }\n}\n``` | 2 | 0 | ['Rust'] | 0 |
count-prefixes-of-a-given-string | Go golang solution | go-golang-solution-by-leaf_peng-rxt2 | go\nfunc countPrefixes(words []string, s string) int {\n ans := 0\n for _, word := range words {\n n := len(word)\n if n > len(s) { continue | leaf_peng | NORMAL | 2022-05-01T01:08:25.469918+00:00 | 2022-05-01T01:08:50.166341+00:00 | 73 | false | ```go\nfunc countPrefixes(words []string, s string) int {\n ans := 0\n for _, word := range words {\n n := len(word)\n if n > len(s) { continue }\n if word == s[:n] { ans++ }\n }\n return ans\n}\n``` | 2 | 0 | ['Go'] | 0 |
count-prefixes-of-a-given-string | C++ very easy to Understand | solution using unordered set | c-very-easy-to-understand-solution-using-du23 | ```\nclass Solution {\npublic:\n int countPrefixes(vector& words, string s) {\n unordered_set st;\n int size = s.size();\n for(int i = 0 | Ashishks0211 | NORMAL | 2022-04-30T18:46:16.772904+00:00 | 2022-04-30T18:46:16.772935+00:00 | 55 | false | ```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n unordered_set<string> st;\n int size = s.size();\n for(int i = 0;i<size;i++){\n string temp = "";\n for(int j = 0;j<=i;j++){\n temp+=s[j];\n }\n st.insert(temp);\n }\n int ans = 0;\n for(int i = 0;i<words.size();i++){\n if(st.find(words[i])!=st.end())\n ans++;\n }\n return ans;\n }\n}; | 2 | 0 | ['C'] | 0 |
count-prefixes-of-a-given-string | 📌 [JAVA] | Simple | java-simple-by-kanojiyakaran-y91t | ```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n \n int preFixCount=0;\n for(String curr:words){\n int | kanojiyakaran | NORMAL | 2022-04-30T16:00:56.377246+00:00 | 2022-04-30T16:03:22.896520+00:00 | 345 | false | ```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n \n int preFixCount=0;\n for(String curr:words){\n int i=0;\n \n if(curr.length()>s.length()) // if current string length greater than s length invalid prefix\n continue; \n int j;\n for(j=0;j<curr.length();j++,i++){\n if(curr.charAt(j)==s.charAt(i))\n continue;\n else \n break;\n \n }\n if(j==curr.length()) // if all character matches than mark valid \n preFixCount++;\n }\n return preFixCount; \n }\n \n} | 2 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | Optimized Set-Based Solution | O(n + m) Time | Python 3 | optimized-set-based-solution-on-m-time-p-hkk1 | IntuitionWe need to count how many words in words are prefixes of the string s. A prefix is any substring that starts from the beginning of s. Instead of checki | NRSingh | NORMAL | 2025-04-03T19:23:59.393721+00:00 | 2025-04-03T19:23:59.393721+00:00 | 19 | false | # Intuition
We need to count how many words in words are prefixes of the string s. A prefix is any substring that starts from the beginning of s. Instead of checking each word against s directly (which would be inefficient), we can precompute all valid prefixes of s and store them in a set for quick lookup.
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
1. Generate all prefixes of s:
* A prefix of s is any substring s[:i], where i ranges from 1 to len(s).
* We store these prefixes in a set to allow quick O(1) lookup.
2. Count the words that exist in the prefix set:
* For each word in words, check if it exists in the prefix_set.
* Use a simple sum() function to count the number of matches.
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(n+m)
* Generating prefixes: O(n) (since we extract s[:i] substrings and insert them into a set).
* Checking membership for each word: O(m) (since each lookup in a set is O(1) on average).
* Total Complexity: O(n+m).
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(n)
* Prefix set storage: O(n) (storing at most n substrings).
* No extra space apart from input storage.
* Total Complexity: O(n).
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
prefix_set = {s[:i] for i in range(1, len(s) + 1)}
return sum(1 for word in words if word in prefix_set)
```
 | 1 | 0 | ['Array', 'String', 'Python3'] | 0 |
count-prefixes-of-a-given-string | Count Prefixes of a Given String (Optimal Approach) | count-prefixes-of-a-given-string-optimal-mi26 | IntuitionApproachGo through the array and check if the each element starting matches the prefix word.Complexity
Time complexity:
O(n)
Space complexity:
O(1)Code | Vinil07 | NORMAL | 2025-04-01T05:22:03.798635+00:00 | 2025-04-01T05:22:03.798635+00:00 | 19 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
Go through the array and check if the each element starting matches the prefix word.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
# Code
```cpp []
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int count=0;
for(string val:words){
if(s.find(val)==0) count++;
}
return count;
}
};
``` | 1 | 0 | ['Array', 'String', 'C++'] | 0 |
count-prefixes-of-a-given-string | 0MS || Beats 100% || Easy and Simple Java Solution | 0ms-beats-100-easy-and-simple-java-solut-g2d7 | 🔍 Intuition:
We need to count how many strings in thewordsarray areprefixesof strings.
Aprefixmeans the string appears at thebeginningofs.
Example Understanding | Yashvendra | NORMAL | 2025-02-20T16:13:35.467778+00:00 | 2025-02-20T16:13:35.467778+00:00 | 100 | false | ## 🔍 Intuition:
- We need to count how many strings in the `words` array are **prefixes** of string `s`.
- A **prefix** means the string appears at the **beginning** of `s`.
- **Example Understanding:**
- **Input:** `words = ["a", "b", "ab", "abc"]`, `s = "abc"`
- `"a"`, `"ab"`, and `"abc"` are **prefixes** of `"abc"`, so the answer is `3`.
---
## 🛠️ Approach:
1. **Iterate through each word** in `words`.
2. **Use `indexOf()`** to check if `s.indexOf(str) == 0`:
- If true, it means the word appears at the start of `s` (is a prefix).
- Increase the `count`.
3. **Return the final count**.
---
## ⏱️ Complexity Analysis:
- **Time Complexity:**
- Checking `s.indexOf(str)` takes **O(M)** (where `M` is the length of `str`).
- We do this for each word in `words`, so **O(N * M)** (where `N` is the number of words).
- **Space Complexity:**
- **O(1)** → Only integer variables are used for counting.
# Code
```java []
class Solution {
public int countPrefixes(String[] words, String s) {
int count = 0;
for(String str : words){
if(s.indexOf(str)==0)
count++;
}
return count;
}
}
``` | 1 | 0 | ['Array', 'String', 'Java'] | 0 |
count-prefixes-of-a-given-string | 0ms, Beats 100%, Hopefully my solution will help everyone with an easy problem solving | 0ms-beats-100-hopefully-my-solution-will-vn5n | IntuitionApproachComplexity
Time complexity:O(n)
Space complexity:O(1)
Code | bvc01654 | NORMAL | 2025-02-05T08:58:45.397939+00:00 | 2025-02-05T08:58:45.397939+00:00 | 66 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int c=0;
for(string x : words)
if(s.substr(0,x.size())==x)
c++;
return c;
}
};
``` | 1 | 0 | ['String', 'Iterator', 'C++'] | 0 |
count-prefixes-of-a-given-string | [C++] Simple Solution | c-simple-solution-by-samuel3shin-8lai | Code | Samuel3Shin | NORMAL | 2025-01-23T16:11:58.057030+00:00 | 2025-01-23T16:11:58.057030+00:00 | 35 | false | # Code
```cpp []
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
unordered_set<string> hs;
for(int i=1; i<=s.size(); i++) {
hs.insert(s.substr(0, i));
}
int ans = 0;
for(auto w : words) {
if(hs.count(w)) ans++;
}
return ans;
}
};
``` | 1 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | 0MS 🥇|| BEATS 100% 🥂 || 5 LINE CODE ⛳ || 🌟JAVA ☕ | 0ms-beats-100-5-line-code-java-by-galani-giph | Code | Galani_jenis | NORMAL | 2025-01-18T05:30:24.151194+00:00 | 2025-01-18T05:30:24.151194+00:00 | 88 | false | 
# Code
```java []
class Solution {
public int countPrefixes(String[] words, String s) {
int ans = 0;
for (String word : words) {
if (s.startsWith(word)) {
ans++;
}
}
return ans;
}
}
```
| 1 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | 4 lines of python code beats 100% | 4-lines-of-python-code-beats-100-by-vsab-mr5x | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | vsabari146 | NORMAL | 2025-01-10T07:35:59.463227+00:00 | 2025-01-10T07:35:59.463227+00:00 | 113 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
l= len(s)
count=0
for i in words:
if i==s[0:len(i)]:
count+=1
return count
``` | 1 | 0 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | Easy solution for begineers || 100% | easy-solution-for-begineers-100-by-kunal-lzjm | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | kunal44885 | NORMAL | 2025-01-09T15:03:26.643077+00:00 | 2025-01-09T15:03:26.643077+00:00 | 112 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int cnt=0;
for(int i =0;i<words.size();i++){
if( s.substr(0, words[i].size())== words[i]){
cnt++;
}
}
return cnt;
}
};
``` | 1 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | Built-in Function | built-in-function-by-khushi_1502-7kuc | Complexity
Time complexity:O(N)
Space complexity:O(1)
Code | khushi_1502 | NORMAL | 2025-01-09T06:42:51.795312+00:00 | 2025-01-09T06:42:51.795312+00:00 | 124 | false |
# Complexity
- Time complexity:O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int countPrefixes(String[] words, String s) {
int count=0;
for(int i=0;i<words.length;i++){
if(s.startsWith(words[i]))
count++;
}
return count;
}
}
``` | 1 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | 🔍 Count Prefixes in a String – Clean and Concise Python Solution!🚀 | count-prefixes-in-a-string-clean-and-con-whmg | 🧠 Intuition💡 The task is to count how many words in the list words are prefixes of the string s.
A word is a prefix of s if it matches the initial characters of | Ramanand98 | NORMAL | 2025-01-09T06:32:51.300285+00:00 | 2025-01-09T06:32:51.300285+00:00 | 95 | false | 
# 🧠 **Intuition**
💡 The task is to count how many words in the list `words` are prefixes of the string `s`.
- A word is a prefix of `s` if it matches the initial characters of `s`.
- We can check this by slicing `s` up to the length of the word and comparing it with the word.
---
# 🔍 **Approach**
1️⃣ Initialize a counter `cnt` to keep track of the number of prefix matches.
2️⃣ Iterate through each word in `words`.
3️⃣ For each word, check if it matches the prefix of `s` (using slicing).
4️⃣ If it matches, increment the counter.
5️⃣ Return the final count after checking all words.
---
# ⏱️ **Complexity**
- **Time Complexity**: $$O(n \cdot m)$$
- **n**: Number of words in the list.
- **m**: Average length of the words.
- **Space Complexity**: $$O(1)$$
- No extra space is used beyond a few variables.
---
# 💻 **Code**
```python
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
cnt = 0 # 🔢 Initialize counter
for word in words:
# ⚡ Check if the word is a prefix of s
if s[:len(word)] == word:
cnt += 1 # ✅ Increment count for a match
return cnt # 🔙 Return the total count
```
---
### 🌟 **Example Walkthrough**
```python
words = ["a", "b", "c", "ab", "abc"]
s = "abc"
solution = Solution()
print(solution.countPrefixes(words, s)) # Output: 3 🚀
```
---
### 🔥 **Like this solution? Let’s keep solving awesome problems together!** 🌟 | 1 | 0 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | Easy Solution | Beat 100% | c++ Solution | easy-solution-beat-100-c-solution-by-nit-5pl7 | IntuitionThe problem asks us to count how many words in the given list words are prefixes of the string s. The simplest approach is to check each word in the li | NITian_Dilip | NORMAL | 2025-01-09T05:41:53.359594+00:00 | 2025-01-09T05:45:54.721417+00:00 | 82 | false | # Intuition
The problem asks us to count how many words in the given list `words` are prefixes of the string `s`. The simplest approach is to check each word in the list and see if it matches the prefix of `s`. If the word is a prefix, we increment the count.
---
# Approach
1. **Initialize a count variable** to keep track of the number of prefixes found.
2. **Iterate through the `words` vector**:
- For each word, compare it with the corresponding prefix of `s` using `substr(0, word.length())`.
- If the prefix matches the word, increment the `count` variable.
3. **Return the `count`** after iterating through all words.
---
# Complexity
- **Time Complexity**:
Let \( n \) be the length of `s` and O\( m \) be the average length of the words in `words`.
For each word in `words`, we extract the prefix of length equal to the word's length, and the comparison operation takes linear time. Hence, the time complexity is \( O( w ) \), where \( w \) is the number of words in `words`.
- **Space Complexity**:
The space complexity is \( O(1) \) since we're only using a few extra variables for counting and string comparisons, regardless of the size of the input.
---
# Code
```cpp
class Solution {
public:
// Function to count how many words in the 'words' vector are prefixes of the string 's'
int countPrefixes(vector<string>& words, string s) {
int count = 0; // Initialize the count of prefix matches
// Loop through each word in the 'words' vector
for (int i = 0; i < words.size(); i++) {
// Extract the prefix of 's' with the same length as the current word
string temp = s.substr(0, words[i].length());
// If the prefix matches the current word, increment the count
if (temp == words[i]) {
count++;
}
}
// Return the total count of prefixes found
return count;
}
};
| 1 | 0 | ['Array', 'String', 'String Matching', 'C++'] | 0 |
count-prefixes-of-a-given-string | 2255. Count Prefixes of a Given String | 2255-count-prefixes-of-a-given-string-by-ky4l | Complexity
Time complexity: O(M*N)
Space complexity: O(1)
Code | Adarshjha | NORMAL | 2025-01-09T05:16:29.130771+00:00 | 2025-01-09T05:16:29.130771+00:00 | 7 | false | # Complexity
- Time complexity: O(M*N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```c []
int countPrefixes(char** words, int wordsSize, char* s) {
int t, count = 0;
for (int i = 0; i < wordsSize; i++) {
t = 1;
if (strlen(words[i]) <= strlen(s)) {
for (int j = 0; j < strlen(words[i]); j++) {
if (words[i][j] != s[j])
t = 0;
}
if (t)
count++;
}
}
return count;
}
``` | 1 | 0 | ['C'] | 0 |
count-prefixes-of-a-given-string | Easy solution with explanation beats 100% | easy-solution-with-explanation-beats-100-3u05 | IntuitionThe goal is to count how many words in the array words are prefixes of the string s. The Java String class provides the startsWith() method, which make | Shubhash_Singh | NORMAL | 2025-01-09T04:24:21.809558+00:00 | 2025-01-09T04:24:21.809558+00:00 | 21 | false | # Intuition
The goal is to count how many words in the array `words` are prefixes of the string `s`. The Java String class provides the `startsWith()` method, which makes it straightforward to check if one string is a prefix of another.
# Approach
1. Iterate Through the Words: Loop through each word in the words array.
2. Check Prefix: Use the startsWith() method to verify if the current word is a prefix of the string s.
3. Increment Counter: If the word is a prefix of s, increment the counter.
4. Return the Count: After checking all words, return the counter as the result.
# Complexity
## Time complexity:
- Each call to startsWith() checks up to the length of the word.
Let kk be the average length of words and mm be the number of words:
- Total time complexity: $$O(m⋅k)$$
## Space complexity:
- $$O(1)$$
# Code
```java []
class Solution {
public int countPrefixes(String[] words, String s) {
int count = 0;
for (String word : words) {
if(s.startsWith(word))
count++;
}
return count;
}
}
``` | 1 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | 🔥BEATS 💯 % 🎯 |✨SUPER EASY FOR BEGINNERS 👏 | beats-super-easy-for-beginners-by-vignes-z7nb | Code | vigneshvaran0101 | NORMAL | 2025-01-09T04:00:08.915637+00:00 | 2025-01-09T04:00:08.915637+00:00 | 40 | false | 
# Code
```python3 []
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
c = 0
for i in words:
if s.startswith(i):
c += 1
return c
```

| 1 | 0 | ['Python3'] | 0 |
count-prefixes-of-a-given-string | ads; | ads-by-abhaydeep_24-b04j | Complexity
Time complexity:
O(N*M)
N=size of words array
M=max length of word in words array
Space complexity:
O(1)
Code | abhaydeep_24 | NORMAL | 2025-01-09T01:25:47.749151+00:00 | 2025-01-09T01:25:47.749151+00:00 | 30 | false | 
# Complexity
- Time complexity:
O(N*M)
N=size of words array
M=max length of word in words array
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int n=words.size();
int count=0;
for(int i=0;i<n;i++){
string word=words[i];
if(s.find(word)==0) count++;
}
return count;
}
};
``` | 1 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | Simplest answer in 1 loop | simplest-answer-in-1-loop-by-saksham_gup-a8bs | Complexity
Time complexity: O(n.m)
(n for strings in array and m for length of each string)
Space complexity: O(1)
Code | Saksham_Gupta_ | NORMAL | 2025-01-04T11:19:52.844737+00:00 | 2025-01-04T11:19:52.844737+00:00 | 116 | false |
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: ***O(n.m)***
*(n for strings in array and m for length of each string)*
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: ***O(1)***
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int countPrefixes(String[] words, String s) {
int count = 0;
for(int i=0; i<words.length; i++){
if(s.startsWith(words[i])){
count++;
}
}
return count;
}
}
``` | 1 | 0 | ['Array', 'String', 'Java'] | 0 |
count-prefixes-of-a-given-string | leetcodedaybyday - Beats 100% with C++ and Beats 100% with Python3 | leetcodedaybyday-beats-100-with-c-and-be-3j3i | IntuitionThe task is to count how many words in the words list are prefixes of the string s. A prefix of a string is defined as a substring that starts at the b | tuanlong1106 | NORMAL | 2024-12-28T08:31:04.567395+00:00 | 2024-12-28T08:31:04.567395+00:00 | 82 | false | # Intuition
The task is to count how many words in the `words` list are prefixes of the string `s`. A prefix of a string is defined as a substring that starts at the beginning of the string. To solve this, we can compare each word in the `words` list with the corresponding prefix of `s` of the same length.
# Approach
1. **Iterate Through Words**:
- For each word in the `words` list, check if it matches the prefix of `s` with the same length as the word.
- In Python, we can use the `startswith` method to check for prefixes efficiently.
- In C++, use the `substr` method to extract the prefix of `s` and compare it with the word.
2. **Count Matches**:
- If the word matches the prefix, increment the prefix counter.
3. **Return the Count**:
- At the end of the loop, return the counter as the result.
# Complexity
- **Time Complexity**:
- `O(n * m)`, where `n` is the number of words in the `words` list and `m` is the average length of the words.
- For each word, we check its length against the prefix of `s`, which takes `O(m)` time.
- **Space Complexity**:
- `O(1)`. No additional space is used apart from a counter.
# Code
```cpp []
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int prefixCount = 0;
for (string word : words){
if (word == s.substr(0, word.size())){
prefixCount++;
}
}
return prefixCount;
}
};
```
```python3 []
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
prefixCount = 0
# for word in words:
# if word == s[:len(word)]:
# prefixCount += 1
# return prefixCount
for word in words:
if s.startswith(word):
prefixCount += 1
return prefixCount
```
| 1 | 0 | ['Array', 'String', 'C++', 'Python3'] | 0 |
count-prefixes-of-a-given-string | Easy and 100% fast solution using string method | easy-and-100-fast-solution-using-string-qmloz | IntuitionTo solve the problem of finding how many strings in the words array are a prefix of string s, let us break this problem :-
Understanding a Prefix :- | code_space | NORMAL | 2024-12-17T09:33:19.897625+00:00 | 2024-12-17T09:33:19.897625+00:00 | 142 | false | \n\n# Intuition\nTo solve the problem of finding how many strings in the `words` array are a prefix of string `s`, let us break this problem :-\n1. **Understanding a Prefix** :- A string w is said to be a prefix of another string `s` if `w` matches the starting part of `s`. FOr example :-\n - `"app"` is a prefix of `"apple"`.\n \n2. **Matching Prefixes** :- Compare each `word` in `words` to the start of `s` to check if it forms a prefix.\n\n3. **Efficient Comparison** :- Loop through all `words` in the words array.\n\n# Approach\n1. Initialize a `counter` variable to keep track of how many `words` are prefixes of `s`.\n2. Loop through each word in the words array.\n3. Use the `startsWith()` method to check if the current `word` is a prefix of `s`.\n4. If it is, increment the counter.\n5. Return the `counter` after checking all the words.\n\n\n# Complexity\n- **Time complexity** :- Let `n` be the number of `words` in the words array, and `m` be the average length of words. For each `word`, startsWith() takes `O(m)` time to compare. So, total time complexity will be `O(n\xD7m)`and as `m` is very small (10). Hence this can be written as `O(n)` time complexity\n\n- **Space complexity** :- `O(1)` as we are not using any additional data structures apart from the counter variable.\n\n# Code\n``` JavaScript []\nlet countPrefixes = function(words, s) {\n // using the reduce() method to accumulate the count of valid prefixes\n return words.reduce((prefixCount, currWord) => {\n // check if \'s\' starts with the current word using startsWith()\n // \'s.startsWith(currentWord)\' returns true (1) if currentWord is a prefix, otherwise false (0)\n return prefixCount + s.startsWith(currWord);\n }, 0); //initialize prefixCount (accumulator) to 0\n};\n```\n``` Java []\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n // initialize the prefix count `prefixCount` to 0\n int prefixCount = 0;\n\n // iterate through each word in the \'words\' array\n for(String word : words){\n // check if the current word is a prefix of \'s\'\n if(s.startsWith(word)){\n prefixCount++;\n }\n }\n\n // return the total count of prefixes\n return prefixCount;\n }\n}\n```\n```cpp []\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n // initialize the prefix count `prefixCount` to 0\n int prefixCount = 0;\n\n // iterate through each word in the \'words\' array\n for (string word : words) {\n // check if the current word is a prefix of \'s\'\n if (s.substr(0, word.size()) == word) {\n prefixCount++;\n }\n }\n\n // return the total count of prefixes\n return prefixCount;\n}\n};\n``` | 1 | 0 | ['Array', 'String', 'C++', 'Java', 'JavaScript'] | 0 |
count-prefixes-of-a-given-string | Count Prefixes of a Given String | count-prefixes-of-a-given-string-by-tejd-3o8v | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is to determine how many strings in an array are prefixes of a given string | tejdekiwadiya | NORMAL | 2024-06-12T17:21:54.940920+00:00 | 2024-06-12T17:21:54.940943+00:00 | 21 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to determine how many strings in an array are prefixes of a given string. A prefix of a string is a substring that starts at the beginning of the string and can be of any length up to the length of the string itself. To solve this problem, we can iterate through each word in the array and check if it is a prefix of the given string using the `startsWith` method.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initialize a counter**: Start with a counter set to zero to keep track of the number of prefixes found.\n2. **Iterate through the array**: Loop through each string in the `words` array.\n3. **Check for prefix**: For each string, use the `startsWith` method to check if it is a prefix of the string `s`.\n4. **Update the counter**: If a string is a prefix, increment the counter.\n5. **Return the count**: After checking all strings, return the final count.\n\nHere\'s the given code, which implements this approach:\n\n```java\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count = 0;\n for (int i = 0; i < words.length; i++) {\n if (s.startsWith(words[i])) {\n count++;\n }\n }\n return count;\n }\n}\n```\n\n# Complexity\n- **Time complexity**:\n - The `startsWith` method has a time complexity of (O(k)) where (k) is the length of the word being checked.\n - Since we iterate through each word in the array and `startsWith` is called for each word, the overall time complexity is (O(n cdot k)), where (n) is the number of words and (k) is the average length of the words.\n\n- **Space complexity**:\n - The space complexity is (O(1)) because we are using only a constant amount of extra space (the integer `count`), irrespective of the input size. The `startsWith` method does not use additional space that scales with the input size. | 1 | 0 | ['Array', 'String', 'Java'] | 0 |
count-prefixes-of-a-given-string | 2 Line Solution || 100% faster || Single Pass | 2-line-solution-100-faster-single-pass-b-v43p | Intuition\nIf given words are the substring at index 0, they are the desired string count\n Describe your first thoughts on how to solve this problem. \n\n# App | Nitesh_Gupta_NG | NORMAL | 2024-06-07T18:25:42.451243+00:00 | 2024-06-07T18:25:42.451279+00:00 | 102 | false | # Intuition\nIf given words are the substring at index 0, they are the desired string count\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nUse indexOf function to validate, words are coming at first index or not\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\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count = 0;\n for(String w:words) if(s.indexOf(w)==0) count++;\n return count;\n }\n}\n``` | 1 | 0 | ['Java'] | 1 |
count-prefixes-of-a-given-string | [Java] Best solution in design scenario - Using Trie Tree | java-best-solution-in-design-scenario-us-kshz | java\nclass Solution {\n public int countPrefixes(final String[] words, final String s) {\n final TrieNode root = new TrieNode();\n\n // Insert | YTchouar | NORMAL | 2024-05-08T06:59:02.029667+00:00 | 2024-05-08T06:59:13.506925+00:00 | 385 | false | ```java\nclass Solution {\n public int countPrefixes(final String[] words, final String s) {\n final TrieNode root = new TrieNode();\n\n // Insert all words in Trie\n\n for(final String word : words) {\n TrieNode curr = root;\n\n for(int i = 0; i < word.length(); ++i) {\n final int idx = word.charAt(i) - \'a\';\n\n if(curr.children[idx] == null)\n curr.children[idx] = new TrieNode();\n\n curr = curr.children[idx];\n }\n\n curr.count++;\n }\n\n int count = 0;\n TrieNode curr = root;\n\n for(int i = 0; i < s.length() && curr.children[s.charAt(i) - \'a\'] != null; ++i) {\n curr = curr.children[s.charAt(i) - \'a\'];\n count += curr.count;\n }\n\n return count;\n }\n\n private class TrieNode {\n public int count;\n public TrieNode[] children;\n\n public TrieNode() {\n this.count = 0;\n this.children = new TrieNode[26];\n }\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
count-prefixes-of-a-given-string | Easiest C || C++ || Java Solutions -- 100% beats Easy[Simple] | easiest-c-c-java-solutions-100-beats-eas-qlco | Intuition\n\nC []\nint countPrefixes(char** words, int wordsSize, char* s) {\n int c = 0;\n for (int i = 0; i < wordsSize; i++) {\n strncmp(words[i | Edwards310 | NORMAL | 2024-02-03T06:39:31.690431+00:00 | 2024-02-03T06:39:31.690462+00:00 | 3 | false | # Intuition\n\n```C []\nint countPrefixes(char** words, int wordsSize, char* s) {\n int c = 0;\n for (int i = 0; i < wordsSize; i++) {\n strncmp(words[i], s, strlen(words[i])) == 0 && c++;\n }\n return c;\n}\n```\n``` Java []\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int cnt = 0;\n for (int i = 0; i < words.length; i++) {\n if (s.startsWith(words[i]))\n cnt++;\n }\n return cnt;\n }\n}\n```\n```C++ []\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int cnt = 0;\n for (auto& it : words) {\n string str = s.substr(0, it.size());\n if (str == it)\n cnt++;\n }\n\n return cnt;\n }\n};\n```\n```C []\nint countPrefixes(char** words, int wordsSize, char* s) {\n int count = 0;\n bool add;\n for (int i = 0; i < wordsSize; i++) {\n\n add = true;\n char* x = words[i];\n for (int j = 0; x[j]; j++)\n if (!s[j] || s[j] != x[j]) {\n add = false;\n break;\n }\n\n if (add)\n count++;\n }\n return count;\n}\n```\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 O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(1)\n# Code\n```\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int cnt = 0;\n for (int i = 0; i < words.length; i++) {\n if (s.startsWith(words[i]))\n cnt++;\n }\n return cnt;\n }\n}\n``` | 1 | 0 | ['C', 'C++', 'Java'] | 0 |
count-prefixes-of-a-given-string | Java || Simple Solution || 100% Beats | java-simple-solution-100-beats-by-akash_-3r88 | \n# Code\n\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count | akash_kce | NORMAL | 2024-02-01T09:51:01.589296+00:00 | 2024-02-01T09:51:01.589330+00:00 | 379 | false | \n# Code\n```\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass Solution {\n public int countPrefixes(String[] words, String s) {\n int count = 0;\n for (String i : words)\n if (s.startsWith(i)) count++;\n return count;\n }\n}\n\n``` | 1 | 0 | ['Array', 'String', 'Java'] | 0 |
count-prefixes-of-a-given-string | Easy one liner solution | easy-one-liner-solution-by-elangoram1998-95c9 | \n# Code\n\nfunction countPrefixes(words: string[], s: string): number {\n return words.filter((word) => s.startsWith(word)).length;\n};\n | elangoram1998 | NORMAL | 2023-12-01T10:46:23.724286+00:00 | 2023-12-01T10:46:23.724312+00:00 | 45 | false | \n# Code\n```\nfunction countPrefixes(words: string[], s: string): number {\n return words.filter((word) => s.startsWith(word)).length;\n};\n``` | 1 | 0 | ['TypeScript', 'JavaScript'] | 0 |
count-prefixes-of-a-given-string | Count Prefixes of a Given String Solution in C++ | count-prefixes-of-a-given-string-solutio-humn | 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 | The_Kunal_Singh | NORMAL | 2023-03-14T18:03:58.544456+00:00 | 2023-03-14T18:03:58.544490+00:00 | 51 | 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)$$ -->\nO(n*m)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n# Code\n```\nclass Solution {\npublic:\n int countPrefixes(vector<string>& words, string s) {\n int i, j, count=0;\n string str="";\n for(i=0 ; i<s.length() ; i++)\n {\n str += s[i];\n for(j=0 ; j<words.size() ; j++)\n {\n if(str==words[j])\n {\n count++;\n }\n }\n }\n return count;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
count-prefixes-of-a-given-string | Ruby solution. 1-line | ruby-solution-1-line-by-guswhitten-rdez | \n# @param {String[]} words\n# @param {String} s\n# @return {Integer}\ndef count_prefixes(words, s)\n words.count { |word| s.start_with?(word) }\nend\n | guswhitten | NORMAL | 2023-02-12T14:37:56.157570+00:00 | 2023-02-12T14:37:56.157600+00:00 | 7 | false | ```\n# @param {String[]} words\n# @param {String} s\n# @return {Integer}\ndef count_prefixes(words, s)\n words.count { |word| s.start_with?(word) }\nend\n``` | 1 | 0 | ['Ruby'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.