문제 풀이/백준(BOJ)
[Python] 백준 17609번 : 회문
희조당
2023. 2. 22. 17:32
728x90
https://www.acmicpc.net/problem/17609
17609번: 회문
각 문자열이 회문인지, 유사 회문인지, 둘 모두 해당되지 않는지를 판단하여 회문이면 0, 유사 회문이면 1, 둘 모두 아니면 2를 순서대로 한 줄에 하나씩 출력한다.
www.acmicpc.net
💡 문제 풀이
투포인터 문제이다.
조건에 맞춰서 잘 출력만 해주면 된다.
✔️ 느낀 점
사실 구현하는데 생각보다 오래 걸렸다.
💻 코드
import sys
input = sys.stdin.readline
def ispseudo(word, left, right):
while left < right:
if word[left] == word[right]:
left += 1
right -= 1
else:
return False
return True
def ispalindrome(word):
left, right = 0, len(word)-1
if word == word[::-1]:
return 0
else:
while left < right:
if word[left] != word[right]:
check_left = ispseudo(word, left + 1, right)
check_right = ispseudo(word, left, right - 1)
if check_left or check_right:
return 1
else:
return 2
else:
left += 1
right -= 1
T = int(input().rstrip("\n"))
for _ in range(T):
word = input().rstrip("\n")
print(ispalindrome(word))