Talentd
FleetCode

3884. First Matching Character From Both Ends

Easy81.5% acceptance

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

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

If no such index exists, return -1.

Example 1:

Input: s = "abcacbd"

Output: 1

Explanation:

At index i = 1, s[1] and s[5] are both 'b'.

No smaller index satisfies the condition, so the answer is 1.

Example 2:

Input: s = "abc"

Output: 1

Explanation:

​​​​​​​At index i = 1, the two compared positions coincide, so both characters are 'b'.

No smaller index satisfies the condition, so the answer is 1.

Example 3:

Input: s = "abcdab"

Output: -1

Explanation:

​​​​​​​For every index i, the characters at positions i and n - i - 1 are different.

Therefore, no valid index exists, so the answer is -1.

Constraints:

  • 1 <= n == s.length <= 100
  • s consists of lowercase English letters.

Hints open one at a time

Hint 1
Use two pointers
1:49
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def firstMatchingIndex(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"abcacbd"
Input
"abc"
Input
"abcdab"
Your code is saved on this device.Solution