-
Notifications
You must be signed in to change notification settings - Fork 1
/
28_Valid Palindrome LC-125.cpp
55 lines (49 loc) · 1.26 KB
/
28_Valid Palindrome LC-125.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
// DATE: 03-07-2023
/* PROGRAM: 28_Valid Palindrome LC-125
https://leetcode.com/problems/valid-palindrome/
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.
INPUT
A man, a plan, a canal: Panama
OUTPUT
True
EXPLANATION
- iswalnum() to checks if the given character is an alphanumeric character or not. It is defined within the <cwctype> header file.
- tolower() function converts an uppercase alphabet to a lowercase alphabet. It is a predefined function of <ctype.h> header file.
*/
// @ankitsamaddar @2023
#include <cstring>
#include <iostream>
using namespace std;
bool isPalindrome(char s[]) {
int i = 0;
int j = strlen(s) - 1;
// int j = s.size() - 1; // for string input
while(i<j){
if (!iswalnum(s[i])) {
i++;
}
else if (!iswalnum(s[j])) {
j--;
}
else{
if(tolower(s[i])!=tolower(s[j]))
return false;
i++;
j--;
}
}
return true;
}
int main() {
int n=1000;
char arr[1000];
cin.getline(arr,n);
if(isPalindrome(arr)){
cout<<"True\n";
}
else {
cout<<"False\n";
}
return 0;
}