| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- using System.Text;
- namespace Day1;
- public class DigitTransformer
- {
- private static readonly Dictionary<string, string> _digits = new Dictionary<string, string>
- {
- ["one"] = "1",
- ["two"] = "2",
- ["three"] = "3",
- ["four"] = "4",
- ["five"] = "5",
- ["six"] = "6",
- ["seven"] = "7",
- ["eight"] = "8",
- ["nine"] = "9",
- };
- public string Replace(string input)
- {
- var result = new StringBuilder();
- for (var i = 0; i < input.Length; i++)
- {
- var current = input.Substring(i);
- var next = false;
- foreach (var kvp in _digits)
- {
- if (current.StartsWith(kvp.Key))
- {
- result.Append(kvp.Value);
- next = true;
- break;
- }
- }
- if (!next)
- {
- result.Append(current[0]);
- }
- }
- return result.ToString();
- }
- }
|