Skip to content

Commit

Permalink
Fix word in words DP solution
Browse files Browse the repository at this point in the history
  • Loading branch information
feserr committed Nov 19, 2023
1 parent 76bab8e commit 30ece7f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
22 changes: 11 additions & 11 deletions coding_challenges/words_in_word/assets/input.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
rock
star
rockstar
foo
bar
foobara
fo
oba
ra
f
oo
rock
star
rockstar
foo
bar
foobara
fo
oba
ra
f
oo
24 changes: 10 additions & 14 deletions coding_challenges/words_in_word/src/WordsInWordsDP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#ifndef CODING_CHALLENGES_WORDS_IN_WORD_SRC_WORDSINWORDSDP_HPP_
#define CODING_CHALLENGES_WORDS_IN_WORD_SRC_WORDSINWORDSDP_HPP_

#include <ranges>

#include <algorithm>
#include <ranges>
#include <string>
#include <unordered_map>
#include <utility>
Expand Down Expand Up @@ -37,31 +36,28 @@ class WordsInWordsDP : public IWordsInWord {
const auto words_for_letter_iterator = words_start_at_letter_.find(letter);
if (words_for_letter_iterator == words_start_at_letter_.end()) continue;

// Check the target in the dictionary.
for (const auto& word_for_letter : words_for_letter_iterator->second) {
// Continue if the word is the same as the target.
if (word_for_letter == word) continue;

// Continue if the word is greater than the target.
int index_for_word = index + std::ssize(word_for_letter);
if (index_for_word > word.size()) continue;

bool valid = true;
for (int index_of_word = 0; std::cmp_less(index_of_word, word_for_letter.size());
++index_of_word) {
if (word_for_letter[index_of_word] != word[index + index_of_word]) {
valid = false;
break;
}
}
if (!valid) continue;
// Continue if the word is not part of the substring of the target.
const auto s_substr = word.substr(index, size(word_for_letter));
if (s_substr != word_for_letter) continue;

std::for_each(word_composition[index].begin(), word_composition[index].end(),
[&](const auto& composition_at) {
word_composition[index_for_word].emplace_back(composition_at);
auto new_composition = composition_at + " " + word_for_letter;
word_composition[index_for_word].emplace_back(new_composition);
});
word_composition[index_for_word].emplace_back(word_for_letter);
}
}

if (!word_composition[word.size()].empty()) {
if (!word_composition[std::size(word)].empty()) {
result.emplace_back(word, word_composition[word.size()]);
}
}
Expand Down

0 comments on commit 30ece7f

Please sign in to comment.