给定字符串str ,任务是查找在给定字符串连续放置的不同子字符串的数量。
例子:
Input: str = “geeksgeeksforgeeks”
Output: 2
Explanation:
geeksgeeksforgeeks -> {“geeks”}
geeksgeeksforgeeks -> {“e”}
Only one consecutive occurrence of “e” is considered.
Therefore two distinct substrings {“geeks”, “e”} occur consecutively in the string.
Therefore, the answer is 2.
Input: s = “geeksforgeeks”
Output: 1
Explanation:
geeksgeeksforgeeks -> {“e”, “e”}
Only one substring {“e”} occurs consecutively in the string.
天真的方法:
最简单的方法是产生给定字符串的所有可能的子串,并为每个子,发现在给定的子字符串中相继出现的次数。最后,打印计数。
时间复杂度: O(N 3 )
辅助空间: O(N)
高效方法:
为了优化上述方法,其思想是使用动态编程。
请按照以下步骤解决问题:
- 如果字符串的长度不超过1 ,则不可能找到任何这样连续放置的相似子字符串。因此,返回0作为count 。
- 否则,初始化尺寸为(N + 1 * N + 1)的备忘录表dp [] ,该表被初始化为0 。
- 初始化一个unordered_set以存储连续放置的不同子字符串。
- 从字符串的结尾进行迭代。
- 在遍历字符串,如果发现任何重复字符,则将考虑先前计算的dp值来确定dp [i] [j],即直到dp [i + 1] [j + 1]个字符且包括以下字符的相同子字符串的计数当前字符。
- 如果字符不相似,则dp [i] [j]将填充0。
- 相似的子字符串连续放置在一起,没有任何其他字符,并且最多(j – i)个字符。因此,对于有效的子字符串, dp [i] [j]的值必须大于(j – i) 。将那些子字符串存储在unordered_set中,该字符串连续出现最大次数。
- 最后,返回unordered_set的大小作为连续放置的不同子字符串的计数。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to count the distinct substrings
// placed consecutively in the given string
int distinctSimilarSubstrings(string str)
{
// Length of the string
int n = str.size();
// If length of the string
// does not exceed 1
if (n <= 1) {
return 0;
}
// Initialize a DP-table
vector > dp(
n + 1, vector(n + 1, 0));
// Stores the distinct substring
unordered_set substrings;
// Iterate from end of the string
for (int j = n - 1; j >= 0; j--) {
// Iterate backward until
// dp table is all computed
for (int i = j - 1; i >= 0; i--) {
// If character at i-th index is
// same as character at j-th index
if (str[i] == str[j]) {
// Update dp[i][j] based on
// previously computed value
dp[i][j] = dp[i + 1][j + 1] + 1;
}
// Otherwise
else {
dp[i][j] = 0;
}
// Condition for consecutively
// placed similar substring
if (dp[i][j] >= j - i) {
substrings.insert(
str.substr(i, j - i));
}
}
}
// Return the count
return substrings.size();
}
// Driver Code
int main()
{
string str = "geeksgeeksforgeeks";
cout << distinctSimilarSubstrings(str);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.ArrayList;
class GFG{
// Function to count the distinct substrings
// placed consecutively in the given string
static int distinctSimilarSubstrings(String str)
{
// Length of the string
int n = str.length();
// If length of the string
// does not exceed 1
if (n <= 1)
return 0;
// Initialize a DP-table
long dp[][] = new long[n + 1][n + 1];
// Declaring ArrayList to store strings
ArrayList list = new ArrayList();
// Iterate from end of the string
for(int j = n - 1; j >= 0; j--)
{
// Iterate backward until
// dp table is all computed
for(int i = j - 1; i >= 0; i--)
{
// If character at i-th index is
// same as character at j-th index
if (str.charAt(i) == str.charAt(j))
{
// Update dp[i][j] based on
// previously computed value
dp[i][j] = dp[i + 1][j + 1] + 1;
}
// Otherwise
else
{
dp[i][j] = 0;
}
// Condition for consecutively
// placed similar substring
if (dp[i][j] >= j - i)
{
list.add(str.substring(j - i, i));
}
}
}
// Return the count
return list.size();
}
// Driver Code
public static void main(String[] args)
{
String str = "geeksforgeeks";
System.out.println(distinctSimilarSubstrings(str));
}
}
// This code is contributed by user_00
Python3
# Python3 program to implement
# the above approach
# Function to count the distinct substrings
# placed consecutively in the given string
def distinctSimilarSubstrings(str):
# Length of the string
n = len(str)
# If length of the string
# does not exceed 1
if(n <= 1):
return 0
# Initialize a DP-table
dp = [[0 for x in range(n + 1)]
for y in range(n + 1)]
# Stores the distinct substring
substrings = set()
# Iterate from end of the string
for j in range(n - 1, -1, -1):
# Iterate backward until
# dp table is all computed
for i in range(j - 1, -1, -1):
# If character at i-th index is
# same as character at j-th index
if(str[i] == str[j]):
# Update dp[i][j] based on
# previously computed value
dp[i][j] = dp[i + 1][j + 1] + 1
# Otherwise
else:
dp[i][j] = 0
# Condition for consecutively
# placed similar substring
if(dp[i][j] >= j - i):
substrings.add(str[i : j - i])
# Return the count
return len(substrings)
# Driver Code
str = "geeksgeeksforgeeks"
# Function call
print(distinctSimilarSubstrings(str))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to count the distinct substrings
// placed consecutively in the given string
static int distinctSimilarSubstrings(string str)
{
// Length of the string
int n = str.Length;
// If length of the string
// does not exceed 1
if (n <= 1)
return 0;
// Initialize a DP-table
long[,] dp = new long[n + 1, n + 1];
// Declaring ArrayList to store strings
List list = new List();
// Iterate from end of the string
for(int j = n - 1; j >= 0; j--)
{
// Iterate backward until
// dp table is all computed
for(int i = j - 1; i >= 0; i--)
{
// If character at i-th index is
// same as character at j-th index
if (str[i] == str[j])
{
// Update dp[i][j] based on
// previously computed value
dp[i, j] = dp[i + 1, j + 1] + 1;
}
// Otherwise
else
{
dp[i, j] = 0;
}
// Condition for consecutively
// placed similar substring
if (dp[i, j] >= j - i)
{
list.Add(str.Substring(i, j - i));
}
}
}
// Return the count
return list.Count;
}
// Driver code
static void Main()
{
string str = "geeksforgeeks";
Console.WriteLine(distinctSimilarSubstrings(str));
}
}
// This code is contributed by divyesh072019
2
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。