| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.SplitStringsBySeparator; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class SplitStringsBySeparatorIterative : ISplitStringsBySeparator |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="words"></param> |
| | | 22 | | /// <param name="separator"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public IList<string> SplitWordsBySeparator(IList<string> words, char separator) |
| | 3 | 25 | | { |
| | 3 | 26 | | var result = new List<string>(); |
| | | 27 | | |
| | 21 | 28 | | foreach (var word in words) |
| | 6 | 29 | | { |
| | 6 | 30 | | var span = word.AsSpan(); |
| | | 31 | | |
| | 6 | 32 | | var wordStart = 0; |
| | | 33 | | |
| | 98 | 34 | | for (var i = 0; i < span.Length; i++) |
| | 43 | 35 | | { |
| | 43 | 36 | | var c = span[i]; |
| | | 37 | | |
| | 43 | 38 | | if (c != separator) |
| | 33 | 39 | | { |
| | 33 | 40 | | continue; |
| | | 41 | | } |
| | | 42 | | |
| | 10 | 43 | | var wordLength = i - wordStart; |
| | | 44 | | |
| | 10 | 45 | | if (wordLength > 0) |
| | 5 | 46 | | { |
| | 5 | 47 | | result.Add(span[wordStart..i].ToString()); |
| | 5 | 48 | | } |
| | | 49 | | |
| | 10 | 50 | | wordStart = i + 1; |
| | 10 | 51 | | } |
| | | 52 | | |
| | 6 | 53 | | if (wordStart < word.Length) |
| | 3 | 54 | | { |
| | 3 | 55 | | result.Add(word[wordStart..]); |
| | 3 | 56 | | } |
| | 6 | 57 | | } |
| | | 58 | | |
| | 3 | 59 | | return result; |
| | 3 | 60 | | } |
| | | 61 | | } |