forked from prakashshuklahub/Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path680 Valid Palindrome II
More file actions
38 lines (30 loc) · 842 Bytes
/
680 Valid Palindrome II
File metadata and controls
38 lines (30 loc) · 842 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
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: "aba"
Output: True
public boolean validPalindrome(String s) {
int start= 0 , end = s.length()-1;
int count1 = 0 , count2 = 0;
while(start<end){
if(s.charAt(start)==s.charAt(end)){
start++;
end--;
}else{
start++;
count1++;
}
}
start = 0 ; end = s.length()-1;
while(start<end){
if(s.charAt(start)==s.charAt(end)){
start++;
end--;
}else{
end--;
count2++;
}
}
if(count1 == 1 || count2 == 1) return true;
if(count1 == 0 || count2 == 0) return true;
return false;
}