-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_string.dart
More file actions
34 lines (28 loc) · 897 Bytes
/
Copy pathdecode_string.dart
File metadata and controls
34 lines (28 loc) · 897 Bytes
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
// https://leetcode.com/problems/decode-string/
class DecodeString {
String decodeString(String s) {
final repeatStack = <int>[];
final stringStack = <String>[];
var currentString = '';
var currentCount = 0;
for (var i = 0; i < s.length; i++) {
final ch = s[i];
final codeUnit = ch.codeUnitAt(0);
if (codeUnit >= 48 && codeUnit <= 57) {
currentCount = currentCount * 10 + int.parse(ch);
} else if (ch == '[') {
repeatStack.add(currentCount);
stringStack.add(currentString);
currentString = '';
currentCount = 0;
} else if (ch == ']') {
final repeatTimes = repeatStack.removeLast();
final prevString = stringStack.removeLast();
currentString = prevString + currentString * repeatTimes;
} else {
currentString += ch;
}
}
return currentString;
}
}