Earliest Mirror Match Index

You are given a string s of length n consisting only of lowercase English letters.

For an index i, compare the character at the front side and its mirrored position from the back side:

  • front position = i
  • mirrored back position = n - i - 1

Return the smallest index i such that: s[i] == s[n - i - 1]

If there is no such index, return -1.

Notes:

  • The mirrored position of index i is n - i - 1.
  • If both positions become the same index, the characters are naturally equal.

Example 1:

Input: s = "abca"

Output: 0

Explanation:

  • At i = 0, compare s[0] and s[3].
  • Both are 'a', so the answer is 0.

Example 2:

Input: s = "wxyzx"

Output: 1

Explanation:

  • At i = 0, compare s[0] and s[4] → 'w' != 'x'
  • At i = 1, compare s[1] and s[3] → 'x' == 'x'
  • The smallest valid index is 1.

Now Your Turn!

Pick the correct output for the given input

Input: s = "abdcba"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 100
  • s consists only of lowercase English letters 'a' to 'z'

Hints

0
class Solution {
public:
int firstMatchingIndex(string s) {
// Your code goes here
}
};
Test Case

Input:

S