Last active
December 12, 2025 16:30
-
-
Save DimsFromDergachy/28e501cb57557eaf070a67eac22e5e8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static class KeyBoard | |
| { | |
| internal static IEnumerable<char> UnEscape(string a) | |
| { | |
| // Stacks keep good indexes | |
| var lowers = new Stack<(int, char chr)>(a.Count()); | |
| var uppers = new Stack<(int, char chr)>(a.Count()); | |
| for (int i = 0; i < a.Count(); i++) | |
| { | |
| var ch = a[i]; | |
| switch (ch) | |
| { | |
| case 'b': | |
| lowers.TryPop(out _); break; | |
| case 'B': | |
| uppers.TryPop(out _); break; | |
| case var o when char.IsLower(o): | |
| lowers.Push((i, ch)); break; | |
| case var o when char.IsUpper(o): | |
| uppers.Push((i, ch)); break; | |
| default: | |
| throw new NotSupportedException($"Unsupported character: '{ch}'"); | |
| } | |
| } | |
| var result = new Stack<char>(a.Length); | |
| while (lowers.Any() || uppers.Any()) | |
| { | |
| if (!lowers.Any()) | |
| { | |
| result.Push(uppers.Pop().chr); continue; | |
| } | |
| if (!uppers.Any()) | |
| { | |
| result.Push(lowers.Pop().chr); continue; | |
| } | |
| (int i1, _) = lowers.Peek(); | |
| (int i2, _) = uppers.Peek(); | |
| if (i1 > i2) | |
| { | |
| result.Push(lowers.Pop().chr); continue; | |
| } | |
| if (i1 < i2) | |
| { | |
| result.Push(uppers.Pop().chr); continue; | |
| } | |
| throw new NotSupportedException("Broken invariant"); | |
| } | |
| return result.ToArray(); | |
| } | |
| } | |
| public class KeyBoardTest | |
| { | |
| [Theory] | |
| [InlineData("abcd", "cd")] | |
| [InlineData("ABCD", "CD")] | |
| [InlineData("abba", "a")] | |
| [InlineData("ABBA", "A")] | |
| [InlineData("aAbB", "")] | |
| [InlineData("xXyYzZbB", "xXyY")] | |
| [InlineData("xXyYBbzZ", "xXzZ")] | |
| [InlineData("abcdefgijklmnopqrstuvwxyz", "cdefgijklmnopqrstuvwxyz")] | |
| [InlineData("ABCDEFGIJKLMNOPQRSTUVWXYZ", "CDEFGIJKLMNOPQRSTUVWXYZ")] | |
| public void UnEscape(string a, string e) | |
| { | |
| Assert.Equal(e, new string(KeyBoard.UnEscape(a).ToArray())); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Kreator22 по мотивам статьи на Habr тоже решил попробовать решить примеры
https://github.com/insanity13/sandbox/tree/main/Sandbox/Keyboard
Для строк до 20000 получил следующие результаты