Skip to main content

Filtered Palindrome

easy
Software engineer

Given two character arrays, ignoreArray and inputArray, determine whether inputArray forms a palindrome after skipping all characters that appear in ignoreArray.

A palindrome is a sequence that reads the same forward and backward. Return true if the filtered inputArray is a palindrome; otherwise, return false.

Note that, assuming the inputArray is significantly larger than ignoreArray, your solution should be efficient in terms of space complexity.

Example 1

Input

ignoreArray = ['#', '@', '!', '1', 'c'], inputArray = ['a', 's', '2', '@', 's', '2', 'a']

Output

true

Explanation

We first identify the characters to ignore: '#', '@', '!', '1', and 'c'. Looking at `inputArray`, the character '@' is present in the ignore list. When we remove it, the remaining characters are ['a', 's', '2', 's', '2', 'a']. This sequence reads the same forward and backward, so it is a palindrome.

Example 2

Input

ignoreArray = ['#', '@'], inputArray = ['a', '#', 'b', 'c', '@', 'b', 'd']

Output

false

Explanation

Filtered array is ['a', 'b', 'c', 'b', 'd'], which is not a palindrome.

Example 3

Input

ignoreArray = ['a', 'b', 'c'], inputArray = ['a', 'b', 'c', 'a', 'c', 'b', 'a']

Output

true

Explanation

After ignoring 'a', 'b', and 'c', the array effectively contains only ['a', 'c', 'b', 'a']? No, wait—the characters 'a', 'b', and 'c' are ALL ignored. The resulting filtered array is empty. An empty sequence is technically a palindrome.

Constraints

  • 1 <= ignoreArray.length <= 100
  • 1 <= inputArray.length <= 1000
  • Each element is a single printable ASCII character.

OA

ArrayHash TableStringTwo Pointers
Language
Code editor loads in the browser.

Output

Input

["#","@","!","1","c"]
["a","s","2","@","s","2","a"]

Expected

true

Your output

Run to see your output.