|
10 | 10 | ## :ballot_box_with_check: 프로그래머스 크레인 인형뽑기 게임(64061) |
11 | 11 |
|
12 | 12 | ## :ballot_box_with_check: 프로그래머스 가장 긴 팰린드롬(12904) |
| 13 | + |
| 14 | +#### 정확성: 62.7, 효율성: 30.7 |
| 15 | + |
| 16 | +- 테스트 6번 12번 실패 |
| 17 | + |
| 18 | +```js |
| 19 | +const isSameWord = (word, pre, next) => { |
| 20 | + if (pre < 0 || next >= word.length) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + if (word[pre] !== word[next]) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + return true; |
| 27 | +}; |
| 28 | + |
| 29 | +const oddPalindrome = (word, start) => { |
| 30 | + let index = 1; |
| 31 | + while (true) { |
| 32 | + const pre = start - index; |
| 33 | + const next = start + index; |
| 34 | + if (isSameWord(word, pre, next)) { |
| 35 | + index = index + 1; |
| 36 | + continue; |
| 37 | + } |
| 38 | + index = index - 1; |
| 39 | + break; |
| 40 | + } |
| 41 | + return index * 2 + 1; |
| 42 | +}; |
| 43 | + |
| 44 | +const evenPalindrome = (word, start) => { |
| 45 | + let index = 0; |
| 46 | + while (true) { |
| 47 | + const pre = start - index; |
| 48 | + const next = start + 1 + index; |
| 49 | + if (isSameWord(word, pre, next)) { |
| 50 | + index = index + 1; |
| 51 | + continue; |
| 52 | + } |
| 53 | + break; |
| 54 | + } |
| 55 | + return index * 2; |
| 56 | +}; |
| 57 | + |
| 58 | +const isPalindrome = (word, start) => { |
| 59 | + let length = oddPalindrome(word, start); |
| 60 | + if (length === 1) { |
| 61 | + const result = evenPalindrome(word, start); |
| 62 | + if (result > length) { |
| 63 | + length = result; |
| 64 | + } |
| 65 | + } |
| 66 | + return length; |
| 67 | +}; |
| 68 | + |
| 69 | +const solution = (s) => { |
| 70 | + let maxLength = 0; |
| 71 | + for (let index = 0; index < s.length; index++) { |
| 72 | + const result = isPalindrome(s, index); |
| 73 | + if (maxLength < result) { |
| 74 | + maxLength = result; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return maxLength; |
| 79 | +}; |
| 80 | +``` |
0 commit comments