Leetcode : 680. Valid Palindrome II | Facebook, Microsoft
Company : Facebook, Microsoft
Question : Given a string s
, return true
if the s
can be palindrome after deleting at most one character from it.
Example 1:
Input: s = "aba"
Output: true
Example 2:
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Example 3:
Input: s = "abc"
Output: false
Constraints:
1 <= s.length <= 105
s
consists of lowercase English letters.
Solution :Runtime: 6 ms, faster than 76.68% of Java online submissions for Valid Palindrome II.
Memory Usage: 39.5 MB, less than 63.22% of Java online submissions for Valid Palindrome II.
Program :
class Solution {
public boolean validPalindrome(String s) {
int i=0;
int j=s.length()-1;
while(i<j){
if(s.charAt(i)==s.charAt(j)){
i++;j — ;
}
else {
return (check(s,i+1,j) || check(s,i,j-1));
}
}
return true;
}
boolean check(String s, int i, int j){
while(i<j){
if(s.charAt(i)==s.charAt(j)){
i++;j — ;
}
else {
return false;
}
}
return true;
}
}