[LC BC] Valid Palindrome 60

YJ·6일 전

LeetCode Bootcamp

목록 보기
2/6

https://www.hackerrank.com/contests/leetcode-bootcamp-week-1/challenges/valid-palindrome-60/problem?isFullScreen=true

문제

주어진 string 에서 문자/숫자 아닌 거 빼고 앞뒤로 똑같은지

답안

public static boolean isPalindrome(String s) {
	int i = 0;
	int j = s.length() - 1;
	while (i < j) {
		while ((i < j) && !Character.isLetterOrDigit(s.charAt(i))) {
			i++;
		}
		while ((i < j) && !Character.isLetterOrDigit(s.charAt(j))) {
			j--;
		}
		if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
			return false;
		}
		i++;
		j--;
	}
	return true;
}

메모

투포인터로.. 접근은 잘 했지만
while 문 돌 때 이중 while 문에서도 i < j 를 체크해줘야 된다는 것을 까묵..
꼼꼼히 체크해주자!!!

profile
Hello

0개의 댓글