-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_palindrome.cpp
97 lines (82 loc) · 2.29 KB
/
valid_palindrome.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Problem: https://leetcode.com/problems/valid-palindrome/
Date: Feb 10, 2023
Approach:
--------
Two pointers
- Explained in https://www.educative.io/courses/grokking-coding-interview-patterns-cpp/R1NKJD3XxBq
Complexity:
----------
Time: O(n)
Memory: O(n)
References:
-----------
- https://www.geeksforgeeks.org/iswalnum-function-in-c-stl/
- Definition of alphanumeric characters.
*/
#include <lest.hpp>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
/* Create string from the input string by
a. converitng upper case to lower case
b. stripping off non-alphanumeric characters
*/
string s_mod = "";
for (unsigned int i=0; i != s.size(); ++i){
char c = s[i];
if (c >= 'A' && c <= 'Z'){
s_mod += tolower(c);
}
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')){
s_mod += c;
}
else {
continue;
}
}
// cout << "Modified string: " << s_mod << endl;
// Now check whether the stripped string is palindrome or not
if (s_mod.empty()){
return true;
}
unsigned int left_ptr_pos = 0;
unsigned int right_ptr_pos = s_mod.size()-1;
while (left_ptr_pos < right_ptr_pos)
{
if (s_mod[left_ptr_pos] != s_mod[right_ptr_pos]){
return false;
}
++left_ptr_pos;
--right_ptr_pos;
}
return true;
}
};
const lest::test tests[] = {
{CASE("Empty String"){
Solution sln;
string s = "";
EXPECT(sln.isPalindrome(s) == true);
}},
{CASE("Empty Mod String"){
Solution sln;
string s = "& -";
EXPECT(sln.isPalindrome(s) == true);
}},
{CASE("Non palindrome String"){
Solution sln;
string s = "race a car";
EXPECT(sln.isPalindrome(s) == false);
}},
{CASE("Palindrome String"){
Solution sln;
string s = "A man, a plan, a canal: Panama";
EXPECT(sln.isPalindrome(s) == true);
}}
};
int main(int argc, char** argv){
lest::run(tests, argc, argv);
}