First and Last Character Occurrence

Easy
0/40
0 upvote
Asked in company
PayU

Problem statement

You are given a string S and a target character C. Your task is to find the first and last index (0-based) at which the target character C appears in the string S.


There are two possible outcomes:

If the character C is found in the string S, you should report both the index of its first occurrence and the index of its last occurrence.

If the character C is not found in the string S at all, you should indicate this.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains the string S.

The second line contains a single character C.


Output Format:
Your function should return a string containing the result.


Note:
The search should be case-sensitive. For example, if you are searching for 'a', the character 'A' will not be considered a match.
Sample Input 1:
hello world
l


Sample Output 1:
2 9


Explanation for Sample 1:
The character 'l' first appears at index 2 and last appears at index 9 in the string "hello world".


Sample Input 2:
programming
z


Sample Output 2:
Character not found


Explanation for Sample 2:
The character 'z' does not exist in the string "programming".


Expected Time Complexity:
The expected time complexity is O(N).


Constraints:
1 <= length of S <= 10^5
The character C will be a single ASCII character.

Time limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
First and Last Character Occurrence
Full screen
Console