| | 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.SentenceSimilarity3; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class SentenceSimilarity3TwoPointers : ISentenceSimilarity3 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(min(n, m)) |
| | 19 | | /// Space complexity - O(n + m) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="sentence1"></param> |
| | 22 | | /// <param name="sentence2"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public bool AreSentencesSimilar(string sentence1, string sentence2) |
| 11 | 25 | | { |
| 11 | 26 | | var sentence1Words = sentence1.Split(' '); |
| 11 | 27 | | var sentence2Words = sentence2.Split(' '); |
| | 28 | |
|
| 11 | 29 | | var sentence1Left = 0; |
| 11 | 30 | | var sentence1Right = sentence1Words.Length - 1; |
| | 31 | |
|
| 11 | 32 | | var sentence2Left = 0; |
| 11 | 33 | | var sentence2Right = sentence2Words.Length - 1; |
| | 34 | |
|
| 25 | 35 | | while (sentence1Left <= sentence1Right && sentence2Left <= sentence2Right) |
| 20 | 36 | | { |
| 20 | 37 | | var word1Left = sentence1Words[sentence1Left]; |
| 20 | 38 | | var word1Right = sentence1Words[sentence1Right]; |
| | 39 | |
|
| 20 | 40 | | var word2Left = sentence2Words[sentence2Left]; |
| 20 | 41 | | var word2Right = sentence2Words[sentence2Right]; |
| | 42 | |
|
| 20 | 43 | | if (word1Left == word2Left) |
| 9 | 44 | | { |
| 9 | 45 | | sentence1Left++; |
| 9 | 46 | | sentence2Left++; |
| 9 | 47 | | } |
| 11 | 48 | | else if (word1Right == word2Right) |
| 5 | 49 | | { |
| 5 | 50 | | sentence1Right--; |
| 5 | 51 | | sentence2Right--; |
| 5 | 52 | | } |
| | 53 | | else |
| 6 | 54 | | { |
| 6 | 55 | | return false; |
| | 56 | | } |
| 14 | 57 | | } |
| | 58 | |
|
| 5 | 59 | | return true; |
| 11 | 60 | | } |
| | 61 | | } |