以排序形式查找字符串的回文子字符串的计数
给定一个由小写英文字母组成的字符串str ,任务是找出以str的排序形式存在的回文子字符串的总数。
例子:
Input: str = “acbbd”
Output: 6
All palindromic sub-string in it’s sorted form (“abbcd”) are “a”, “b”, “b”, “bb”, “c” and “d”.
Input: str = “abbabdbd”
Output: 16
幼稚的方法:一种方法是对给定的字符串进行排序,然后计算存在的回文子字符串的总数。为了找到回文子串的数量,可以使用这种方法,其时间复杂度为 O(n^2)。
优化方法:一种有效的方法是计算每个字符的频率,然后对于每个频率,回文数的总数将为(n*(n+1))/2 ,因为已排序字符串将由相同的字符。
例如,字符串“aabbbcd”的回文子字符串将是“a”、“aa”、...、“bbb”、“c”、...等。这种方法的时间复杂度将为 O(n)。
- 创建一个哈希表来存储字符串str的每个字符的频率。
- 遍历哈希表并为每个非零频率添加(hash[i] * (hash[i]+1)) / 2到sum 。
- 最后打印总和。
下面是上述方法的实现:
C++
// CPP program to find the count of palindromic sub-string
// of a string in it's ascending form
#include
using namespace std;
const int MAX_CHAR = 26;
// function to return count of palindromic sub-string
int countPalindrome(string str)
{
int n = str.size();
int sum = 0;
// calculate frequency
int hashTable[MAX_CHAR];
for (int i = 0; i < n; i++)
hashTable[str[i] - 'a']++;
// calculate count of palindromic sub-string
for (int i = 0; i < 26; i++) {
if (hashTable[i])
sum += (hashTable[i] * (hashTable[i] + 1) / 2);
}
// return result
return sum;
}
// driver program
int main()
{
string str = "ananananddd";
cout << countPalindrome(str);
return 0;
}
Java
// Java program to find the count of palindromic sub-string
// of a string in it's ascending form
class GFG {
final static int MAX_CHAR = 26;
// function to return count of palindromic sub-string
static int countPalindrome(String str) {
int n = str.length();
int sum = 0;
// calculate frequency
int hashTable[] = new int[MAX_CHAR];
for (int i = 0; i < n; i++) {
hashTable[str.charAt(i) - 'a']++;
}
// calculate count of palindromic sub-string
for (int i = 0; i < 26; i++) {
if (hashTable[i] != 0) {
sum += (hashTable[i] * (hashTable[i] + 1) / 2);
}
}
// return result
return sum;
}
// driver program
public static void main(String[] args) {
String str = "ananananddd";
System.out.println(countPalindrome(str));
}
}
Python3
# Python3 program to find the count of
# palindromic sub-string of a string
# in it's ascending form
MAX_CHAR = 26
# function to return count of
# palindromic sub-string
def countPalindrome(str):
n = len (str)
sum = 0
# calculate frequency
hashTable = [0] * MAX_CHAR
for i in range(n):
hashTable[ord(str[i]) -
ord('a')] += 1
# calculate count of palindromic
# sub-string
for i in range(26) :
if (hashTable[i]):
sum += (hashTable[i] *
(hashTable[i] + 1) // 2)
# return result
return sum
# Driver Code
if __name__ == "__main__":
str = "ananananddd"
print (countPalindrome(str))
# This code is contributed by ita_c
C#
// C# program to find the count of palindromic sub-string
// of a string in it's ascending form
using System;
public class GFG{
readonly static int MAX_CHAR = 26;
// function to return count of palindromic sub-string
static int countPalindrome(String str) {
int n = str.Length;
int sum = 0;
// calculate frequency
int []hashTable = new int[MAX_CHAR];
for (int i = 0; i < n; i++) {
hashTable[str[i] - 'a']++;
}
// calculate count of palindromic sub-string
for (int i = 0; i < 26; i++) {
if (hashTable[i] != 0) {
sum += (hashTable[i] * (hashTable[i] + 1) / 2);
}
}
// return result
return sum;
}
// driver program
public static void Main() {
String str = "ananananddd";
Console.Write(countPalindrome(str));
}
}
// This code is contributed by Rajput-Ji
PHP
Javascript
输出:
26