Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2160 from AkifhanIlgaz/0472
Browse files Browse the repository at this point in the history
Create: 0472-concatenated-words
  • Loading branch information
a93a authored Jan 28, 2023
2 parents 626d865 + de141a7 commit b9a6d75
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
35 changes: 35 additions & 0 deletions go/0472-concatenated-words.go
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
}
36 changes: 36 additions & 0 deletions javascript/0472-concatenated-words.js
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;
}
};
29 changes: 29 additions & 0 deletions rust/0472-concatenated-words.rs
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
}
}
28 changes: 28 additions & 0 deletions typescript/0472-concatenated-words.ts
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;
}
}

0 comments on commit b9a6d75

Please sign in to comment.