forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidPalindromeII.java
52 lines (40 loc) · 1.29 KB
/
ValidPalindromeII.java
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
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
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
TWO POINTER APPROACH
SOLUTION:
//1. Iterate through the string until you find first pair of chars that dont match!!
//2. DO ANOTHER palindrome check (through helper function) to check if the rest of string is palindrome EITHER
//witout the ith leter, or without the jth letter
//TC: O(N), because we go through the string at MAX 3 times!!
//SC: O(1) no extra space
class Solution {
public boolean validPalindrome(String s) {
int start = 0;
int end = s.length()-1;
while(start < end){
if(s.charAt(start) != s.charAt(end))
{
return isPalindromeWithoutThese(s, start+1, end) || isPalindromeWithoutThese(s,start,end-1);
}
start++;
end--;
}
return true;
}
public boolean isPalindromeWithoutThese(String s, int i, int j){
while(i<j){
if(s.charAt(i) != s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
}