| | 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.MovePiecesToObtainString; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MovePiecesToObtainStringTwoPointers : IMovePiecesToObtainString |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="start"></param> |
| | 22 | | /// <param name="target"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public bool CanChange(string start, string target) |
| 3 | 25 | | { |
| 3 | 26 | | var startIndex = 0; |
| 3 | 27 | | var targetIndex = 0; |
| | 28 | |
|
| 6 | 29 | | while (startIndex < start.Length && targetIndex < target.Length) |
| 5 | 30 | | { |
| 11 | 31 | | while (startIndex < start.Length && start[startIndex] == '_') |
| 6 | 32 | | { |
| 6 | 33 | | startIndex++; |
| 6 | 34 | | } |
| | 35 | |
|
| 13 | 36 | | while (targetIndex < target.Length && target[targetIndex] == '_') |
| 8 | 37 | | { |
| 8 | 38 | | targetIndex++; |
| 8 | 39 | | } |
| | 40 | |
|
| 5 | 41 | | if (startIndex == start.Length && targetIndex == target.Length) |
| 0 | 42 | | { |
| 0 | 43 | | return true; |
| | 44 | | } |
| | 45 | |
|
| 5 | 46 | | if (startIndex == start.Length || targetIndex == target.Length) |
| 0 | 47 | | { |
| 0 | 48 | | return false; |
| | 49 | | } |
| | 50 | |
|
| 5 | 51 | | if (start[startIndex] != target[targetIndex]) |
| 1 | 52 | | { |
| 1 | 53 | | return false; |
| | 54 | | } |
| | 55 | |
|
| 4 | 56 | | if (start[startIndex] == 'L' && targetIndex > startIndex) |
| 0 | 57 | | { |
| 0 | 58 | | return false; |
| | 59 | | } |
| | 60 | |
|
| 4 | 61 | | if (start[startIndex] == 'R' && targetIndex < startIndex) |
| 1 | 62 | | { |
| 1 | 63 | | return false; |
| | 64 | | } |
| | 65 | |
|
| 3 | 66 | | startIndex++; |
| 3 | 67 | | targetIndex++; |
| 3 | 68 | | } |
| | 69 | |
|
| 2 | 70 | | while (startIndex < start.Length && start[startIndex] == '_') |
| 1 | 71 | | { |
| 1 | 72 | | startIndex++; |
| 1 | 73 | | } |
| | 74 | |
|
| 1 | 75 | | while (targetIndex < target.Length && target[targetIndex] == '_') |
| 0 | 76 | | { |
| 0 | 77 | | targetIndex++; |
| 0 | 78 | | } |
| | 79 | |
|
| 1 | 80 | | return startIndex == start.Length && targetIndex == target.Length; |
| 3 | 81 | | } |
| | 82 | | } |