Programming/Algorithm

[Algorithm] checkPalindrome

ST1CKER 2018. 2. 7. 19:05


Problem

Given the string, check if it is a palindrome. (* palindrome은 어느 방향으로 읽어도 동일한 문장을 의미합니다.)


Example

  • For inputString = "aabaa", the output should be
    checkPalindrome(inputString) = true;
  • For inputString = "abac", the output should be
    checkPalindrome(inputString) = false;
  • For inputString = "a", the output should be
    checkPalindrome(inputString) = true.

Input/Output

  • [input] string inputString

    A non-empty string consisting of lowercase characters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 105.

  • [output] boolean

    true if inputString is a palindrome, false otherwise.


Solution

def checkPalindrome(inputString):

return inputString == inputString[::-1]

'리스트 슬라이싱' 을 활용하여 간단하게 문제해결 가능합니다.

inputString[::-1] 은 해당 문자열을 뒤에서 부터 순서대로 출력합니다.

inputString과 해당 문자열을 거꾸로 출력한 것을 비교한 값을 Boolean 값으로 반환하여 문제를 해결합니다.