forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2160 from AkifhanIlgaz/0472
Create: 0472-concatenated-words
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
func findAllConcatenatedWordsInADict(words []string) []string { | ||
wordSet := initWordSet(words) | ||
res := []string{} | ||
|
||
var dfs func(word string) bool | ||
|
||
dfs = func(word string) bool { | ||
for i := 1; i < len(word); i++ { | ||
prefix, suffix := word[:i], word[i:] | ||
if (wordSet[prefix] && wordSet[suffix]) || wordSet[prefix] && dfs(suffix) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
for _, w := range words { | ||
if dfs(w) { | ||
res = append(res, w) | ||
} | ||
} | ||
|
||
return res | ||
} | ||
|
||
func initWordSet(words []string) map[string]bool { | ||
wordSet := make(map[string]bool, len(words)) | ||
|
||
for _, word := range words { | ||
wordSet[word] = true | ||
} | ||
|
||
return wordSet | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @param {string[]} words | ||
* @return {string[]} | ||
*/ | ||
var findAllConcatenatedWordsInADict = function (words) { | ||
let wordSet = new Set(words); | ||
let res = []; | ||
|
||
for (let w of words) { | ||
if (dfs(w)) { | ||
res.push(w); | ||
} | ||
} | ||
|
||
return res; | ||
|
||
/** | ||
* | ||
* @param {string} word | ||
* @returns {boolean} | ||
*/ | ||
function dfs(word) { | ||
for (let i = 1; i < word.length; i++) { | ||
let prefix = word.slice(0, i); | ||
let suffix = word.slice(i, word.length); | ||
|
||
if ( | ||
(wordSet.has(prefix) && wordSet.has(suffix)) || | ||
(wordSet.has(prefix) && dfs(suffix)) | ||
) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::collections::HashSet; | ||
use std::iter::FromIterator; | ||
|
||
impl Solution { | ||
pub fn find_all_concatenated_words_in_a_dict(words: Vec<String>) -> Vec<String> { | ||
let word_set: HashSet<String> = HashSet::from_iter(words.iter().cloned()); | ||
let mut res = vec![]; | ||
|
||
for w in words { | ||
if Self::dfs(&w, &word_set) { | ||
res.push(w); | ||
} | ||
} | ||
|
||
res | ||
} | ||
|
||
pub fn dfs(word: &str, word_set: &HashSet<String>) -> bool { | ||
for i in 1..word.len() { | ||
let (prefix, suffix) = (&word[..i], &word[i..]); | ||
if (word_set.contains(prefix) && word_set.contains(suffix)) | ||
|| (word_set.contains(prefix) && Self::dfs(suffix, word_set)) | ||
{ | ||
return true; | ||
} | ||
} | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function findAllConcatenatedWordsInADict(words: string[]): string[] { | ||
let wordSet = new Set(words); | ||
let res: string[] = []; | ||
|
||
for (let w of words) { | ||
if (dfs(w)) { | ||
res.push(w); | ||
} | ||
} | ||
|
||
return res; | ||
|
||
function dfs(word: string): boolean { | ||
for (let i = 1; i < word.length; i++) { | ||
let prefix = word.slice(0, i); | ||
let suffix = word.slice(i); | ||
|
||
if ( | ||
(wordSet.has(prefix) && wordSet.has(suffix)) || | ||
(wordSet.has(prefix) && dfs(suffix)) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |