-
-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathOstenHun.cpp
More file actions
84 lines (69 loc) ยท 2.03 KB
/
OstenHun.cpp
File metadata and controls
84 lines (69 loc) ยท 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
A phrase is a palindrome if,
after converting all uppercase letters into lowercase letters and
removing all non-alphanumeric characters, it reads the same forward and backward.
Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.
*/
#include <cctype>
#include <string>
#include <vector>
using namespace std;
#pragma region ExtraSpaceIdea
// ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด์ ๋ฌธ์์ด์ ์ ๋ฆฌํ ํ์ ํฐ๋ฆฐ๋๋กฌ ํ๋ณ
// ์๊ฐ ๋ณต์ก๋ : O(n)
// ๊ณต๊ฐ ๋ณต์ก๋ : O(n)
// n์ ๋ฌธ์์ด์ ๊ธธ์ด์ผ ๊ฒ์ด๋ค.
namespace extra_space_idea {
class Solution {
public:
bool isPalindrome(string s) {
vector<char> str;
str.reserve(s.size());
size_t len = s.length();
for (size_t i = 0; i < len; i++) {
if (isalnum(s[i])) {
str.push_back(tolower(s[i]));
}
}
size_t vec_len = str.size();
for (size_t i = 0; i < (vec_len + 1) / 2; i++) {
if (str[i] != str[vec_len - 1 - i]) return false;
}
return true;
}
};
} // namespace extra_space_idea
#pragma endregion
#pragma region FinalSolution
// ํฌ ํฌ์ธํฐ๋ฅผ ์ด์ฉํด ์ถ๊ฐ ๋ฐฐ์ด ์์ด ๊ตฌํ
// ์๊ฐ ๋ณต์ก๋ : O(n)
// ๊ณต๊ฐ ๋ณต์ก๋ : O(1)
class Solution {
public:
bool isPalindrome(string s) {
int left = 0;
int right = s.length() - 1;
while(left < right) {
if (!isalnum(s[left]))
left++;
else if (!isalnum(s[right]))
right--;
else {
if (tolower(s[left]) != tolower(s[right]))
return false;
left++;
right--;
}
}
return true;
}
};
#pragma endregion