Menu

3884. First Matching Character From Both Ends
πŸ“°

3884. First Matching Character From Both Ends

DEV Community: phpΒ·MD ARIFUL HAQUEΒ·1 day ago
#ArsHVYdh
#devto#dev#index#example#input
Long article (623 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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 =…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.