-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReverseOnlyLetters.cs
68 lines (64 loc) · 2.4 KB
/
ReverseOnlyLetters.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 917. Reverse Only Letters
/// Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.
/// IE) Input: "Test1ng-Leet=code-Q!"
/// Output: "Qedo1ct-eeLg=ntse-T!"
/// </summary>
public class ReverseOnlyLetters : Solution
{
private string _inputString;
public ReverseOnlyLetters(string inputString)
{
this._inputString = inputString;
}
private string reverseOnlyLetters(string inputString)
{
// 1) Iterate through each char in the inputString
// if the char is a letter, push it onto a stack
// 2) Iterate through, each char in the inputString
// if the char is a letter, pop the stack and append that char the results string
// otherwise it is not a letter and can be placed in the string directly
// Time Complexity : O(n), where n is the length of inputString
// Space Complexity: O(n)
Stack<char> letters = new Stack<char>();
foreach (char character in inputString)
{
if (Char.IsLetter(character))
{
letters.Push(character);
}
}
// 3)
//StringBuilder result = new StringBuilder();
string result = "";
foreach (char character in inputString)
{
if (Char.IsLetter(character))
{
//result.Append()
result += letters.Pop();
}
else
{
result += character;
}
}
return result;
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = reverseOnlyLetters(this._inputString);
watch.Stop();
Console.WriteLine($"917. Reverse Only Letters\n" +
$"Input String = {this._inputString} \n" +
$"Result: [{results}] \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}