-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeString.csx
More file actions
101 lines (81 loc) · 2.06 KB
/
Copy pathDecodeString.csx
File metadata and controls
101 lines (81 loc) · 2.06 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Question
// Given an encoded string, return its decoded string.
// The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times.
// #stack
// works only for some cases of non nested count and strings, except - 3[a2[c]]
public string DecodeString(string s)
{
var output = new StringBuilder();
string count = ""; string t = "";
int i = 0;
while (i < s.Length)
{
if (char.IsDigit(s[i]))
count = count + s[i];
else if (char.IsLetter(s[i])) t = t + s[i];
else if (s[i] == ']')
{
int repeat = count != "" ? int.Parse(count) : 1;
for (int j = 0; j < repeat; j++)
{
output.Append(t);
}
Console.WriteLine("Count - " + repeat + ", string - " + t);
count = "";
t = "";
}
i++;
}
if (t != "")
{
output.Append(t);
}
return output.ToString();
}
// Main solution using stack, works for all cases
public string DecodeString(string s)
{
var stack = new Stack<char>();
int n; string t = "";
for (int i = 0; i < s.Length; i++)
{
if (s[i] != ']')
stack.Push(s[i]);
else
{
t = GetString(stack);
n = GetCount(stack);
ExpandAndPush(stack, t, n);
}
}
return new string(stack.Reverse().ToArray());
}
string GetString(Stack<char> stack)
{
var s = new StringBuilder();
while (stack.Count > 0 && stack.Peek() != '[')
{
s.Insert(0, stack.Pop());
}
stack.Pop();
return s.ToString();
}
int GetCount(Stack<char> stack)
{
var count = new StringBuilder();
while (stack.Count > 0 && char.IsDigit(stack.Peek()))
{
count.Insert(0, stack.Pop());
}
return count.Length != 0 ? int.Parse(count.ToString()) : 1;
}
void ExpandAndPush(Stack<char> stack, string s, int n)
{
for (int i = 0; i < n; i++)
{
foreach (char c in s)
{
stack.Push(c);
}
}
}