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]ands[5]are both'b'. - No smaller index satisfies the condition, so the answer is 1.
- At index
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.
- At index
Example 3:
- Input: s = "abcdab"
- Output: -1
-
Explanation:
- For every index
i, the characters at positionsiandn - i - 1are different. - Therefore, no valid index exists, so the answer is -1.
- For every index
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 = "aba"
- Output: 0
Example 8:
- Input: s = "abba"
- Output: 0
Example 9:
- Input: s = "abcdef"
- Output: -1
Constraints:
1 <= n == s.length <= 100-
sconsists of lowercase English letters.
Hint:
- Use two pointers
Solution:
we use a two-pointer scan from both ends of the string. Starting with the smallest possible index i = 0, compare s[left] with s[right]; the first match gives the smallest valid index.
Approach
- Initialize
left = 0andright = n - 1. - While
left <= right:- Compare
s[left]ands[right]. - If they are equal, return
leftimmediately because we are scanning from the smallest index upward. - Otherwise, move inward:
left++,right--.
- Compare
- If the loop finishes without a match, return
-1.
Let's implement this solution in PHP: 3884. First Matching Character From Both Ends
<?php
/**
* @param String $s
* @return Integer
*/
function firstMatchingIndex(string $s): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo firstMatchingIndex("abcacbd") . "\n"; // Output: 1
echo firstMatchingIndex("abc") . "\n"; // Output: 1
echo firstMatchingIndex("abcdab") . "\n"; // Output: -1
echo firstMatchingIndex("a") . "\n"; // Output: 0
echo firstMatchingIndex("aa") . "\n"; // Output: 0
echo firstMatchingIndex("ab") . "\n"; // Output: -1
echo firstMatchingIndex("aba") . "\n"; // Output: 0
echo firstMatchingIndex("abba") . "\n"; // Output: 0
echo firstMatchingIndex("abcdef") . "\n"; // Output: -1
?>
Explanation
- The condition asks for the smallest
isuch thats[i] == s[n - i - 1]. - Scanning from both ends checks exactly these mirrored pairs.
- Returning as soon as a match is found guarantees the smallest matching index.
- For odd-length strings, the middle position eventually satisfies
left == right, so it matches automatically if reached. - Once
left > right, all relevant mirrored pairs have been checked.
Complexity Analysis
-
Time Complexity:
O(n)— each character pair is checked at most once. -
Space Complexity:
O(1)— only two pointers are used.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)