-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path0604-design-compressed-string-iterator.js
More file actions
65 lines (57 loc) · 1.67 KB
/
0604-design-compressed-string-iterator.js
File metadata and controls
65 lines (57 loc) · 1.67 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
/**
* 604. Design Compressed String Iterator
* https://leetcode.com/problems/design-compressed-string-iterator/
* Difficulty: Easy
*
* Design and implement a data structure for a compressed string iterator. The given compressed
* string will be in the form of each letter followed by a positive integer representing the
* number of this letter existing in the original uncompressed string.
*
* Implement the StringIterator class:
* - next() Returns the next character if the original string still has uncompressed characters,
* otherwise returns a white space.
* - hasNext() Returns true if there is any letter needs to be uncompressed in the original string,
* otherwise returns false.
*/
/**
* @param {string} compressedString
*/
var StringIterator = function(compressedString) {
this.segments = [];
this.currentIndex = 0;
this.currentCount = 0;
let i = 0;
while (i < compressedString.length) {
const character = compressedString[i];
i++;
let count = '';
while (i < compressedString.length && /\d/.test(compressedString[i])) {
count += compressedString[i];
i++;
}
this.segments.push([character, parseInt(count)]);
}
};
/**
* @return {character}
*/
StringIterator.prototype.next = function() {
if (!this.hasNext()) {
return ' ';
}
if (this.currentCount === 0) {
this.currentCount = this.segments[this.currentIndex][1];
}
const character = this.segments[this.currentIndex][0];
this.currentCount--;
if (this.currentCount === 0) {
this.currentIndex++;
}
return character;
};
/**
* @return {boolean}
*/
StringIterator.prototype.hasNext = function() {
return this.currentIndex < this.segments.length;
};