본문 바로가기
  • Adillete
【스터디노트】/▶알고리즘문제풀기

ValidPalindrome

by 아딜렛 2025. 4. 10.

 

문제 출처: https://leetcode.com/problems/valid-palindrome/

 

접근 방법

/**
         * 문자열에서 알파벳과 숫자만 추출하여 새로운 문자열 생성
         * 추출된 문자열을 모두 소문자로 변환
         * 변환된 문자열이 앞뒤로 같은지 비교
         */

 

수도 코드

/**
 * function isPalindrome(s):
 *     // 1. 알파벳과 숫자만 추출하고 소문자로 변환
 *     cleanedString = ""
 *     for character in s:
 *         if character is alphanumeric:
 *             cleanedString += toLowercase(character)
 *
 *     // 2. 앞뒤로 읽어서 같은지 확인
 *     left = 0
 *     right = length(cleanedString) - 1
 *
 *     while left < right:
 *         if cleanedString[left] != cleanedString[right]:
 *             return false
 *         left++
 *         right--
 *
 *     return true
 */

 

새롭게 알게된것 

문자열에서 s.length() -1 을 사용하는 이유:

인덱스 번호와 문자열 길이 사이의 차이 때문이다.

'【스터디노트】 > ▶알고리즘문제풀기' 카테고리의 다른 글

[Valid Anagram]  (0) 2025.04.14
[Invert Binary Tree]  (0) 2025.04.12
[시간 복잡도]  (0) 2025.04.10
[Best Time to Buy and Sell Stock]  (0) 2025.03.29
[Merge Two Sorted Lists]  (0) 2025.03.27