No video

Substring with Concatenation of All Words | Leetcode #30

  Рет қаралды 9,244

Techdose

Techdose

Күн бұрын

Пікірлер: 12
@prashantbharti8314
@prashantbharti8314 Ай бұрын
One way to optimize this implementation is to store all already checked substrings in input S against there evaluation output matched/not matched. If the same substring comes up again later for matching then we can avoid processing it on the basis of the stored matched/not matched value. If matched then current substring will also match. If not matched then current will not match. It will help us bypass the testcase of S = "aaaaa.....aaaa" of length 10000 and words = {"a", "a", ... "a"} of length = 5000. It will increase memory usage but help to optimize on latency.
@enigma2886
@enigma2886 28 күн бұрын
That's genius bro. I was stuck on that case only.
@KushagraJain-fc3nt
@KushagraJain-fc3nt 24 күн бұрын
Bro thanks , you are the leetcode pro , i will gift you leetcode premium. Thaks so much. I was stuck since 2 months . thanks bro.
@ahyungrocks5509
@ahyungrocks5509 4 ай бұрын
At time 9min 4s, I am still not able to comprehend why you don't need to process every position. Appreciate it if you can elaborate more.
@prabhatjha7802
@prabhatjha7802 4 ай бұрын
Basically, in one process it starts at index "i" and goes through entire string taking the window size, basically (i, i + window_size), (i+word_size, i + word_size +window_size) and so on generally (i+k*word_size, i + k*word_size +window_size) for different possible values of k. Now, he is saying i just needs to be 0, 1, 2 in this example and in general case (0, 1, 2, . . . , word_size - 1). Why this would suffice is what you are asking right? Let me explain this way: Lets suppose your answer lies at some (i_0, i_0 + window_size), the in the run where you started at (i_0%word_size) would do, why as i_0 would be of the form (i+k*word_size, i + k*word_size +window_size) for i = i_0%word_size and k = i_0//word_size. Please think on this line and let me know if this make sense.
@madhavilathakarumanchi7100
@madhavilathakarumanchi7100 2 ай бұрын
Sorry to say this , Little tricky to understand the explanation you have given.
@MrLeyt1125
@MrLeyt1125 5 ай бұрын
Great vid, but leetcode wont accept it Time Limit Exceeded 179 / 179 testcases passed Testcases passed, but took too long.
@techdose4u
@techdose4u 5 ай бұрын
Leetcode has given 3 seconds time limit for it. It will paas in 2900 ms approx. But yes, you can try some optimizations on string operations as used by top performers in submissions. This idea is same but code can be modified but this code makes life easier for someone new :)
@MrLeyt1125
@MrLeyt1125 5 ай бұрын
@@techdose4u I'm a newbie myself, your video helped me to understand algorithm. As far as I understand, the time is mainly spent on the copying operation curr = freq, so it’s faster to manually add and remove words from the curr dictionary. I stealed other code, very similar yo yours, it runs in 23 ms instead of 3sec: class Solution { public: vector findSubstring(string s, vector& words) { unordered_map dict_reference; //Эталонный словарь for (string& word: words) //Перебираю все слова в массиве words dict_reference[word]++; //Заполнение эталонного словаря (к его виду привожу текущий словарь каждую итерацию) int s_size = s.size(); //Длина s int word_size = words[0].size(); //длина words int words_count = words.size(); //кол-во words int window_size = word_size * words_count; //Размер sliding window vector result; //массив с ответами if (window_size > s_size or s.empty() or words.empty()) return result; for (int i = 0; i < word_size; i++) //Перебор всех стартовых позиций (проходим s words[0].size раз) { unordered_map dictionary; //Текущий словарь int left = i; //Левый край sliding window int right = i; //Правый край sliding window int count = 0; //Счетчик найденных слов while (right + word_size dict_reference[word]) //Пока текущий словарь толще эталонного { string leftWord = s.substr(left, word_size); left += word_size; dictionary[leftWord]--; //Удаляем из него самое левое слово (то от которого уже уехало sliding window) count--; //Счетчик найденных слов -- } if (count == words_count) result.push_back(left); //Все слова найдены, добавляем ответ в результат } else { dictionary.clear(); //убиваем текущий словарь count = 0; left = right; } } } return result; } };
@kushagarsharma4783
@kushagarsharma4783 2 ай бұрын
I wrote similar solution on my own 178/180 cases passed , that's why I am here😂
@harsha171
@harsha171 26 күн бұрын
java code explained the same way ,not my sol class Solution { public List findSubstring(String s, String[] words) { List ans = new ArrayList(); int n = s.length(); int m = words.length; int w = words[0].length(); HashMap map = new HashMap(); for(String x : words) map.put(x, map.getOrDefault(x,0)+1); for(int i=0; i 1) ? b - 1 : null); count--; k=k+w; } }//inner for loop }//outer for loop return ans; }//method }//class //................................................................
@NikhilKumar-yu3zh
@NikhilKumar-yu3zh Ай бұрын
to be honest, explanation can be improved.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 457 М.
Concatenated Words - Leetcode 472 - Python
12:00
NeetCodeIO
Рет қаралды 13 М.
Мы сделали гигантские сухарики!  #большаяеда
00:44
wow so cute 🥰
00:20
dednahype
Рет қаралды 28 МЛН
The New Option and Result Types of C#
15:05
Nick Chapsas
Рет қаралды 59 М.
String - 16: Find Substrings formed using Concatenation of all given words
18:06
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 183 М.
How Topson plays RINGMASTER the FIRST TIME!🤡
11:21
Topson
Рет қаралды 115 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 327 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 391 М.
Closest Prime Numbers in Range | Leetcode #2523
10:46
Techdose
Рет қаралды 1,5 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 828 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 642 М.
Мы сделали гигантские сухарики!  #большаяеда
00:44