You are given a string s of lowercase English letters and a list of strings pairs. Each element of pairs is a two-character string, such as "ab", meaning the characters a and b are equivalent.
If a and b are equivalent, then b and a are also equivalent. If a and b are equivalent and b and c are equivalent, then a and c are also equivalent too. Every character is equivalent to itself, even one that never appears in pairs.
Return whether s is a palindrome once characters at mirrored positions are compared by equivalence instead of by exact equality.
Note that pairs may contain repeated entries, and both characters of a pair may be the same, such as "aa".
Examples
Example 1
Input: s = "mnop", pairs = ["mp", "no"]
Output: true
Explanation: m and p are equivalent, so position 0 matches position 3. n and o are equivalent, so position 1 matches position 2.
Example 2
Input: s = "mnop", pairs = ["mp"]
Output: false
Explanation: m and p are equivalent, so position 0 matches position 3. But n at position 1 and o at position 2 are never made equivalent, and n != o, so s is not a palindrome under the equivalence relation.
Constraints
0≤s.length≤105
0≤pairs.length≤105
s consists of lowercase English letters.
Each element of pairs is a two-character string of lowercase English letters.
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 2
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run