LeetCode Courses 3884. First Matching Character From Both Ends
LeetCode

3884. First Matching Character From Both Ends

We iterate from a full-string scan to a half-string walk by noticing the mirror-index symmetry between `s[i]` and `s[n - i - 1]`. A handy trick for any "compare from both ends" problem.

1 chapter Beginner Written walk-through Updated 16 May 2026 Phil · @FirnoxGames
Free

Free for everyone. No account needed.

Start reading →

What you'll learn

1 3884. First Matching Character From Both Ends 📖 5 min read

We iterate from a full-string scan to a half-string walk by noticing the mirror-index symmetry between `s[i]` and `s[n - i - 1]`. A handy trick for any "compare from both ends" problem.

In this chapter
  1. Problem
  2. Full iteration
  3. Stopping half way

Problem

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.

Included with this course
📖Written version of every chapter