-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ5525.js
80 lines (63 loc) · 1.35 KB
/
BOJ5525.js
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
// Solution 1
function solution1(n, m, s) {
let pCount = 0;
let count = 0;
let index = 1;
while (index < m - 1) {
if (s[index - 1] === 'I' && s[index] === 'O' && s[index + 1] === 'I') {
count += 1;
if (count === n) {
count -= 1;
pCount += 1;
}
index += 1;
} else {
count = 0;
}
index += 1;
}
return pCount;
}
// Solution 2
function solution2(n, s) {
const len = 2 * n + 1;
let p = '';
let pCount = 0;
for (let i = 1; i <= len; i++) {
i % 2 !== 0 ? (p += 'I') : (p += 'O');
}
for (let i = 0; i <= s.length - len; i++) {
if (s.substring(i, i + len) === p) {
pCount += 1;
}
}
return pCount;
}
// Solution 3
function solution3(n, s) {
const len = 2 * n + 1;
let p = '';
let pCount = 0;
let index = 0;
for (let i = 1; i <= len; i++) {
i % 2 !== 0 ? (p += 'I') : (p += 'O');
}
while (true) {
const targetIndex = s.indexOf(p, index);
if (targetIndex === -1) {
break;
} else {
pCount += 1;
index = targetIndex + 1;
}
}
return pCount;
}
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const n = parseInt(input[0]);
const m = parseInt(input[1]);
const s = input[2];
console.log(solution1(n, m, s));
console.log(solution2(n, s));
console.log(solution3(n, s));