Given a string S, the task is to find the sum of distances between all pairs of indices from the given string which contains the same character.
Input: S = “ababa”
The pair of indices having same character are: (0, 2) (0, 4) (1, 3) (2, 4)
Sum of absolute differences between these pair of indices = |2 – 0| + |4 – 0| + |1 – 3| + |2 – 4| = 10.
Therefore, the required answer is 10.
Input: S = “ttt”
Naive Approach: The simplest approach to solve the problem is to traverse the string and for each character encountered, traverse the remaining string on its right to find occurrences of that character. For every repetition of characters found, keep adding the absolute difference between the concerned indices to the answer. Finally, print the sum obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Below is the implementation of the above approach:
Efficient Approach: The above approach can be optimized based on the following observations:
- Initially for every character, assume that all its similar characters are at index 0.
- With the above assumption, the required sum becomes equal to:
Number of previously visited similar characters * Index of the character – sum of distances of those similar characters from index 0
Therefore, follow the steps below to solve the problem:
- Initialize two arrays visited and distance to store the frequency of each character present in the string and the distance between the previous occurrences of each character respectively.
Traverse the string and for every character encountered, i.e. S[i], update the following:
- Add visited[S[i] * i – distance[S[i]] to the required sum.
- Increment visited[S[i]] to increase frequency of characters.
- Increase distance[S[i]] by i, to increase the distance from the previous occurrence of S[i], considered to be 0.
- Once the above steps are completed, print the sum obtained.
Below is the implementation of the above approach :
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Smallest character in a string having minimum sum of distances between consecutive repetitions
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Minimum Sum of Euclidean Distances to all given Points
- Min flips of continuous characters to make all characters same in a string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Swap all occurrences of two characters to get lexicographically smallest string
- Minimum characters to be replaced to make frequency of all characters same
- Find a point such that sum of the Manhattan distances is minimized
- String with k distinct characters and no same characters adjacent
- Find the original coordinates whose Manhattan distances are given
- C/C++ program to add N distances given in inch-feet system using Structures
- Maximum distance between two occurrences of same element in array
- Group all occurrences of characters according to first appearance
- Longest substring between any pair of occurrences ōf similar characters
- Count of substrings of a given Binary string with all characters same
- Check whether two strings contain same characters in same order
- Find the distance covered to collect items at equal distances
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Replace all occurrences of pi with 3.14 in a given string
- Replace all occurrences of character X with character Y in given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.