🌐
LeetCode
leetcode.com β€Ί problems β€Ί find-the-index-of-the-first-occurrence-in-a-string
Find the Index of the First Occurrence in a String - LeetCode
Find the Index of the First Occurrence in a String - Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
🌐
LeetCode
leetcode.com β€Ί problems β€Ί substring-matching-pattern
Substring Matching Pattern - LeetCode
Can you solve this real interview question? Substring Matching Pattern - You are given a string s and a pattern string p, where p contains exactly one '*' character. The '*' in p can be replaced with any sequence of zero or more characters. Return true if p can be made a substring of s, and ...
Discussions

Optimized way to find all substrings/subsets/subarrays
Depends on which subarrays you want to find. All of them? Just do 2 for loops. Ones with some "maximum size" criterion like "at most 2 distinct values"? Maybe something like sliding windows. Want to match a specific word? There's some string algorithms for that that are better than O(len(word)*len(bigstring)). Want to find matches with a set of words? Probably more string specific algorithms. Want to just count the subarrays of a certain type? Dynamic programming or something similar to prefix sum might help. In general, but especially with dp, it might help to try and group all the subarrays by their ending index or by their starting index. It helps recognizing that every subarray ending at index i is a subarray ending at index i-1 with arr[i] appended at the end or arr[i] itself or possibly the empty subarray if you count that. Want to find something like all intervals whose sum is a multiple of k? Try computing the prefix sums mod k. If i and j have the same prefix sum mod k (like both 2 mod 5) then the subarray from i to j has a sum that is a multiple of k. Want to do something like find the min value or something of each subarray and then add them up? Maybe use monotonic stack and summarize the results like the min values of subarrays ending at index i are (3 subarrays with 2, 5 subarrays with 4, 9 subarrays with 8). When you want to figure out what the min values are for at index i+1, and say arr[i+1] is 3, then pop off some larger values and now your summary of the mins is (3 subarrays with 2, 1(index i+1) +5+9 subarrays with minimum of 3) Probably lots more dp and/or problems where you want to add up some f(subarray) for each subarray. Subsets is very wide topic but might be greedy or dp. Subsequences is also very wide topic but might be dp or greedy. More on reddit.com
🌐 r/leetcode
3
2
August 29, 2024
Not sure how to improve this answer. (Count the appearance of substring in string)
Rabin karp or KMP algorithm More on reddit.com
🌐 r/leetcode
8
2
November 29, 2022
How did you guys get good at substring and 2 pointer problems?
not to be pedantic, but these are both sliding window problems, not 2 pointer. like others have said, the trick with these sliding window problems is to hammer out a ton of them. Find a problem list (for example, https://leetcode.com/discuss/study-guide/3630462/Top-20-Sliding-Window-Problems-for-beginners , and work through them all. You will discover patterns as you solve them. It can also help to seek out a template (for example, https://leetcode.com/problems/find-all-anagrams-in-a-string/solutions/92007/sliding-window-algorithm-template-to-solve-all-the-leetcode-substring-search-problem/ ) to solve the first 5 or so, then try to solve without the template. As you've said, the reason graph and tree problems feel easier is because the patterns are very recognizable. For example, it's usually immediately obvious if you need to do a level order traversal/BFS on a tree, and you probably have a BFS template in your head already. You need to do this for sliding window as well. Another tip for sliding window in particular: I like to think of these problems as "cutting down the search space". For sliding window, you usually increase the right pointer as far as it will go, until the condition is not satisfied anymore. then, you increase the left pointer until the condition is once again satisfied. Each time you increase the left pointer, you're knocking out all the substring/subarrays that are rooted at that left pointer (it's impossible to consider them again - and for good reason: the condition would not be satisfied by *any* subset rooted at that left pointer). this is the key to understanding how sliding window works. good luck! More on reddit.com
🌐 r/leetcode
15
71
March 30, 2024
Can this be done this way ? Find the Index of the First Occurrence in a String
Yeah, this should work. It's not strictly optimal being O(hLenth * nLength) while the optimal solution is O(hLength + nLength) but it's still a solution. What are the solutions you've seen in YouTube videos? More on reddit.com
🌐 r/leetcode
1
1
December 22, 2023
🌐
LeetCode
leetcode.com β€Ί problems β€Ί find-and-replace-in-string
Find And Replace in String - LeetCode
The replacement operations are ... k. To complete the ith replacement operation: 1. Check if the substring sources[i] occurs at index indices[i] in the original string s. 2. If it does not occur, do nothing....
🌐
LeetCode
leetcode.com β€Ί problems β€Ί existence-of-a-substring-in-a-string-and-its-reverse
Existence of a Substring in a String and Its Reverse - LeetCode
Existence of a Substring in a String and Its Reverse - Given a string s, find any substring of length 2 which is also present in the reverse of s. Return true if such a substring exists, and false otherwise.
🌐
LeetCode
leetcode.com β€Ί problems β€Ί longest-substring-without-repeating-characters
Longest Substring Without Repeating Characters - LeetCode
Longest Substring Without Repeating Characters - Given a string s, find the length of the longest substring without duplicate characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Note that ...
🌐
LeetCode
leetcode.com β€Ί problems β€Ί number-of-strings-that-appear-as-substrings-in-word
Number of Strings That Appear as Substrings in Word - LeetCode
Can you solve this real interview question? Number of Strings That Appear as Substrings in Word - Given an array of strings patterns and a string word, return the number of strings in patterns that exist as a substring in word. A substring is a contiguous sequence of characters within a string.
🌐
LeetCode
leetcode.com β€Ί problems β€Ί find-substring-with-given-hash-value
Find Substring With Given Hash Value - LeetCode
Find Substring With Given Hash Value - The hash of a 0-indexed string s of length k, given integers p and m, is computed using the following function: * hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m. Where ...
🌐
LeetCode
leetcode.com β€Ί problems β€Ί find-all-anagrams-in-a-string
Find All Anagrams in a String - LeetCode
Can you solve this real interview question? Find All Anagrams in a String - Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.
Find elsewhere
🌐
LeetCode
leetcode.com β€Ί problems β€Ί string-matching-in-an-array
String Matching in an Array - LeetCode
Can you solve this real interview question? String Matching in an Array - Given an array of string words, return all strings in words that are a substring of another word. You can return the answer in any order.
🌐
Reddit
reddit.com β€Ί r/leetcode β€Ί optimized way to find all substrings/subsets/subarrays
r/leetcode on Reddit: Optimized way to find all substrings/subsets/subarrays
August 29, 2024 -

Hi! This is entirely based on a pattern in LeetCode problems, but if I need to find ALL substrings/subsets/subarrays of a string/array, is there a way I can do it in less than O(n^2) time complexity? Thank you!

Top answer
1 of 2
2
Depends on which subarrays you want to find. All of them? Just do 2 for loops. Ones with some "maximum size" criterion like "at most 2 distinct values"? Maybe something like sliding windows. Want to match a specific word? There's some string algorithms for that that are better than O(len(word)*len(bigstring)). Want to find matches with a set of words? Probably more string specific algorithms. Want to just count the subarrays of a certain type? Dynamic programming or something similar to prefix sum might help. In general, but especially with dp, it might help to try and group all the subarrays by their ending index or by their starting index. It helps recognizing that every subarray ending at index i is a subarray ending at index i-1 with arr[i] appended at the end or arr[i] itself or possibly the empty subarray if you count that. Want to find something like all intervals whose sum is a multiple of k? Try computing the prefix sums mod k. If i and j have the same prefix sum mod k (like both 2 mod 5) then the subarray from i to j has a sum that is a multiple of k. Want to do something like find the min value or something of each subarray and then add them up? Maybe use monotonic stack and summarize the results like the min values of subarrays ending at index i are (3 subarrays with 2, 5 subarrays with 4, 9 subarrays with 8). When you want to figure out what the min values are for at index i+1, and say arr[i+1] is 3, then pop off some larger values and now your summary of the mins is (3 subarrays with 2, 1(index i+1) +5+9 subarrays with minimum of 3) Probably lots more dp and/or problems where you want to add up some f(subarray) for each subarray. Subsets is very wide topic but might be greedy or dp. Subsequences is also very wide topic but might be dp or greedy.
2 of 2
1
One more trick: If you have some function f such that f(subarray) = some number and you want to make make queries about like the sum or min or max of f of each subarray within the subarray arr[i:j] try: dp1[i][j] = f( arr[i:j] ) \ this might be quicker to compute with dp or something dp2[i][j] = max( dp2[i+1][j], dp2[i][j-1], dp1[i][j] ) You either find the best answer as the entire subarray or the first element of the subarray isn't in the best answer so dp2[i+1][j] or the best answer doesnt use the last element so dp2[i][j-1].
🌐
LeetCode
leetcode.com β€Ί problems β€Ί find-the-substring-with-maximum-cost
Find the Substring With Maximum Cost - LeetCode
Can you solve this real interview question? Find the Substring With Maximum Cost - You are given a string s, a string chars of distinct characters and an integer array vals of the same length as chars. The cost of the substring is the sum of the values of each character in the substring.
🌐
LeetCode
leetcode.com β€Ί discuss β€Ί interview-question β€Ί 5140133 β€Ί Finding-All-Valid-Substrings-Interview-Question
Finding All Valid Substrings Interview Question - Discuss
Ex: strList = ["poiabcdefg", "bcdpoia", "defgzbcdamnapoi", "mpoinaqwbcd"] subStrLen = 3 result = ["bcd", "poi"] What would the optimal solution be here? In the interview they didn't seem to care about the most efficient solution - just moreso about me getting a working answer so I just went with the brute-force solution. My solution was finding the shortest string in the list and performing sliding window on that string to get substrings of the correct length and then just brute-force checking if that substring is found in all the other strings.
🌐
LeetCode
leetcode.com β€Ί problem-list β€Ί string-matching
String Matching - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
🌐
Reddit
reddit.com β€Ί r/leetcode β€Ί not sure how to improve this answer. (count the appearance of substring in string)
r/leetcode on Reddit: Not sure how to improve this answer. (Count the appearance of substring in string)
November 29, 2022 -

I can't find the question on leetcode but geeksforgeeks has it.

https://www.geeksforgeeks.org/videos/frequency-of-a-substring-in-a-string/

I just had an interview with a hiring manager. They gave me this question, I explained the same solution found in this link. They asked me about time complexity, I said O(n * m).

They asked me how I could improve the solution, internally I was floored and confused. I thought it couldn't be improve but I decided to give another answer anyways. This second answer was more complicated and they didn't ask about time complexity (I don't think it was improved) since we were running low on time. I went ahead and implemented yet although I had some bugs I was able to communicate but not able to fix before the end of the interview.

Is there really a way to improve this answer?

🌐
LeetCode
leetcode.com β€Ί problems β€Ί minimum-window-substring
Minimum Window Substring - LeetCode
Minimum Window Substring - Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window.
🌐
LeetCode
leetcode.com β€Ί problems β€Ί maximum-number-of-occurrences-of-a-substring
Maximum Number of Occurrences of a Substring - LeetCode
Can you solve this real interview question? Maximum Number of Occurrences of a Substring - Given a string s, return the maximum number of occurrences of any substring under the following rules: * The number of unique characters in the substring must be less than or equal to maxLetters.
🌐
LeetCode
leetcode.com β€Ί problems β€Ί repeated-substring-pattern
Repeated Substring Pattern - LeetCode
Can you solve this real interview question? Repeated Substring Pattern - Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Example 1: Input: s = "abab" Output: true Explanation: It is the substring "ab" twice.
🌐
LeetCode
leetcode.com β€Ί problems β€Ί substring-with-concatenation-of-all-words
Substring with Concatenation of All Words - LeetCode
Can you solve this real interview question? Substring with Concatenation of All Words - You are given a string s and an array of strings words. All the strings of words are of the same length. A concatenated string is a string that exactly contains all the strings of any permutation of words ...
🌐
LeetCode
leetcode.com β€Ί problems β€Ί number-of-substrings-containing-all-three-characters
Number of Substrings Containing All Three Characters - LeetCode
Can you solve this real interview question? Number of Substrings Containing All Three Characters - Given a string s consisting only of characters a, b and c. Return the number of substrings containing at least one occurrence of all these characters a, b and c. Example 1: Input: s = "abcabc" Output: 10 Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).