字符串中重复之间的曼哈顿距离之和
给定一个由小写字符组成的大小为N的字符串S ,任务是找到每对(i, j)之间的曼哈顿距离之和,使得i≤j和S[j] = S[i] 。
例子:
Input: S = “ababa”
Output: 10
Explanation: The pairs having same characters are: (1, 3), (1, 5), (2, 4) and (3, 5). Therefore, the sum of Manhattan distance will be |3 – 1| + |5 – 1| + |4 – 2| + |5 – 3| = 10
Input: S = “abc”
Output: 0
朴素方法:最简单的方法是使用两个嵌套循环生成所有对(i, j)并检查每一对是否满足给定条件。如果发现是真的,将他们的距离添加到答案中。检查所有对后,打印答案。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:该方法类似于查找所有点对之间的曼哈顿距离之和。请按照以下步骤解决问题:
- 将变量ans初始化为 0 以存储所需的结果。
- 为每个字符创建一个向量V以分别存储每个字符的位置。
- 遍历字符串S ,并将每个字符的位置附加到它们各自的向量V中。
- 现在,问题被简化为找到每个向量数组的每对点之间的曼哈顿距离之和。
- 使用变量i在[0, 25]范围内迭代
- 将向量V[i]中存在的所有元素的总和存储在变量sum中。
- 使用变量j遍历向量V[i]
- 从sum中减去V[i][j]的值。
- 将值sum – V[i][j] * (V[i].size() – 1 – j)添加到ans 。
- 打印ans的值作为结果。
Let the elements of the vector be x1, x2, x3, x4 which represents the indices of the same character.
This character will contribute value = |x2 – x1| + |x3 – x1| + |x4 – x1| + |x3 – x2| + | x4 – x2| + |x4 – x3|
For a sorted array, this can also be written as (x2 + x3 + x4) – 3*x1 + (x3 + x4) – 2*x2 + (x4) – 1*x3.
Now, the sum can also be expressed as ∑suffix[i + 1] – (n-i)*xi for i = 1 to n.
where suffix[i+1] is sum of elements from [i+1, n].
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the sum of the manhattan
// distances between same characters in string
void SumofDistances(string s)
{
// Vector to store indices for each
// unique character of the string
vector v[26];
// Append the position of each character
// in their respective vectors
for (int i = 0; i < s.size(); i++) {
v[s[i] - 'a'].push_back(i);
}
// Store the required result
int ans = 0;
// Iterate over all the characters
for (int i = 0; i < 26; i++) {
int sum = 0;
// Calculate sum of all elements
// present in the vector
for (int j = 0; j < v[i].size(); j++) {
sum += v[i][j];
}
// Traverse the current vector
for (int j = 0; j < v[i].size(); j++) {
// Find suffix[i+1]
sum -= v[i][j];
// Adding distance of all pairs
// whose first element is i and
// second element belongs to [i+1, n]
ans += (sum
- (v[i].size() - 1 - j) * (v[i][j]));
}
}
// Print the result
cout << ans;
}
// Driver Code
int main()
{
// Given Input
string s = "ababa";
// Function Call
SumofDistances(s);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the sum of the manhattan
// distances between same characters in string
static void SumofDistances(String s)
{
// Vector to store indices for each
// unique character of the string
ArrayList> v = new ArrayList<>();
for(int i = 0; i < 26; i++)
v.add(new ArrayList<>());
// Append the position of each character
// in their respective vectors
for(int i = 0; i < s.length(); i++)
{
v.get(s.charAt(i) - 'a').add(i);
}
// Store the required result
int ans = 0;
// Iterate over all the characters
for(int i = 0; i < 26; i++)
{
int sum = 0;
// Calculate sum of all elements
// present in the vector
for(int j = 0; j < v.get(i).size(); j++)
{
sum += v.get(i).get(j);
}
// Traverse the current vector
for(int j = 0; j < v.get(i).size(); j++)
{
// Find suffix[i+1]
sum -= v.get(i).get(j);
// Adding distance of all pairs
// whose first element is i and
// second element belongs to [i+1, n]
ans += (sum - (v.get(i).size() - 1 - j) *
(v.get(i).get(j)));
}
}
// Print the result
System.out.println(ans);
}
// Driver code
public static void main(String[] args)
{
// Given Input
String s = "ababa";
// Function Call
SumofDistances(s);
}
}
// This code is contributed by offbeat
Python3
# Python program for the above approach
#Function to find the sum of the manhattan
#distances between same characters in string
def SumofDistances(s):
# Vector to store indices for each
# unique character of the string
v = [[] for i in range(26)]
# Append the position of each character
# in their respective vectors
for i in range(len(s)):
v[ord(s[i]) - ord('a')].append(i)
# Store the required result
ans = 0
# Iterate over all the characters
for i in range(26):
sum = 0
# Calculate sum of all elements
# present in the vector
for j in range(len(v[i])):
sum += v[i][j]
# Traverse the current vector
for j in range(len(v[i])):
# Find suffix[i+1]
sum -= v[i][j]
# Adding distance of all pairs
# whose first element is i and
# second element belongs to [i+1, n]
ans += (sum - (len(v[i]) - 1 - j) * (v[i][j]))
# Print the result
print (ans)
# Driver Code
if __name__ == '__main__':
# Given Input
s = "ababa"
# Function Call
SumofDistances(s)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the sum of the manhattan
// distances between same characters in string
static void SumofDistances(string s)
{
// Vector to store indices for each
// unique character of the string
List[] v = new List[26];
for(int i = 0; i < 26; i++)
v[i] = new List();
// Append the position of each character
// in their respective vectors
for(int i = 0; i < s.Length; i++)
{
v[(int)s[i] - 97].Add(i);
}
// Store the required result
int ans = 0;
// Iterate over all the characters
for(int i = 0; i < 26; i++)
{
int sum = 0;
// Calculate sum of all elements
// present in the vector
for(int j = 0; j < v[i].Count; j++)
{
sum += v[i][j];
}
// Traverse the current vector
for(int j = 0; j < v[i].Count; j++)
{
// Find suffix[i+1]
sum -= v[i][j];
// Adding distance of all pairs
// whose first element is i and
// second element belongs to [i+1, n]
ans += (sum - (v[i].Count - 1 - j) *
(v[i][j]));
}
}
// Print the result
Console.Write(ans);
}
// Driver Code
public static void Main()
{
// Given Input
string s = "ababa";
// Function Call
SumofDistances(s);
}
}
// This code is contributed by ipg2016107
Javascript
10
时间复杂度: O(N)
辅助空间: O(N)