Problem Solving/리트코드(leetcode)
[Leetcode/C++] 125. Valid Palindrome
높은곳에영광
2022. 1. 28. 17:39
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