forked from ManishGupta1908/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.ts
28 lines (23 loc) · 730 Bytes
/
res.ts
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
function countAndSay(n: number): string {
let result = '1';
for (let i = 1; i < n; i++) {
let newResult: string = '';
let stackList: string[] = result.split('');
let currentCount = 1;
let currentChar = stackList[0];
for (let j = 1; j < stackList.length; j++) {
if (currentChar === stackList[j]) {
currentCount++;
} else {
newResult += `${currentCount}${currentChar}`;
currentChar = stackList[j];
currentCount = 1;
}
}
if (currentCount) {
newResult += `${currentCount}${currentChar}`;
}
result = newResult;
}
return result;
};