给定字符串中增加子字符串的计数
给定长度为N的字符串str ,任务是打印其中每个字符的 ASCII 值大于或等于前一个字符的 ASCII 值的子字符串的数量。子字符串的长度必须至少为 2。
示例:
Input: str = “bcdabc”
Output: 6
Explanation: The 6 substrings with alphabets having greater ASCII value than the previous character and length at least 2 are bc, bcd, cd, ab, abc, bc
Input: str = “cegxza”
Output: 10
天真的方法:上述问题可能是迭代字符串并从每个索引开始计算增加的 ASCII 值子字符串。
时间复杂度: O(N^2)
方法:对于给定问题,一种有效的方法是迭代字符串并使用数学计算在更大的子字符串中找到所有可能增加的 ASCII 值子字符串。请按照以下方法解决问题:
- 将左右指针初始化为0
- 初始化一个变量ans 存储答案
- 迭代字符串直到右指针小于字符串的长度:
- 使用 while 循环使用右指针进行迭代,直到str[right] >= str[right – 1]和right < str.length()
- 通过添加初始化len = right – left,然后将(len * (len + 1) / 2) – len的值添加到ans来计算具有递增 ASCII 值的子字符串的数量
- 使left = right并且右指针将在下一次迭代中递增
- 返回ans作为我们的结果
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to count number of substrings
// with increasing ascii values
int countIncSub(string str)
{
// Initialize left and right pointers
int left = 0, right = 1;
// Initialize length of the string str
int l = str.length();
// Initialize a variable to store answer
int ans = 0;
// Iterate the string
for (; right < l; right++)
{
// Iterate the while loop until the
// string has increasing ASCII value
while (right < l && str[right] >= str[right - 1])
{
right++;
}
// Calculate length of longest
// substring found starting from left
int len = right - left;
// Calculate the number of substrings
// with length at least 2 and add it
// to the ans
ans += (len * (len + 1)) / 2 - len;
// Update left equal to right
left = right;
}
// Return the answer
return ans;
}
int main()
{
// Initialize the string str
string str = "cegxza";
// Call the function and
// print the result
cout << (countIncSub(str));
}
// This code is contributed by Potta Lokesh
Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
// Driver code
class GFG {
// Function to count number of substrings
// with increasing ascii values
public static int countIncSub(String str)
{
// Initialize left and right pointers
int left = 0, right = 1;
// Initialize length of the string str
int l = str.length();
// Initialize a variable to store answer
int ans = 0;
// Iterate the string
for (; right < l; right++) {
// Iterate the while loop until the
// string has increasing ASCII value
while (right < l
&& str.charAt(right)
>= str.charAt(right - 1)) {
right++;
}
// Calculate length of longest
// substring found starting from left
int len = right - left;
// Calculate the number of substrings
// with length at least 2 and add it
// to the ans
ans += (len * (len + 1)) / 2 - len;
// Update left equal to right
left = right;
}
// Return the answer
return ans;
}
public static void main(String[] args)
{
// Initialize the string str
String str = "cegxza";
// Call the function and
// print the result
System.out.println(countIncSub(str));
}
}
Python3
# Python code for the above approach
# Function to count number of substrings
# with increasing ascii values
def countIncSub(str):
# Initialize left and right pointers
left = 0
right = 1
# Initialize length of the string str
l = len(str)
# Initialize a variable to store answer
ans = 0
# Iterate the string
for right in range(1, l):
# Iterate the while loop until the
# string has increasing ASCII value
while (right < l and str[right] >= str[right - 1]):
right += 1
# Calculate length of longest
# substring found starting from left
length = right - left
# Calculate the number of substrings
# with length at least 2 and add it
# to the ans
ans += (length * (length + 1)) // 2 - length
# Update left equal to right
left = right
# Return the answer
return ans
# Driver Code
# Initialize the string str
str = "cegxza"
# Call the function and
# print the result
print(countIncSub(str))
# This code is contributed by Saurabh Jaiswal
C#
// C# implementation for the above approach
using System;
using System.Collections;
// Driver code
class GFG {
// Function to count number of substrings
// with increasing ascii values
static int countIncSub(string str)
{
// Initialize left and right pointers
int left = 0, right = 1;
// Initialize length of the string str
int l = str.Length;
// Initialize a variable to store answer
int ans = 0;
// Iterate the string
for (; right < l; right++) {
// Iterate the while loop until the
// string has increasing ASCII value
while (right < l
&& str[right]
>= str[right - 1]) {
right++;
}
// Calculate length of longest
// substring found starting from left
int len = right - left;
// Calculate the number of substrings
// with length at least 2 and add it
// to the ans
ans += (len * (len + 1)) / 2 - len;
// Update left equal to right
left = right;
}
// Return the answer
return ans;
}
public static void Main()
{
// Initialize the string str
string str = "cegxza";
// Call the function and
// print the result
Console.Write(countIncSub(str));
}
}
// This code is contributed by Samim Hossain Mondal
Javascript
输出:
10
时间复杂度:O(N)
辅助空间: O(1)