| | 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.DivideStringIntoGroupsOfSizeK; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class DivideStringIntoGroupsOfSizeKSimulation : IDivideStringIntoGroupsOfSizeK |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="s"></param> |
| | 24 | | /// <param name="k"></param> |
| | 25 | | /// <param name="fill"></param> |
| | 26 | | /// <returns></returns> |
| | 27 | | public string[] DivideString(string s, int k, char fill) |
| 2 | 28 | | { |
| 2 | 29 | | var groupsCount = (int)Math.Ceiling(s.Length / (double)k); |
| | 30 | |
|
| 2 | 31 | | var result = new string[groupsCount]; |
| | 32 | |
|
| 2 | 33 | | var sIndex = 0; |
| | 34 | |
|
| 18 | 35 | | for (var i = 0; i < groupsCount; i++) |
| 7 | 36 | | { |
| 7 | 37 | | var stringBuilder = new StringBuilder(); |
| | 38 | |
|
| 56 | 39 | | for (var j = 0; j < k; j++) |
| 21 | 40 | | { |
| 21 | 41 | | if (sIndex < s.Length) |
| 19 | 42 | | { |
| 19 | 43 | | stringBuilder.Append(s[sIndex]); |
| | 44 | |
|
| 19 | 45 | | sIndex++; |
| 19 | 46 | | } |
| | 47 | | else |
| 2 | 48 | | { |
| 2 | 49 | | stringBuilder.Append(fill); |
| 2 | 50 | | } |
| 21 | 51 | | } |
| | 52 | |
|
| 7 | 53 | | result[i] = stringBuilder.ToString(); |
| 7 | 54 | | } |
| | 55 | |
|
| 2 | 56 | | return result; |
| 2 | 57 | | } |
| | 58 | | } |