-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.cpp
More file actions
38 lines (32 loc) · 815 Bytes
/
Copy pathpalindrome.cpp
File metadata and controls
38 lines (32 loc) · 815 Bytes
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
//day 6
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s, int left, int right) {
while (left < right) {
if (s[left] != s[right]) return false;
left++;
right--;
}
return true;
}
// you need to implement this function only
bool canBePalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) {
// Check if removing one character resolves the mismatch
return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1);
}
left++;
right--;
}
return true;
}
int main() {
string s;
cin >> s;
// please don't modify the main function
cout << (canBePalindrome(s) ? "YES" : "NO");
return 0;
}