class Solution {
public:
bool isPalindrome(string s) {
string str;
for(char it : s) {
if(isalpha(it)) str+=tolower(it);
if(isdigit(it)) str+=it;
}
int start = 0, end = str.length()-1;
for(; start < str.length(); start++, end--) {
if(start > end) break;
if(str[start] != str[end]) return false;
}
return true;
}
};
가운데 문자를 기준으로 앞뒤가 같은 글자인지 확인하는 문제이다. 한국어로 치면(토마토, 기러기 ...)
문자나 숫자면 새로운 string에 넣어두고 아니라면 버린다. 문자는 소문자로 모두 변환시키고 맨 앞과 맨 뒤에서 체크하기 시작하며
달라지는 순간 false를 리턴하고 모든 문자가 체크되면 true를 리턴하도록 구현하였다.
문제: https://leetcode.com/problems/valid-palindrome/
Valid Palindrome - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글
[Leetcode/C++] 169. Majority Element (0) | 2022.02.10 |
---|---|
[Leetcode/C++] 509. Fibonacci Number (0) | 2022.01.28 |
[Leetcode/C++] 412. Fizz Buzz (0) | 2022.01.28 |
[Leetcode/C++] 1154. Day of the Year (0) | 2022.01.28 |
[Leetcode/C++] 1047. Remove All Adjacent Duplicates In String (0) | 2022.01.26 |