-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabbling.js
69 lines (61 loc) · 2.01 KB
/
babbling.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
/*
옹알이
*Node.js에서 실행 시 replaceAll()이 실행 불가(함수 제공X)
*프로그래머스 터미널에서는 정상 실행
*/
// 1. 실패 : 87.5 / 100
function solution1(babbling) {
let babble = ["aya", "ye", "woo", "ma"];
let answer = 0;
babbling.map(word => {
babble.map((b, i) => {
let un = word.slice(i, i + b.length * 2);
if (un !== b + b) {
word = word.replace(b, "");
}
});
if (word.length == 0) return answer++;
});
return answer;
}
// 2. 아래 코드가 정답으로 처리가 되었지만,(...?)
// ["ayayewoomawooma"]이 경우에는 통과가 안됨
function solution2(babbling) {
let babble = ["aya", "ye", "woo", "ma"];
return babbling.reduce((answer, word) => {
babble.map(b => {
word = word.replace(b + b, "V");
word = word.replaceAll(b, "");
});
return word.length === 0 ? ++answer : answer;
}, 0);
}
// 3. 이 경우에는 ["ayayewoomawooma"] 테스트가 통과됨
function solution3(babbling) {
let babble = ["aya", "ye", "woo", "ma"];
let unbabbl = ["ayaaya", "yeye", "woowoo", "mama"];
return babbling.reduce((answer, word) => {
unbabbl.map((b, i) => {
word = word.replace(b, "V");
});
babble.map((b, i) => {
word = word.replaceAll(b, "");
});
return word.length === 0 ? ++answer : answer;
}, 0);
}
// 4. 정규식 이용한 방법(다른사람 코드 참고) - 옹알이2에서도 정답 처리
function solution4(babbling) {
const reg1 = /(aya|ye|woo|ma)\1+/;
const reg2 = /^(aya|ye|woo|ma)+$/;
// const reg3 = /^(aya(?!(aya))|ye(?!(ye))|woo(?!(woo))|ma(?!(ma)))+$"/; // 1, 2 합친 정규식
return babbling.reduce(
(answer, word) => (!reg1.test(word) && reg2.test(word) ? ++answer : answer),
0
);
}
let babbling = ["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]; // 2
console.log("solution1", solution1(babbling));
console.log("solution2", solution2(babbling));
console.log("solution3", solution3(babbling));
console.log("solution4", solution4(babbling));