Leaderboard
Showing:
Maximum number of characters between any two same character
Easy Accuracy: 51.2% Submissions: 679 Points: 2

Given a string containing lower and uppercase alphabets, the task is to find the maximum number of characters between any two same character in the string.

Example 1:

Input:
S = "socks"
Output: 3
Explanation: There are 3 characters between
the two occurrences of 's'.

​Example 2:

Input: 
S = "FoR"
Output: -1
Explanation: No repeating character present.


Your Task:
You don't need to read input or print anything. Your task is to complete the function maxChars() which takes the string S as input and returns the maximum number of characters between any two same characters in the string.


Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).


Constraints:
1<=|S|<=105

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Maximum number of characters between any two same character

Output Window