Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 314634c

Browse files
Add solution for Decode String problem with comprehensive test cases
1 parent 712d70f commit 314634c

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
- https://leetcode.com/problems/sort-colors/description/
5353
- Medium but Very Hard: https://leetcode.com/problems/3sum/
5454
- https://leetcode.com/problems/asteroid-collision/
55+
- https://leetcode.com/problems/decode-string/
5556

5657
### Hard Problems
5758

‎leetcode/394.decode_string.dart

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// https://leetcode.com/problems/decode-string/
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
final stopwatch = Stopwatch()..start();
6+
runTests();
7+
8+
final Solution s = Solution();
9+
s.decodeString("1[1[1[1[1[a]]]]]");
10+
11+
///
12+
stopwatch.stop();
13+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
14+
// Function Execution Time : 365384 micro s
15+
// Function Execution Time : 394717 micro s
16+
}
17+
18+
class Solution {
19+
////! Accepted from the first time
20+
////! Try to find another solution
21+
String decodeString(String s) {
22+
final StringBuffer sbResult = StringBuffer();
23+
String subString = "";
24+
final List<String> stack = [];
25+
for (var i = 0; i < s.length; i++) {
26+
final char = s[i];
27+
if (char == ']') {
28+
String lastChar = stack.removeLast();
29+
30+
/// Get the substring needed to be repeated
31+
/// Add all the chars in the [] in the substring to repeat
32+
while (lastChar != '[') {
33+
subString = lastChar + subString;
34+
lastChar = stack.removeLast();
35+
}
36+
lastChar = stack.last;
37+
int number = 0;
38+
int tens = 1;
39+
40+
/// Get the repeat number
41+
while (lastChar.startsWith(RegExp(r'[0-9]'))) {
42+
stack.removeLast();
43+
number += int.parse(lastChar) * tens;
44+
tens *= 10;
45+
if (stack.isNotEmpty) {
46+
lastChar = stack.last;
47+
} else {
48+
break;
49+
}
50+
}
51+
52+
/// Check if the stack is empty:
53+
/// Then I will add the substring to the result
54+
/// Else Then I need to repeat this again and add this to the stack
55+
if (stack.isNotEmpty) {
56+
stack.add(subString * number);
57+
} else {
58+
sbResult.write(subString * number);
59+
}
60+
subString = "";
61+
number = 0;
62+
tens = 1;
63+
} else if (char == '[') {
64+
stack.add(char);
65+
} else if (char.startsWith(RegExp(r'[0-9]'))) {
66+
stack.add(char);
67+
} else {
68+
/// If the string not in the [] then add it to the string result without repeat
69+
if (stack.isEmpty) {
70+
sbResult.write(char);
71+
} else {
72+
stack.add(char);
73+
}
74+
}
75+
}
76+
77+
return sbResult.toString();
78+
}
79+
}
80+
81+
void runTests() {
82+
final Solution s = Solution();
83+
84+
group('Decode String', () {
85+
// Basic examples from problem statement
86+
test('Example 1: "3[a]2[bc]" → "aaabcbc"', () {
87+
expect(s.decodeString("3[a]2[bc]"), equals("aaabcbc"));
88+
});
89+
90+
test('Example 2: "3[a2[c]]" → "accaccacc"', () {
91+
expect(s.decodeString("3[a2[c]]"), equals("accaccacc"));
92+
});
93+
94+
test('Example 3: "2[abc]3[cd]ef" → "abcabccdcdcdef"', () {
95+
expect(s.decodeString("2[abc]3[cd]ef"), equals("abcabccdcdcdef"));
96+
});
97+
98+
// Edge cases
99+
test('Single character: "a" → "a"', () {
100+
expect(s.decodeString("a"), equals("a"));
101+
});
102+
103+
test('Single repetition: "1[a]" → "a"', () {
104+
expect(s.decodeString("1[a]"), equals("a"));
105+
});
106+
107+
test('Multiple digits for k: "10[a]" → "aaaaaaaaaa"', () {
108+
expect(s.decodeString("10[a]"), equals("aaaaaaaaaa"));
109+
});
110+
111+
// Nested cases
112+
test('Double nesting: "2[3[a]b]" → "aaabaaab"', () {
113+
expect(s.decodeString("2[3[a]b]"), equals("aaabaaab"));
114+
});
115+
116+
test('Triple nesting: "2[a3[b2[c]]]" → "abccbccbccabccbccbcc"', () {
117+
expect(s.decodeString("2[a3[b2[c]]]"), equals("abccbccbccabccbccbcc"));
118+
});
119+
120+
// Multiple segments
121+
test('Multiple segments: "2[a]3[b]4[c]" → "aabbbcccc"', () {
122+
expect(s.decodeString("2[a]3[b]4[c]"), equals("aabbbcccc"));
123+
});
124+
125+
test('Mixed segments: "a2[b3[c]d]e" → "abcccdbcccde"', () {
126+
expect(s.decodeString("a2[b3[c]d]e"), equals("abcccdbcccde"));
127+
});
128+
129+
// Maximum constraints
130+
test('Maximum k value: "300[a]" → 300 a\'s', () {
131+
expect(s.decodeString("300[a]"), equals("a" * 300));
132+
});
133+
134+
test('Maximum nesting depth', () {
135+
expect(s.decodeString("1[1[1[1[1[a]]]]]"), equals("a"));
136+
});
137+
138+
// No encoding cases
139+
test('No encoding: "abc" → "abc"', () {
140+
expect(s.decodeString("abc"), equals("abc"));
141+
});
142+
143+
test('Empty brackets: "3[]" → ""', () {
144+
expect(s.decodeString("3[]"), equals(""));
145+
});
146+
147+
// Complex cases
148+
test('Complex case 1: "3[z]2[2[y]pq4[2[jk]e1[f]]]ef"', () {
149+
expect(
150+
s.decodeString("3[z]2[2[y]pq4[2[jk]e1[f]]]ef"),
151+
equals(
152+
"zzzyypqjkjkefjkjkefjkjkefjkjkefyypqjkjkefjkjkefjkjkefjkjkefef"));
153+
});
154+
155+
test('Complex case 2: "2[2[y]a3[b2[c]d]]"', () {
156+
expect(s.decodeString("2[2[y]a3[b2[c]d]]"),
157+
equals("yyabccdbccdbccdyyabccdbccdbccd"));
158+
});
159+
160+
// Random cases
161+
test('Random case 1: "2[a3[b]]" → "abbbabbb"', () {
162+
expect(s.decodeString("2[a3[b]]"), equals("abbbabbb"));
163+
});
164+
165+
test('Random case 2: "a2[bc3[d]]e" → "abcdddbcddde"', () {
166+
expect(s.decodeString("a2[bc3[d]]e"), equals("abcdddbcddde"));
167+
});
168+
});
169+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /