[Leetcode] 125. Valid Palindrome

Hannana·2024년 12월 11일

문제 보기

  • 맨 뒤부터 순회 - 맨 앞부터 서로 비교해야하므로 스택 자료구조 활용

이슈사항

  • 없음

풀이

import java.util.*;
class Solution {
    public boolean isPalindrome(String s) {
        String words = s.toLowerCase().replaceAll("[^0-9A-Za-z]","");
        Deque<Character> stack = new ArrayDeque<>();
        for(int i=0;i<words.length();i++){
            stack.push(words.charAt(i));
        }
        while(!stack.isEmpty()){
            for(int i=0;i<words.length();i++){
                if(words.charAt(i)==stack.pop()) continue;
                else return false;
            }
        }
        return true;
    }
}
profile
(구) https://hansjour.tistory.com/ 이사옴. 성장하는 하루를 쌓아가는 블로그

0개의 댓글