주어진 문자열이 Palindrome(좌우에서 읽어도 동일)인지 확인
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
https://leetcode.com/problems/valid-palindrome/
std::tolower()
사용. 한 char를 변환가능. for (auto &c: s)
c = std::tolower(c);
converted_s.push_back(std::tolower(c));
while (left < right) {
if (new_s[left++] != new_s[right--])
return false;
}
#include <string>
class Solution {
public:
string nomalize_str(string s) {
std::string converted_s;
for (auto c: s) {
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9'))
converted_s.push_back(std::tolower(c));
else
continue;
}
return converted_s;
}
bool isPalindrome(string s) {
string new_s = nomalize_str(s);
int left = 0;
int right = new_s.size() - 1;
while (left < right) {
if (new_s[left++] != new_s[right--])
return false;
}
return true;
}
};