| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.ReplaceWords; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ReplaceWordsBruteForce : IReplaceWords |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m + n log n + k * n * L), where n is the number of words in the dictionary, m is the len |
| | 19 | | /// the sentence, k is the number of words in the sentence, and L is the length of the longest word |
| | 20 | | /// Space complexity - O(m + n), where n is the number of words in the dictionary, m is the length of the senten |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="dictionary"></param> |
| | 23 | | /// <param name="sentence"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public string ReplaceWords(IList<string> dictionary, string sentence) |
| 6 | 26 | | { |
| 6 | 27 | | var words = sentence.Split(' '); |
| | 28 | |
|
| 68 | 29 | | for (var i = 0; i < words.Length; i++) |
| 28 | 30 | | { |
| 28 | 31 | | var word = words[i]; |
| | 32 | |
|
| 175 | 33 | | foreach (var dictionaryWord in dictionary.Order()) |
| 55 | 34 | | { |
| 55 | 35 | | if (!word.StartsWith(dictionaryWord)) |
| 36 | 36 | | { |
| 36 | 37 | | continue; |
| | 38 | | } |
| | 39 | |
|
| 19 | 40 | | words[i] = dictionaryWord; |
| | 41 | |
|
| 19 | 42 | | break; |
| | 43 | | } |
| 28 | 44 | | } |
| | 45 | |
|
| 6 | 46 | | return string.Join(' ', words); |
| 6 | 47 | | } |
| | 48 | | } |