Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

String:Valid Palindrome

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
Enter fullscreen mode Exit fullscreen mode
Example 2:

Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Enter fullscreen mode Exit fullscreen mode
Example 3:

Input: s = "abc"
Output: false
Enter fullscreen mode Exit fullscreen mode
main problem:find length of logest unique substring

sub problem: pattern matching-unique substring
Enter fullscreen mode Exit fullscreen mode
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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Valid Palindrome.

Example 1:

Input: s = "aba"
Output: true
Example 2:
Enter fullscreen mode Exit fullscreen mode
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Example 3:
Enter fullscreen mode Exit fullscreen mode
Input: s = "abc"
Output: false
Java Version :
Enter fullscreen mode Exit fullscreen mode
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;

};
Enter fullscreen mode Exit fullscreen mode

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 

Enter fullscreen mode Exit fullscreen mode
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;

};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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!
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)