Substring with Concatenation of All Words
LeetCode 30 · কঠিন · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু
Word-sized sliding window, run once per offset.
আমি Substring with Concatenation of All Words (LeetCode 30) এভাবে সমাধান করেছি — Window + Hashing (Multi-word) প্যাটার্নের একটি কঠিন প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।
নিচের বিবরণ ইংরেজিতে।
Intuition
All words have the same length, so the string can be read in word-sized chunks. For each of the word-length offsets, slide a window of words: add the next word, and if it isn't a word at all reset; if it appears too often, drop words from the left until it's fine. A full window with no excess is a match.
Approach
- 1
Count the required words.
- 2
For each offset 0..word_size−1, slide over chunks.
- 3
Unknown chunk: reset the window past it.
- 4
Too many of a word or window too long: drop the leftmost word.
- 5
Window holds exactly all words → record left.
আমি কীভাবে সমাধান করেছি
Word-level sliding windowPythonO(n·w) time
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
#sliding window with two pointers
#check the frequency of words
#if it matches the window is valid
#till the window is valid move right
res=[]
freq_map = Counter(words)
word_size = len(words[0])
substring_size = word_size*len(words)
def slide(left):
cur_words_used = defaultdict(int)
excess_word = False
words_used =0
right=left
for right in range(left,len(s),word_size):
if right+word_size>len(s):
break
subs = s[right:right+word_size]
if subs not in freq_map:
#reset the window
left = right+word_size
cur_words_used=defaultdict(int)
excess_word = False
words_used=0
else:
#slide till the window is maintained or we have excess word, shrink/slide
while right-left==substring_size or excess_word:
left_most_word = s[left:left+word_size]
cur_words_used[left_most_word]-=1
left=left+word_size
#excess word check post removal
if freq_map[left_most_word]==cur_words_used[left_most_word]:
excess_word=False
else:
#window is shrinked, reduce the words used
words_used-=1
#do actual work
cur_words_used[subs]+=1
if cur_words_used[subs]>freq_map[subs]:
excess_word=True
else:
words_used+=1
if words_used==len(words) and excess_word==False:
res.append(left)
for i in range(len(words[0])):
slide(i)
return resSubstring with Concatenation of All Words — সাধারণ প্রশ্ন
Substring with Concatenation of All Words-এর পেছনে intuition কী?
All words have the same length, so the string can be read in word-sized chunks. For each of the word-length offsets, slide a window of words: add the next word, and if it isn't a word at all reset; if it appears too often, drop words from the left until it's fine. A full window with no excess is a match.
Substring with Concatenation of All Words (LeetCode 30) কীভাবে সমাধান করবেন?
1. Count the required words. 2. For each offset 0..word_size−1, slide over chunks. 3. Unknown chunk: reset the window past it. 4. Too many of a word or window too long: drop the leftmost word. 5. Window holds exactly all words → record left.
Substring with Concatenation of All Words-এর time আর space complexity কত?
Time: O(n · word_size)। Space: O(m)। (Accepted approach: Word-level sliding window।)
Substring with Concatenation of All Words-এ সাধারণ ভুলগুলো কী?
Different offsets are independent windows — you need all of them.