Given a string s, return true if the s can be palindrome after deleting at most one character from it.
Two program
valid palindrome
almost palindrome
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
main problem:find length of logest unique substring
sub problem: pattern matching-unique substring
palindrome is string that reads same forward and backwards
"aba" "a" "race car" ""
"A man, a plan, a canal:Panama"==>amanaplanacanalapanama
aaabaaa
aabbaa
race a car===>raceacar
Valid palinodrome
aabaa ==>true
aabbaa==>true
abc==>false
a==>true
""===>true
"A man, a plan, a canal:Panama" ==>true
"A man, a plan, a canal:Panama" ==>true
Valid Palindrome.
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
Java Version :
const is ValidPalindrome= function(s) {
s=s.replace(/[^A-Z,a-z 0-9]/9,"").toLowerCase();
let left=0;right=s.length-1;
while(left<right)
{
if(s[left]!==s[right]{
return false
}
left++;
right--;
}
return true;
};
Explanation
s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase() convert
A man, a plan, a canal:Panama
to
amanaplanacanalapanama
ex aabbad
while(left<right)
{
if(s[left]!==s[right]{
return false
}
1<6
s[left]!==s[right]=>s[1]!==s[6]==>a!=d==>return false
ex aabbaa
while(left<right)
{
}
left++;
right--;
}
return true;
aabbaa==>this will return true because this s[left]!==s[right] not working so outside of if
const is AlmostPalindrome= function(s) {
s=s.replace(/[^A-Z,a-z 0-9]/9,"").toLowerCase();
let left=0;right=s.length-1;
while(left<right)
{
if(s[left]!==s[right]{
return validSubPalindrome(s,left+1,right)||validSubPalindrome(s,left,right-1);
}
left++;
right--;
}
return true;
};
NOTE
validSubPalindrome(s,left+1,right)||validSubPalindrome(s,left,right-1) this const is ValidPalindrome= function(s) { } is working here false or true again working
class Solution {
public boolean isPalindrome(String s) {
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
String reverseString = "";
for (int i = 0; i <= s.length() - 1; i++) {
char ch = s.charAt(i);
reverseString = ch + reverseString;
}
if (s.equals(reverseString)) {
return true;
}
return false;
}
Copied!
}
Python version:
import re
def is_valid_palindrome(s: str) -> bool:
string: str = re.sub(r'[^A-Za-z0-9]', '', s).lower()
reverse: str = ''
# generate a reverse string using a reverse for loop.
for idx in range(len(string) - 1, -1, -1):
reverse += string[idx]
return reverse == string
Top comments (0)