Character Distance

Easy
string character-search indexing distance
Problem Description

Find the distance between the first and last occurrence of a given character in a string. Distance is the number of positions between them (exclusive).

Input Format
First line contains the string. Second line contains the character to find.
Output Format
Distance between first and last occurrence, or -1 if character appears less than twice.
Constraints
• 1 ≤ string length ≤ 1000
• Character is a single ASCII character
• Case-sensitive search
• Distance = last_index - first_index - 1
• Return -1 if character appears 0 or 1 times
Sample Input/Output
Input:
programming
m
Output:
2
Explanation

In "programming", 'm' appears at indices 6 and 9. Distance = 9 - 6 - 1 = 2.

Solution Hints
50
Points
1000ms
Time Limit
64MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback