| | 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.FindTheKthCharacterInStringGame1; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class FindTheKthCharacterInStringGame1StringBuilder : IFindTheKthCharacterInStringGame1 |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(k) |
| | 21 | | /// Space complexity - O(k) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="k"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public char KthCharacter(int k) |
| 2 | 26 | | { |
| 2 | 27 | | var stringBuilder = new StringBuilder("a"); |
| | 28 | |
|
| 9 | 29 | | while (stringBuilder.Length < k) |
| 7 | 30 | | { |
| 7 | 31 | | var length = stringBuilder.Length; |
| | 32 | |
|
| 58 | 33 | | for (var i = 0; i < length; i++) |
| 22 | 34 | | { |
| 22 | 35 | | var nextChar = NextChar(stringBuilder[i]); |
| | 36 | |
|
| 22 | 37 | | stringBuilder.Append(nextChar); |
| 22 | 38 | | } |
| 7 | 39 | | } |
| | 40 | |
|
| 2 | 41 | | return stringBuilder[k - 1]; |
| 2 | 42 | | } |
| | 43 | |
|
| | 44 | | private static char NextChar(char c) |
| 22 | 45 | | { |
| 22 | 46 | | return c == 'z' ? 'a' : (char)(c + 1); |
| 22 | 47 | | } |
| | 48 | | } |