648. Replace Words #168
-
Topics: In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root Given a Return the Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We can approach this using a Trie (prefix tree) to store the dictionary of roots. Then, we process each word in the sentence, replacing it with the shortest root if it exists in the Trie. Approach:
Let's implement this solution in PHP: 648. Replace Words <?php
// Define a Trie Node class
class TrieNode {
/**
* @var array
*/
public $children = [];
/**
* @var bool
*/
public $isEndOfWord = false; // Marks the end of a root word
}
// Define a Trie class for storing the dictionary of roots
class Trie {
/**
* @var TrieNode
*/
private $root;
public function __construct() {
$this->root = new TrieNode();
}
/**
* Insert a word (root) into the Trie
*
* @param $word
* @return void
*/
public function insert($word) {
$node = $this->root;
for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
if (!isset($node->children[$char])) {
$node->children[$char] = new TrieNode();
}
$node = $node->children[$char];
}
$node->isEndOfWord = true; // Mark the end of the root word
}
/**
* Find the shortest root for a given word
*
* @param $word
* @return mixed|string
*/
public function findRoot($word) {
$node = $this->root;
$prefix = "";
for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
if (!isset($node->children[$char])) {
break;
}
$prefix .= $char;
$node = $node->children[$char];
if ($node->isEndOfWord) {
return $prefix; // Return the root if found
}
}
return $word; // Return the original word if no root is found
}
}
/**
* Function to replace words in the sentence
*
* @param String[] $dictionary
* @param String $sentence
* @return String
*/
function replaceWords($dictionary, $sentence) {
// Step 1: Build the Trie with the dictionary of roots
$trie = new Trie();
foreach ($dictionary as $root) {
$trie->insert($root);
}
// Step 2: Split the sentence into words
$words = explode(" ", $sentence);
// Step 3: For each word, find the root and replace the word if needed
for ($i = 0; $i < count($words); $i++) {
$words[$i] = $trie->findRoot($words[$i]);
}
// Step 4: Join the words back into a sentence
return implode(" ", $words);
}
// Example usage:
// Example 1:
$dictionary = ["cat", "bat", "rat"];
$sentence = "the cattle was rattled by the battery";
$result = replaceWords($dictionary, $sentence);
echo $result . "\n"; // Output: "the cat was rat by the bat"
// Example 2:
$dictionary2 = ["a", "b", "c"];
$sentence2 = "aadsfasf absbs bbab cadsfafs";
$result2 = replaceWords($dictionary2, $sentence2);
echo $result2 . "\n"; // Output: "a a b c"
?> Explanation:
Example 1:Input: $dictionary = ["cat", "bat", "rat"];
$sentence = "the cattle was rattled by the battery"; Process:
Output:
Example 2:Input: $dictionary = ["a", "b", "c"];
$sentence = "aadsfasf absbs bbab cadsfafs"; Output:
Time Complexity:
Thus, the overall time complexity is (O(N \cdot L + M \cdot L)), which is efficient given the constraints. Space Complexity:The space complexity is (O(N \cdot L)) for storing the Trie. |
Beta Was this translation helpful? Give feedback.
We can approach this using a Trie (prefix tree) to store the dictionary of roots. Then, we process each word in the sentence, replacing it with the shortest root if it exists in the Trie.
Approach:
Trie Structure:
Processing the Sentence:
Efficiency: