DEV Community

Cover image for 3884. First Matching Character From Both Ends
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

3884. First Matching Character From Both Ends

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 = "aba"
  • Output: 0

Example 8:

  • Input: s = "abba"
  • Output: 0

Example 9:

  • Input: s = "abcdef"
  • Output: -1

Constraints:

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

Hint:

  1. 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 = 0 and right = n - 1.
  • While left <= right:
    • Compare s[left] and s[right].
    • If they are equal, return left immediately because we are scanning from the smallest index upward.
    • Otherwise, move inward: left++, right--.
  • 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
?>
Enter fullscreen mode Exit fullscreen mode

Explanation

  • The condition asks for the smallest i such that s[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!
Buy Me A Coffee

If you want more helpful content like this, feel free to follow me:

Top comments (0)