| | 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 IsSubsequenceIterative : 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 | | if (t.Length == 0) |
| 1 | 32 | | { |
| 1 | 33 | | return false; |
| | 34 | | } |
| | 35 | |
|
| 5 | 36 | | var tIndex = 0; |
| 5 | 37 | | var sIndex = 0; |
| | 38 | |
|
| 18 | 39 | | while (tIndex < t.Length && sIndex < s.Length) |
| 14 | 40 | | { |
| 14 | 41 | | var isFound = false; |
| | 42 | |
|
| 1041 | 43 | | while (tIndex < t.Length && !isFound) |
| 1027 | 44 | | { |
| 1027 | 45 | | if (s[sIndex] == t[tIndex]) |
| 13 | 46 | | { |
| 13 | 47 | | isFound = true; |
| 13 | 48 | | } |
| | 49 | |
|
| 1027 | 50 | | tIndex++; |
| 1027 | 51 | | } |
| | 52 | |
|
| 14 | 53 | | if (!isFound) |
| 1 | 54 | | { |
| 1 | 55 | | return false; |
| | 56 | | } |
| | 57 | |
|
| 13 | 58 | | sIndex++; |
| 13 | 59 | | } |
| | 60 | |
|
| 4 | 61 | | return sIndex == s.Length; |
| 7 | 62 | | } |
| | 63 | | } |