给定字符串str ,任务是计算给定字符串的所有可能的子字符串,这些子字符串是同一字符串的前缀。
例子:
Input: str = “ababc”
Output: 7
All possible sub-string are “a”, “ab”, “aba”, “abab”, “ababc”, “a” and “ab”
Input: str = “abdabc”
Output: 8
做法:遍历字符串逐个,如果当前字符是等于字符串的第一个字符再算上从这里开始的所有可能的子字符串也是str的前缀和添加它来计算。遍历完整的字符串后,打印count 。
下面是上述方法的实现:
C++14
// C++ implementation of the approach
#include
#include
using namespace std;
// Function to return the
// count of sub-strings starting
// from startIndex that are
// also the prefixes of str
int subStringsStartingHere(string str, int n,
int startIndex)
{
int count = 0, i = 1;
while (i <= n)
{
if (str.substr(0,i) ==
str.substr(startIndex, i))
{
count++;
}
else
break;
i++;
}
return count;
}
// Function to return the
// count of all possible sub-strings
// of str that are also the prefixes of str
int countSubStrings(string str, int n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
// If current character is equal to
// the starting character of str
if (str[i] == str[0])
count += subStringsStartingHere(str,
n, i);
}
return count;
}
// Driver code
int main()
{
string str = "abcda";
int n = str.length();
// Function Call
cout << (countSubStrings(str, n));
}
// This code is contributed by harshvijeta0
Java
// Java implementation of the approach
public class GFG
{
// Function to return
// the count of sub-strings starting
// from startIndex that
// are also the prefixes of str
public static int subStringsStartingHere(
String str, int n,
int startIndex)
{
int count = 0, i = startIndex + 1;
while (i <= n)
{
if (str.startsWith(str.substring(
startIndex, i)))
{
count++;
}
else
break;
i++;
}
return count;
}
// Function to return the
// count of all possible sub-strings
// of str that are also the prefixes of str
public static int countSubStrings(String str,
int n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
// If current character is equal to
// the starting character of str
if (str.charAt(i) == str.charAt(0))
count += subStringsStartingHere(str, n, i);
}
return count;
}
// Driver code
public static void main(String[] args)
{
String str = "ababc";
int n = str.length();
System.out.println(countSubStrings(str, n));
}
}
Python3
# Python3 implementation of the approach
# Function to return the
# count of sub-strings starting
# from startIndex that are
# also the prefixes of string
def subStringsStartingHere(string, n,
startIndex):
count = 0
i = startIndex + 1
while(i <= n) :
if string.startswith(
string[startIndex : i]):
count += 1
else :
break
i += 1
return count
# Function to return the
# count of all possible sub-strings
# of string that are also
# the prefixes of string
def countSubStrings(string, n) :
count = 0
for i in range(n) :
# If current character is equal to
# the starting character of str
if string[i] == string[0] :
count += subStringsStartingHere(
string, n, i)
return count
# Driver Code
if __name__ == "__main__" :
string = "ababc"
n = len(string)
print(countSubStrings(string, n))
# this code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the
// count of sub-strings starting
// from startIndex that
// are also the prefixes of str
static int subStringsStartingHere(
String str, int n,
int startIndex)
{
int count = 0, i = startIndex + 1;
while (i <= n) {
if (str.StartsWith(str.Substring(
startIndex, i-startIndex)))
{
count++;
}
else
break;
i++;
}
return count;
}
// Function to return the
// count of all possible sub-strings
// of str that are also the prefixes of str
static int countSubStrings(String str, int n)
{
int count = 0;
for (int i = 0; i < n; i++) {
// If current character is equal to
// the starting character of str
if (str[i] == str[0])
count += subStringsStartingHere(
str, n, i);
}
return count;
}
// Driver code
static public void Main(String []args)
{
String str = "ababc";
int n = str.Length;
Console.WriteLine(countSubStrings(str, n));
}
}
//contributed by Arnab Kundu
输出
6