Тёмный

Substring with Concatenation of All Words | Leetcode #30 

Techdose
Подписаться 177 тыс.
Просмотров 12 тыс.
50% 1

Опубликовано:

 

16 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 12   
@prashantbharti8314
@prashantbharti8314 3 месяца назад
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 2 месяца назад
That's genius bro. I was stuck on that case only.
@KushagraJain-fc3nt
@KushagraJain-fc3nt 2 месяца назад
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 6 месяцев назад
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 6 месяцев назад
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.
@harsha171
@harsha171 2 месяца назад
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 //................................................................
@madhavilathakarumanchi7100
@madhavilathakarumanchi7100 4 месяца назад
Sorry to say this , Little tricky to understand the explanation you have given.
@MrLeyt1125
@MrLeyt1125 7 месяцев назад
Great vid, but leetcode wont accept it Time Limit Exceeded 179 / 179 testcases passed Testcases passed, but took too long.
@techdose4u
@techdose4u 7 месяцев назад
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 7 месяцев назад
@@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 4 месяца назад
I wrote similar solution on my own 178/180 cases passed , that's why I am here😂
@NikhilKumar-yu3zh
@NikhilKumar-yu3zh 3 месяца назад
to be honest, explanation can be improved.
Далее
Concatenated Words - Leetcode 472 - Python
12:00
Просмотров 14 тыс.
Maximum Swap | 4 techniques | Leetcode 670
19:05
БАБУШКА ВСЁ ИСПОРТИЛА #shorts
00:24
Просмотров 580 тыс.
Пряный осенний баббл чай
00:44
Просмотров 261 тыс.
Слушали бы такое на повторе?
01:00
Check If Array Pairs Are Divisible by k | Leetcode 1497
12:53
If Your Tech Job is Comfortable, You're in Danger
20:57
Longest Happy String | Leetcode 1405
14:01
Просмотров 2,1 тыс.
Sentence Similarity III | Leetcode 1813
9:49
Просмотров 1,5 тыс.
Design Circular Deque | Leetcode 641
15:14
Просмотров 1,6 тыс.
БАБУШКА ВСЁ ИСПОРТИЛА #shorts
00:24
Просмотров 580 тыс.