计算由相同字符组成的字符串的子字符串数
给定一个字符串。任务是找出由相同字符组成的子串的数量。
例子:
Input: abba
Output: 5
The desired substrings are {a}, {b}, {b}, {a}, {bb}
Input: bbbcbb
Output: 10
方法:已知长度为n的字符串,共有n*(n+1)/2个子字符串。
让我们将结果初始化为 0。遍历字符串并找到相同字符的连续元素的数量(比如说count )。每当我们找到另一个字符时,将结果增加count*(count+1)/2 ,将count设置为 1,然后从该索引开始重复上述过程。
请记住,对于每个不同的字符,我们想要的子字符串的数量是 1。
下面是上述方法的实现:
C++
// C++ implementation
// of the above approach
#include
using namespace std;
// Function to return the
// number of substrings of
// same characters
void findNumbers(string s)
{
if (s.empty()) return 0;
// Size of the string
int n = s.size();
// Initialize count to 1
int count = 1;
int result = 0;
// Initialize left to 0 and
// right to 1 to traverse the
// string
int left = 0;
int right = 1;
while (right < n) {
// Checking if consecutive
// characters are same and
// increment the count
if (s[left] == s[right]) {
count++;
}
// When we encounter a
// different characters
else {
// Increment the result
result += count * (count + 1) / 2;
// To repeat the whole
// process set left equals
// right and count variable to 1
left = right;
count = 1;
}
right++;
}
// Store the final
// value of result
result += count * (count + 1) / 2;
cout << result << endl;
}
// Driver code
int main()
{
string s = "bbbcbb";
findNumbers(s);
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the
// number of substrings of
// same characters
static void findNumbers(String s)
{
// Size of the string
int n = s.length();
// Initialize count to 1
int count = 1;
int result = 0;
// Initialize left to 0 and
// right to 1 to traverse the
// string
int left = 0;
int right = 1;
while (right < n)
{
// Checking if consecutive
// characters are same and
// increment the count
if (s.charAt(left) == s.charAt(right))
{
count++;
}
// When we encounter a
// different characters
else
{
// Increment the result
result += count * (count + 1) / 2;
// To repeat the whole
// process set left equals
// right and count variable to 1
left = right;
count = 1;
}
right++;
}
// Store the final
// value of result
result += count * (count + 1) / 2;
System.out.println(result);
}
// Driver code
public static void main (String[] args)
{
String s = "bbbcbb";
findNumbers(s);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
# Function to return the number of
# substrings of same characters
def findNumbers(s):
# Size of the string
n = len(s)
# Initialize count to 1
count = 1
result = 0
# Initialize left to 0 and right to 1
# to traverse the string
left = 0
right = 1
while (right < n):
# Checking if consecutive
# characters are same and
# increment the count
if (s[left] == s[right]):
count += 1
# When we encounter a
# different characters
else:
# Increment the result
result += count * (count + 1) // 2
# To repeat the whole
# process set left equals
# right and count variable to 1
left = right
count = 1
right += 1
# Store the final value of result
result += count * (count + 1) // 2
print(result)
# Driver code
s = "bbbcbb"
findNumbers(s)
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the
// number of substrings of
// same characters
static void findNumbers(String s)
{
// Size of the string
int n = s.Length;
// Initialize count to 1
int count = 1;
int result = 0;
// Initialize left to 0 and
// right to 1 to traverse the
// string
int left = 0;
int right = 1;
while (right < n)
{
// Checking if consecutive
// characters are same and
// increment the count
if (s[left] == s[right])
count++;
// When we encounter a
// different characters
else
{
// Increment the result
result += count * (count + 1) / 2;
// To repeat the whole
// process set left equals
// right and count variable to 1
left = right;
count = 1;
}
right++;
}
// Store the final
// value of result
result += count * (count + 1) / 2;
Console.WriteLine(result);
}
// Driver code
public static void Main(String[] args)
{
String s = "bbbcbb";
findNumbers(s);
}
}
// This code is contributed by
// sanjeev2552
Javascript
输出:
10