Given a string, find the maximum number of characters between any two same character in the string. If no character repeats, print -1.
Input : str = "abba" Output : 2 The maximum number of characters are between two occurrences of 'a'. Input : str = "baaabcddc" Output : 3 The maximum number of characters are between two occurrences of 'b'. Input : str = "abc" Output : -1
Approach 1 (Simple): Use two nested loops. The outer loop picks character from left to right, the inner loop finds farthest occurrence and keeps track of maximum.
Time Complexity : O(n^2)
Approach 2 (Efficient) : Initialize an array”FIRST” of length 26 in which we have to store first occurrence of an alphabet in the string and another array “LAST” of length 26 in which we will store last occurrence of the alphabet in the string here index 0 is correspond to alphabet a, 1 for b and so on . After that we will take the difference of last and first arrays to find max difference if they are not at same position.
# Efficient Python3 program to find maximum
# number of characters between two occurrences
# of same character
MAX_CHAR = 256
n = len(str1)
res = -1
# Initialize all indexes as -1.
firstInd = [-1 for i in range(MAX_CHAR)]
for i in range(n):
first_ind = firstInd[ord(str1[i])]
# If this is first occurrence
if (first_ind == -1):
firstInd[ord(str1[i])] = i
# Else find distance from previous
# occurrence and update result (if
res = max(res, abs(i – first_ind – 1))
# Driver code
str1 = “abba”
# This code is contributed by Mohit kumar 29
Time Complexity : O(n)
This article is contributed by UDIT UPADHYAY. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Permutation of a string with maximum number of characters greater than its adjacent characters
- String with maximum number of unique characters
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Minimum sum of squares of character counts in a given string after removing k characters
- Maximum occurring character in an input string | Set-2
- Maximum consecutive repeating character in string
- Return maximum occurring character in an input string
- Maximum length of balanced string after swapping and removal of characters
- Length of the smallest sub-string consisting of maximum distinct characters
- Count of number of given string in 2D character array
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Minimize number of unique characters in string
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Minimum number of characters to be removed to make a binary string alternate