|
| 1 | +- λ¬Έμ : https://leetcode.com/problems/longest-palindromic-substring/ |
| 2 | +- νμ΄: https://algorithm.jonghoonpark.com/2024/07/01/leetcode-5 |
| 3 | + |
| 4 | +## brute force λ°©μ (beats 12.41%) |
| 5 | + |
| 6 | +```java |
| 7 | +class Solution { |
| 8 | + public String longestPalindrome(String s) { |
| 9 | + int start = 0; |
| 10 | + int end = start; |
| 11 | + |
| 12 | + String max = ""; |
| 13 | + |
| 14 | + char[] charArray = s.toCharArray(); |
| 15 | + while (true) { |
| 16 | + if (end == s.length()) { |
| 17 | + break; |
| 18 | + } |
| 19 | + |
| 20 | + if (charArray[start] == charArray[end]) { |
| 21 | + int tempStart = start; |
| 22 | + int tempEnd = end; |
| 23 | + |
| 24 | + boolean isPalindrome = true; |
| 25 | + while (tempEnd >= tempStart) { |
| 26 | + if (charArray[tempStart] != charArray[tempEnd]) { |
| 27 | + isPalindrome = false; |
| 28 | + break; |
| 29 | + } |
| 30 | + if (tempStart == tempEnd) { |
| 31 | + break; |
| 32 | + } |
| 33 | + tempStart = tempStart + 1; |
| 34 | + tempEnd = tempEnd - 1; |
| 35 | + } |
| 36 | + |
| 37 | + if (isPalindrome) { |
| 38 | + String temp = s.substring(start, end + 1); |
| 39 | + if (temp.length() > max.length()) { |
| 40 | + max = temp; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + end++; |
| 46 | + if (end == s.length()) { |
| 47 | + start = start + 1; |
| 48 | + end = start; |
| 49 | + } |
| 50 | + |
| 51 | + if (start == s.length()) { |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return max; |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### TC, SC |
| 62 | + |
| 63 | +μκ° λ³΅μ‘λλ O(n^2)μ΄κ³ , κ³΅κ° λ³΅μ‘λλ O(n)μ΄λ€. |
| 64 | + |
| 65 | +## brute force λ°©μ κ°μ (beats 48.52%) |
| 66 | + |
| 67 | +μλ λΆλΆμ΄ λΆλΆμ΄λ€. |
| 68 | + |
| 69 | +```java |
| 70 | +start = start + 1; |
| 71 | +end = start + max.length(); |
| 72 | +``` |
| 73 | + |
| 74 | +λ€μ end κ°μ `start + max.length()` λ‘ λμ΄ μ°μ°μ λ§μ΄ μ€μΌ μ μμκ³ κ½€ ν° μ°¨μ΄κ° λ°μλλ€. |
| 75 | + |
| 76 | +```java |
| 77 | +class Solution { |
| 78 | + public String longestPalindrome(String s) { |
| 79 | + int start = 0; |
| 80 | + int end = start; |
| 81 | + |
| 82 | + String max = ""; |
| 83 | + |
| 84 | + char[] charArray = s.toCharArray(); |
| 85 | + while (start < s.length() && end < s.length()) { |
| 86 | + if (charArray[start] == charArray[end]) { |
| 87 | + int tempStart = start; |
| 88 | + int tempEnd = end; |
| 89 | + |
| 90 | + boolean isPalindrome = true; |
| 91 | + while (tempEnd >= tempStart) { |
| 92 | + if (charArray[tempStart] != charArray[tempEnd]) { |
| 93 | + isPalindrome = false; |
| 94 | + break; |
| 95 | + } |
| 96 | + if (tempStart == tempEnd) { |
| 97 | + break; |
| 98 | + } |
| 99 | + tempStart = tempStart + 1; |
| 100 | + tempEnd = tempEnd - 1; |
| 101 | + } |
| 102 | + |
| 103 | + if (isPalindrome) { |
| 104 | + String temp = s.substring(start, end + 1); |
| 105 | + if(temp.length() > max.length()) { |
| 106 | + max = temp; |
| 107 | + } |
| 108 | + end = end + 1; |
| 109 | + } else { |
| 110 | + if (s.indexOf(charArray[start], end) > -1) { |
| 111 | + end = end + 1; |
| 112 | + } else { |
| 113 | + start = start + 1; |
| 114 | + end = start + max.length(); |
| 115 | + } |
| 116 | + } |
| 117 | + } else { |
| 118 | + end++; |
| 119 | + if (end == s.length()) { |
| 120 | + start = start + 1; |
| 121 | + end = start + max.length(); |
| 122 | + } |
| 123 | + |
| 124 | + if (start == s.length()) { |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (end == s.length()) { |
| 130 | + start = start + 1; |
| 131 | + end = start + max.length(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return max; |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### TC, SC |
| 141 | + |
| 142 | +μκ° λ³΅μ‘λλ O(n^2)μ΄κ³ , κ³΅κ° λ³΅μ‘λλ O(n)μ΄λ€. |
| 143 | + |
| 144 | +λΉ
μ€ νκΈ°λ² μμΌλ‘λ λμΌνλ, μ€ν μκ°μ΄ λ§€μ° λ¨μΆλμλ€. |
| 145 | + |
| 146 | +## best solution (beats 95.84%) |
| 147 | + |
| 148 | +νλμ ν¬μΈν°λ₯Ό μ¬μ©νμ¬, κ° λ¬Έμλ₯Ό μ€μ¬μΌλ‘ Palindrome μ΄ λ°μν μ μλ μΌμ΄μ€λ₯Ό μ‘°μ¬νλ€. |
| 149 | + |
| 150 | +```java |
| 151 | +class Solution { |
| 152 | + public String longestPalindrome(String s) { |
| 153 | + String max = ""; |
| 154 | + |
| 155 | + char[] charArray = s.toCharArray(); |
| 156 | + for (int i = 0; i < s.length(); i++) { |
| 157 | + char currentChar = charArray[i]; |
| 158 | + int left = i; |
| 159 | + int right = i; |
| 160 | + |
| 161 | + while (right < s.length() - 1 && currentChar == charArray[right + 1]) { |
| 162 | + right++; |
| 163 | + } |
| 164 | + |
| 165 | + while (left > 0 && right < s.length() - 1 && charArray[left - 1] == charArray[right + 1]) { |
| 166 | + left--; |
| 167 | + right++; |
| 168 | + } |
| 169 | + |
| 170 | + String temp = s.substring(left, right + 1); |
| 171 | + if (temp.length() > max.length()) { |
| 172 | + max = temp; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return max; |
| 177 | + } |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +### TC, SC |
| 182 | + |
| 183 | +μκ° λ³΅μ‘λλ νκ· μ μΌλ‘ O(n)μ΄λ€. palindrome μ κΈΈμ΄κ° n μ κ°κΉμμ§μλ‘ μκ° λ³΅μ‘λλ O(n^2) μ κ°κΉμ μ§λ€. |
| 184 | +κ³΅κ° λ³΅μ‘λλ O(n)μ΄λ€. |
| 185 | + |
| 186 | +λΉ
μ€ νκΈ°λ² μμΌλ‘λ κ°μ μ΄ λ λ°©μμ΄λ€. μ€μ λ‘ μκ°λ λ λ¨μΆλμλ€. |
0 commit comments