Valid Palindrome II | Delete at Most One Character to Make it a Palindrome

In this tutorial, I am going to discuss a variation of palindrome questions valid palindrome II. In this problem, we can remove a character from a string to make it a palindrome.

Given a non-empty string, We may delete at most one character from a string. We have to write a code which checks whether we can make this string palindrome or not.

For example –

Example 1:

Input : “aba”
Output: true

This string is already palindrome. So we return true.

Example 2:

Input : “abca”
Output: true

This string is not a palindrome. But, if we delete either a character b or c from this string then the new string is a palindrome.

First Non Repeating Character in a Stream of Characters

Find the first non-repeating character in a stream of characters.

Given a string A denoting a stream of lowercase alphabets. We have to write a code to make a new string B.

Here is the rule to form string B.

i) We have to find the first non-repeating character each time a character is inserted into the stream and append it at the end to B.

ii) If no non-repeating character is found then append ‘#’ at the end of B.

For example – 

Example 1 –

Input   = “abadbc”

Output  = “aabbdd”

Explanation:

“a”      –   From the stream, the first character is a, So at this point, the first non-repeating character is ‘a’.

“ab”     –   When b comes, Still the first non-repeating character is ‘a’.

“aba”    –   This time the character is a. Character a is repeated twice so the first non repeating character is ‘b’

“abad”   –   When character d comes, the first non-repeating character is ‘b’.

“abadb”  –   When character b comes, its count is 2 now. So the first non-repeating character at this point is ‘d’.

“abadbc” –   Next character is c. At this point, the first non-repeating character is ‘d’.

Valid Palindrome

Valid palindrome.

Given an input string. We have to write a code to check if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For this problem, consider the empty string as a valid palindrome.

For Example –

Example 1:

Input: “A man, a plan, a canal: Panama”
Output: true

Explanation: After removing non-alphanumeric characters, the string is amanaplanacanalpanama. So, it’s a palindrome

Example 2:

Input: “race a car”
Output: false