| | 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.CheckIfTwoStringArraysAreEquivalent; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CheckIfTwoStringArraysAreEquivalentIterative : ICheckIfTwoStringArraysAreEquivalent |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + m) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="word1"></param> |
| | 22 | | /// <param name="word2"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public bool ArrayStringsAreEqual(string[] word1, string[] word2) |
| 7 | 25 | | { |
| 22 | 26 | | var word1CharsLength = word1.Select(w => w.Length).Sum(); |
| 21 | 27 | | var word2CharsLength = word2.Select(w => w.Length).Sum(); |
| | 28 | |
|
| 7 | 29 | | if (word1CharsLength != word2CharsLength) |
| 0 | 30 | | { |
| 0 | 31 | | return false; |
| | 32 | | } |
| | 33 | |
|
| 28 | 34 | | int wordIndex1 = 0, wordIndex2 = 0, charIndex1 = 0, charIndex2 = 0; |
| | 35 | |
|
| 90 | 36 | | for (var i = 0; i < word1CharsLength; i++) |
| 41 | 37 | | { |
| 41 | 38 | | if (charIndex1 > word1[wordIndex1].Length - 1) |
| 8 | 39 | | { |
| 8 | 40 | | wordIndex1++; |
| 8 | 41 | | charIndex1 = 0; |
| 8 | 42 | | } |
| | 43 | |
|
| 41 | 44 | | if (charIndex2 > word2[wordIndex2].Length - 1) |
| 6 | 45 | | { |
| 6 | 46 | | wordIndex2++; |
| 6 | 47 | | charIndex2 = 0; |
| 6 | 48 | | } |
| | 49 | |
|
| 41 | 50 | | var c1 = word1[wordIndex1][charIndex1]; |
| 41 | 51 | | var c2 = word2[wordIndex2][charIndex2]; |
| | 52 | |
|
| 41 | 53 | | if (c1 != c2) |
| 3 | 54 | | { |
| 3 | 55 | | return false; |
| | 56 | | } |
| | 57 | |
|
| 38 | 58 | | charIndex1++; |
| 38 | 59 | | charIndex2++; |
| 38 | 60 | | } |
| | 61 | |
|
| 4 | 62 | | return true; |
| 7 | 63 | | } |
| | 64 | | } |