728x90
๋ฐ์ํ
๋ฌธ์ ์ค๋ช
์๋ค๋ฅผ ๋ค์ง์ด๋ ๋๊ฐ์ ๋ฌธ์์ด์ ํฐ๋ฆฐ๋๋กฌ(palindrome)์ด๋ผ๊ณ ํฉ๋๋ค.
๋ฌธ์์ด s๊ฐ ์ฃผ์ด์ง ๋, s์ ๋ถ๋ถ๋ฌธ์์ด(Substring)์ค ๊ฐ์ฅ ๊ธด ํฐ๋ฆฐ๋๋กฌ์ ๊ธธ์ด๋ฅผ return ํ๋ solution ํจ์๋ฅผ ์์ฑํด ์ฃผ์ธ์.
์๋ฅผ๋ค๋ฉด, ๋ฌธ์์ด s๊ฐ "abcdcba"์ด๋ฉด 7์ returnํ๊ณ "abacde"์ด๋ฉด 3์ returnํฉ๋๋ค.
์ ํ ์ฌํญ
- ๋ฌธ์์ด s์ ๊ธธ์ด : 2,500 ์ดํ์ ์์ฐ์
- ๋ฌธ์์ด s๋ ์ํ๋ฒณ ์๋ฌธ์๋ก๋ง ๊ตฌ์ฑ
์ ์ถ๋ ฅ ์
s | answer |
"abcdcba" | 7 |
"abacde" | 3 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์
์ถ๋ ฅ ์ #1
4๋ฒ์งธ์๋ฆฌ 'd'๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด s ์ ์ฒด๊ฐ ํฐ๋ฆฐ๋๋กฌ์ด ๋๋ฏ๋ก 7์ returnํฉ๋๋ค.
์
์ถ๋ ฅ ์ #2
2๋ฒ์งธ์๋ฆฌ 'b'๋ฅผ ๊ธฐ์ค์ผ๋ก "aba"๊ฐ ํฐ๋ฆฐ๋๋กฌ์ด ๋๋ฏ๋ก 3์ returnํฉ๋๋ค.
ํ์ด
- ๋จธ๋ฆฌ๊ฐ ์ ๋์๊ฐ๊ณ ๋ฌด์ง์ฑ ์ฝ๋ฉํ๊ณ ์ถ์ด์ ๋ฌธ์ ๊ธธ์ด๊ฐ ์งง์ ๊ฑธ ๊ณจ๋ผ๋ณด์๋ค.
- ๋ญ๊ฐ ์ด ๊ฐ๋จํ ๋ฌธ์ ๊ฐ์์ ๋ฌด์ง์ฑ์ผ๋ก ๋์ ...
- ์ผ๋ถ ํ
์ผ๋ง ํต๊ณผํจ... ๋ฐ์ ๋๋ง ๋ง๋ค? ์ ์ด๋...ใ
.ใ
- ์ ์ ๊ดด๋ก์์ง... ๋ฌด์ง์ฑ์ผ๋ก ์ฝ๋ฉํ๊ณ ์ถ์...
- ์ฝ์ง์ค...
def solution(s):
answer = 0
i,j=0,0
s_l = list(s)
s_l = [ord(i) for i in s]
s_r = s_l.copy()
s_r.reverse()
# print(s_l)
# print(s_r)
count = 0
for i in range(len_s):
for j in range(len_s-i):
if s_l[i+j] == s_r[j]:
count += 1
else:
if answer < count:
word_a = s_r[j-count:j]
word_b = word_a[::-1]
print(word_a, word_b)
if word_a == word_b:
answer = max(answer, count)
count = 0
if answer < count:
word_a = s_r[j-count:j]
word_b = word_a[::-1]
print(word_a, word_b)
if word_a == word_b:
answer = max(answer, count)
count = 0
for i in range(len_s):
for j in range(len_s-i):
if s_l[j] == s_r[i+j]:
count += 1
else:
if answer < count:
word_a = s_l[j-count:j]
word_b = word_a[::-1]
print(word_a, word_b)
if word_a == word_b:
answer = max(answer, count)
count = 0
if answer < count:
word_a = s_l[j-count:j]
word_b = word_a[::-1]
print(word_a, word_b)
if word_a == word_b:
answer = max(answer, count)
count = 0
return answer
- ๋๋ฒ์งธ ์ฝ์ง์ค
def solution(s):
answer = 1
count = 1
set_s = list(set(s))
word_indexes = {}
if len(set_s) == 1:
return len(s)
while True:
for c in s:
begin = s.index(c)
end = s.rindex(c)
if begin != end:
word_indexes[c] = (begin, end)
if len(word_indexes) == 0:
break
b2 = len(s)
e2 = -1
for k,v in list(word_indexes.items()):
b,e = v[0],v[1]
if b < b2:
b2 = b
if e2 < e:
e2 = e
if e-b+1 > count:
count = e-b+1
word1 = s[b:e+1]
word2 = word1[::-1]
if word1 == word2:
answer = max(answer, count)
word_indexes.clear()
s = s[b2:e2]
return answer
- ์ฌ๋ฌ๋ฒ ๋๋ฑ๋๋ฑ ํ๋ค๋ณด๋๊น ์ด์ ์ ๋ฌผ์ด ๋ ์ง๊ฒฝ ใ
ใ
ใ
- ๋ ๊ฑฐ ๊ฐ์๋ฐ ์๋๋๊น
- ๋ ์ง์ฆ๋จ.
- ๋ ๊ฑฐ ๊ฐ์๋ฐ ์๋๋๊น
- ์ข๋ง ๋จธ๋ฆฌ๋ฅผ ์ผ์ผ๋ฉด ์ฝ๊ฒ ํ๋ ธ์ ๊ฑฐ ๊ฐ์๋ฐ...
- ๊ฒฐ๊ตญ ํฌ๊ธฐํ๊ณ
- for๋ฌธ์ ๋ฌด์ํ๊ฒ ๋๋ฆฌ๋ฉด์ ๋ฌธ์์ด ์ธ๋ฑ์ฑ์ผ๋ก ํด๊ฒฐ
def solution(s):
answer = 1
if len(set(s)) == 1:
return len(s)
for i in range(len(s)):
for j in range(i,len(s)):
word1 = s[i:j+1]
word2 = word1[::-1]
if word1 == word2:
answer = max(answer, len(word1))
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.05ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (2.43ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (2.80ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (1.37ms, 10.3MB)
ํ
์คํธ 6 ใ ์คํจ (1.38ms, 10.3MB)
ํ
์คํธ 7 ใ ํต๊ณผ (2.66ms, 10.1MB)
ํ
์คํธ 8 ใ ํต๊ณผ (2.76ms, 10.3MB)
ํ
์คํธ 9 ใ ์คํจ (1.46ms, 10.1MB)
ํ
์คํธ 10 ใ ์คํจ (1.27ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (1.34ms, 10.1MB)
ํ
์คํธ 12 ใ ํต๊ณผ (1.80ms, 10.1MB)
ํ
์คํธ 13 ใ ํต๊ณผ (1.27ms, 10.2MB)
ํ
์คํธ 14 ใ ์คํจ (5.24ms, 10.2MB)
ํ
์คํธ 15 ใ ํต๊ณผ (4.65ms, 10.2MB)
ํ
์คํธ 16 ใ ํต๊ณผ (8.61ms, 10.1MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 19 ใ ์คํจ (1.27ms, 10.2MB)
ํ
์คํธ 20 ใ ํต๊ณผ (1.47ms, 10.1MB)
ํ
์คํธ 21 ใ ์คํจ (1.45ms, 10.1MB)
ํจ์จ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ์คํจ (2088.40ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (2255.26ms, 10.2MB)
- ๊ณ ์์ ํ์ด
- ๋๋ถ๋ถ 2์ค for๋ฌธ + ๋ฌธ์์ด ์ธ๋ฑ์ฑ์ผ๋ก ํ์๊ณ ...
- DP ํ์ด. ๊ณ ๊ธ์ ธ ๋ณด์ธ๋ค. ใ .ใ
def solution(s):
dp = [[0]*len(s) for _ in range(len(s))]
answer = 1
for i in range(len(dp)):
dp[i][i] = 1
answer = 1
for i in range(len(dp)-1):
if s[i] == s[i+1]:
dp[i][i+1] = 1
answer = 2
for k in range(3, len(dp)+1):
for i in range(len(dp)-k +1):
j = i + k -1
if dp[i+1][j-1] == 1 and s[i] == s[j]:
dp[i][j] = 1
answer = k
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.61ms, 10.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.58ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.61ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.58ms, 10.3MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.62ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.61ms, 10.4MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.75ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.61ms, 10.4MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.73ms, 10.3MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.97ms, 10.2MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.57ms, 10.3MB)
ํ
์คํธ 14 ใ ํต๊ณผ (2.16ms, 10.4MB)
ํ
์คํธ 15 ใ ํต๊ณผ (1.96ms, 10.4MB)
ํ
์คํธ 16 ใ ํต๊ณผ (2.09ms, 10.5MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.01ms, 10.4MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.55ms, 10.2MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.67ms, 10.3MB)
ํ
์คํธ 21 ใ ํต๊ณผ (0.69ms, 10.2MB)
ํจ์จ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (538.53ms, 58MB)
ํ
์คํธ 2 ใ ํต๊ณผ (612.13ms, 58MB)
728x90
๋ฐ์ํ