通过将给定字符串的 [L, R] 范围内的每个字符重复其字典值次而形成的字符串长度
给定一个长度为N的字符串S和一个范围[L, R] (1 <= L, R <= N)。任务是找到通过将[L, R]范围内的每个字符重复到其字典值次而形成的字符串的长度。
例子:
Input: s = “cbbde”, l = 2, r = 5
Output: 13
Explanation: Resultant String is formed after repeating each character in range [2, 5] as shown below:
b repeated 2 times
b repeated 2 times
d repeated 4 times
e repeated 5 times
Resultant string: ‘bbbbddddeeeee’
Therefore, length of resultant string so formed is 13
Input: s = “xyyz”, l = 1, r = 2
Output: 49
本机方法:可以通过形成一个临时字符串来解决该任务,在[L, R]范围内附加按字典顺序重复的字符,最后返回结果字符串的长度。
时间复杂度: O((R-L+1) * 26)
辅助空间: O((R-L+1) * 26)
高效方法:可以借助前缀数组来解决该任务,该数组将存储相应字典值的总和。
请按照以下步骤解决问题:
- 创建一个前缀数组' prefix ',用来存储当前字符的字典值的累积和
- 得到给定 [L, R] 的答案:求prefix[R]和prefix[L-1]的差异,如果 L > 0 和prefix[L] ,如果 L = 0,得到窗口的答案( R-L+1 )。
- 注意:这种方法在多个查询的情况下有效。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of
// recurring substring in range [l, r]
int recurringSubstring(string s, int l, int r)
{
// Length of the string
int N = s.size();
// Variable to store the index of
// the character in the alphabet
int a[N];
for (int i = 0; i < N; i++) {
a[i] = (s[i] - 'a') + 1;
}
// Prefix array to store the sum
int prefix[N];
prefix[0] = a[0];
for (int i = 1; i < N; i++) {
prefix[i] = prefix[i - 1] + a[i];
}
l = l - 1;
r = r - 1;
// If l is greater than 0
if (l != 0) {
return prefix[r] - prefix[l - 1];
}
// If l is less or equal to 0
else {
return prefix[r];
}
}
// Driver Code
int main()
{
string s = "cbbde";
int l = 2, r = 5;
cout << recurringSubstring(s, l, r);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the length of
// recurring substring in range [l, r]
static int recurringSubstring(String s, int l, int r)
{
// Length of the string
int N = s.length();
// Variable to store the index of
// the character in the alphabet
int []a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = (s.charAt(i) - 'a') + 1;
}
// Prefix array to store the sum
int []prefix = new int[N];
prefix[0] = a[0];
for (int i = 1; i < N; i++) {
prefix[i] = prefix[i - 1] + a[i];
}
l = l - 1;
r = r - 1;
// If l is greater than 0
if (l != 0) {
return prefix[r] - prefix[l - 1];
}
// If l is less or equal to 0
else {
return prefix[r];
}
}
// Driver Code
public static void main(String args[])
{
String s = "cbbde";
int l = 2, r = 5;
System.out.println(recurringSubstring(s, l, r));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to find the length of
# recurring substring in range [l, r]
def recurringSubstring(s, l, r):
# Length of the string
N = len(s)
# Variable to store the index of
# the character in the alphabet
a = [0] * N
for i in range(N):
a[i] = (ord(s[i]) - ord('a')) + 1
# Prefix array to store the sum
prefix = [0] * N
prefix[0] = a[0]
for i in range(1, N):
prefix[i] = prefix[i - 1] + a[i]
l = l - 1
r = r - 1
# If l is greater than 0
if (l != 0):
return prefix[r] - prefix[l - 1]
# If l is less or equal to 0
else:
return prefix[r]
# Driver Code
s = "cbbde"
l = 2
r = 5
print(recurringSubstring(s, l, r))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the length of
// recurring substring in range [l, r]
static int recurringSubstring(string s, int l, int r)
{
// Length of the string
int N = s.Length;
// Variable to store the index of
// the character in the alphabet
int []a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = (s[i] - 'a') + 1;
}
// Prefix array to store the sum
int []prefix = new int[N];
prefix[0] = a[0];
for (int i = 1; i < N; i++) {
prefix[i] = prefix[i - 1] + a[i];
}
l = l - 1;
r = r - 1;
// If l is greater than 0
if (l != 0) {
return prefix[r] - prefix[l - 1];
}
// If l is less or equal to 0
else {
return prefix[r];
}
}
// Driver Code
public static void Main()
{
string s = "cbbde";
int l = 2, r = 5;
Console.Write(recurringSubstring(s, l, r));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of
// recurring substring in range [l, r]
int recurringSubstring(string s, int l, int r)
{
// Length of the string
int N = s.size();
// Length of resultant string
int ans = 0;
for (int i = l - 1; i <= r - 1; i++) {
// Add lexicographic value of s[i]
ans += (s[i] - 'a' + 1);
}
return ans;
}
// Driver Code
int main()
{
string s = "cbbde";
int l = 2, r = 5;
cout << recurringSubstring(s, l, r);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the length of
// recurring substring in range [l, r]
static int recurringSubstring(String s, int l, int r)
{
// Length of the string
int N = s.length();
// Length of resultant string
int ans = 0;
for (int i = l - 1; i <= r - 1; i++) {
// Add lexicographic value of s[i]
ans += (s.charAt(i) - 'a' + 1);
}
return ans;
}
// Driver Code
public static void main(String args[])
{
String s = "cbbde";
int l = 2, r = 5;
System.out.println(recurringSubstring(s, l, r));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to find the length of
# recurring substring in range [l, r]
def recurringSubstring(s, l, r):
# Length of the string
N = len(s)
# Length of resultant string
ans = 0
for i in range(l-1, r):
# Add lexicographic value of s[i]
ans = ans + (ord(s[i]) - ord('a') + 1)
return ans
# Driver Code
s = "cbbde"
l = 2
r = 5
print(recurringSubstring(s, l, r))
# This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the length of
// recurring substring in range [l, r]
static int recurringSubstring(string s, int l, int r)
{
// Length of the string
int N = s.Length;
// Length of resultant string
int ans = 0;
for (int i = l - 1; i <= r - 1; i++) {
// Add lexicographic value of s[i]
ans += (s[i] - 'a' + 1);
}
return ans;
}
// Driver Code
public static void Main()
{
string s = "cbbde";
int l = 2, r = 5;
Console.Write(recurringSubstring(s, l, r));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
13
时间复杂度: O(N),其中 N 是字符串的长度
辅助空间: O(N)
空间优化方法:上述方法可以进一步优化,方法是去掉前缀数组,简单地迭代给定范围,并将相应的字典值添加到答案中。
请按照以下步骤解决问题:
- 遍历 [L, R] 范围,并将相应的字典值添加到答案中。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of
// recurring substring in range [l, r]
int recurringSubstring(string s, int l, int r)
{
// Length of the string
int N = s.size();
// Length of resultant string
int ans = 0;
for (int i = l - 1; i <= r - 1; i++) {
// Add lexicographic value of s[i]
ans += (s[i] - 'a' + 1);
}
return ans;
}
// Driver Code
int main()
{
string s = "cbbde";
int l = 2, r = 5;
cout << recurringSubstring(s, l, r);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the length of
// recurring substring in range [l, r]
static int recurringSubstring(String s, int l, int r)
{
// Length of the string
int N = s.length();
// Length of resultant string
int ans = 0;
for (int i = l - 1; i <= r - 1; i++) {
// Add lexicographic value of s[i]
ans += (s.charAt(i) - 'a' + 1);
}
return ans;
}
// Driver Code
public static void main(String args[])
{
String s = "cbbde";
int l = 2, r = 5;
System.out.println(recurringSubstring(s, l, r));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to find the length of
# recurring substring in range [l, r]
def recurringSubstring(s, l, r):
# Length of the string
N = len(s)
# Length of resultant string
ans = 0
for i in range(l-1, r):
# Add lexicographic value of s[i]
ans = ans + (ord(s[i]) - ord('a') + 1)
return ans
# Driver Code
s = "cbbde"
l = 2
r = 5
print(recurringSubstring(s, l, r))
# This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the length of
// recurring substring in range [l, r]
static int recurringSubstring(string s, int l, int r)
{
// Length of the string
int N = s.Length;
// Length of resultant string
int ans = 0;
for (int i = l - 1; i <= r - 1; i++) {
// Add lexicographic value of s[i]
ans += (s[i] - 'a' + 1);
}
return ans;
}
// Driver Code
public static void Main()
{
string s = "cbbde";
int l = 2, r = 5;
Console.Write(recurringSubstring(s, l, r));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
13
时间复杂度: O(N),其中 N 是字符串的长度
辅助空间: O(1)