| | 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 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.RemoveAllOccurrencesOfSubstring; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class RemoveAllOccurrencesOfSubstringStringBuilder : IRemoveAllOccurrencesOfSubstring |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="s"></param> |
| | 24 | | /// <param name="part"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public string RemoveOccurrences(string s, string part) |
| 3 | 27 | | { |
| 3 | 28 | | var stringBuilder = new StringBuilder(); |
| | 29 | |
|
| 129 | 30 | | foreach (var c in s) |
| 60 | 31 | | { |
| 60 | 32 | | stringBuilder.Append(c); |
| | 33 | |
|
| 60 | 34 | | if (stringBuilder.Length >= part.Length && |
| 60 | 35 | | stringBuilder.ToString(stringBuilder.Length - part.Length, part.Length) == part) |
| 9 | 36 | | { |
| 9 | 37 | | stringBuilder.Length -= part.Length; |
| 9 | 38 | | } |
| 60 | 39 | | } |
| | 40 | |
|
| 3 | 41 | | return stringBuilder.ToString(); |
| 3 | 42 | | } |
| | 43 | | } |