লিটকোড ১৩৯ | LEARN PROBLEM SOLVING with PYTHON | PART 72 : Leetcode 139 : Word Break

  Рет қаралды 178

IT VAYA

IT VAYA

Күн бұрын

Пікірлер: 4
@itvaya
@itvaya 28 күн бұрын
Algorithm: Word Break Using DFS with Memoization Definitions: - `s`: Input string to check if it can be segmented into words from the dictionary. - `wordDict`: List of valid words for segmentation. - `word_set`: A set of words from `wordDict` for fast lookups. - `cache`: A dictionary to store results of subproblems for memoization. Class: Solution Method: wordBreak(s, wordDict) Input: - s (string): Input string to segment. - wordDict (List[string]): List of valid words. Output: Boolean indicating whether `s` can be segmented into words from `wordDict`. 1. Convert `wordDict` into a set `word_set` for efficient lookups. 2. Initialize an empty dictionary `cache` for memoization. 3. Define a helper function `dfs(s)` for recursive depth-first search: a. If `s` is in `cache`, return `cache[s]` (result for the subproblem is already computed). b. If `s` is in `word_set`, return `True` (the whole string `s` is a valid word). c. For `i` from 1 to `len(s) - 1`: i. Set `pre` to `s[:i]` (prefix of the string). ii. If `pre` is in `word_set` and `dfs(s[i:])` (the suffix can be segmented), do the following: - Set `cache[s] = True`. - Return `True`. d. If no valid segmentation is found, set `cache[s] = False` and return `False`. 4. Call `dfs(s)` to determine if the string `s` can be segmented and return its result. Complexity Analysis: - Time Complexity: O(n²), where `n` is the length of the string. The recursive function evaluates each substring, and slicing takes O(n) time for each split. - Space Complexity: O(n), for the recursion stack and memoization dictionary.
@HasipSheikh-r3x
@HasipSheikh-r3x 26 күн бұрын
Amazing.
@wahidurrahman77
@wahidurrahman77 26 күн бұрын
vai Java language theke ashe Python dekhle mone hoi ulonggo
@itvaya
@itvaya 26 күн бұрын
🤣🤣🤣
Zelensky Announces Talks with Russia / End of Martial Law?
13:55
NEXTA Live
Рет қаралды 1,1 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
How To Speak Fluently In English About Almost Anything
1:49:55
EnglishAnyone
Рет қаралды 3,1 МЛН
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 163 М.
I Helped 2,000 People Walk Again
15:31
MrBeast
Рет қаралды 12 МЛН
Сборник Эксклюзивов 2024 - Уральские Пельмени
1:33:24
Уральские Пельмени
Рет қаралды 1,3 МЛН
Netflix Removed React?
20:36
Theo - t3․gg
Рет қаралды 67 М.
КАК ЖИВЕТ КВАНТУМ? РУМ ТУР КВАНТУМА!!!
13:51
Массаж головы пранк🤣
0:55
Kirya Kolesnikov
Рет қаралды 5 МЛН
ТЕЛЕФОН МЕНЯЕТ ЦВЕТ😅 #upx
0:34
RanF
Рет қаралды 639 М.
This thing is CRAZY 🤯 #shorts
0:20
House of Highlights
Рет қаралды 48 МЛН