3884. First Matching Character From Both Ends
Difficulty: Easy
Topics: Mid Level , Two Pointers , String , Weekly Contest 495
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.
Example 4:
Input: s = "a"
Output: 0
Example 5:
Input: s = "aa"
Output: 0
Example 6:
Input: s = "ab"
Output: -1
Example 7:
Input: s =β¦