| | 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.IsSubsequence; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class IsSubsequenceTwoPointers : IIsSubsequence |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + m) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="s"></param> |
| | 22 | | /// <param name="t"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public bool IsSubsequence(string s, string t) |
| 7 | 25 | | { |
| 7 | 26 | | if (s.Length == 0) |
| 1 | 27 | | { |
| 1 | 28 | | return true; |
| | 29 | | } |
| | 30 | |
|
| 6 | 31 | | var sIndex = 0; |
| 6 | 32 | | var tIndex = 0; |
| | 33 | |
|
| 1031 | 34 | | while (tIndex < t.Length) |
| 1027 | 35 | | { |
| 1027 | 36 | | if (t[tIndex] == s[sIndex]) |
| 13 | 37 | | { |
| 13 | 38 | | sIndex++; |
| | 39 | |
|
| 13 | 40 | | if (sIndex == s.Length) |
| 2 | 41 | | { |
| 2 | 42 | | return true; |
| | 43 | | } |
| 11 | 44 | | } |
| | 45 | |
|
| 1025 | 46 | | tIndex++; |
| 1025 | 47 | | } |
| | 48 | |
|
| 4 | 49 | | return false; |
| 7 | 50 | | } |
| | 51 | | } |