-
Notifications
You must be signed in to change notification settings - Fork 30
/
Valid Palindrome II.cpp
53 lines (41 loc) · 1.11 KB
/
Valid Palindrome II.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
/*
Solution by Rahul Surana
***********************************************************
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
bool validPalindrome(string s) {
if(s.length()<3) return true;
if(s.length()==3) return s[0] == s[2];
int k = 0;
bool f = true;
int i = 0;
while( i+k < s.length()/2){
if(s[i+k] != s[s.length()-i-1]){
k++;
}
else{
i++;
}
if(k>1) { f = false; break;}
}
if(f) return true;
f = true;
k =0;
i = 0;
while( i+k < s.length()/2){
if(s[i] != s[s.length()-i-1-k]){
k++;
}
else{
i++;
}
if(k>1) { f = false; break;}
}
if(f) return true;
return false;
}
};