给定一个由N 个字符组成的字符串S ,任务是找到给定字符串S的所有前缀的长度,这些前缀也是同一字符串S 的后缀。
例子:
Input: S = “ababababab”
Output: 2 4 6 8
Explanation:
The prefixes of S that are also its suffixes are:
- “ab” of length = 2
- “abab” of length = 4
- “ababab” of length = 6
- “abababab” of length = 8
Input: S = “geeksforgeeks”
Output: 5
朴素的方法:解决给定问题的最简单的方法是从头开始遍历给定的字符串S ,并在每次迭代中将当前字符添加到前缀字符串,并检查前缀字符串是否与字符串的后缀相同长度相同与否。如果发现为真,则打印前缀字符串的长度。否则,检查下一个前缀。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of all
// prefixes of the given string that
// are also suffixes of the same string
void countSamePrefixSuffix(string s, int n)
{
// Stores the prefix string
string prefix = "";
// Traverse the string S
for (int i = 0; i < n - 1; i++) {
// Add the current character
// to the prefix string
prefix += s[i];
// Store the suffix string
string suffix = s.substr(
n - 1 - i, n - 1);
// Check if both the strings
// are equal or not
if (prefix == suffix) {
cout << prefix.size() << " ";
}
}
}
// Driver Code
int main()
{
string S = "ababababab";
int N = S.size();
countSamePrefixSuffix(S, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the length of all
// prefixes of the given string that
// are also suffixes of the same string
static void countSamePrefixSuffix(String s, int n)
{
// Stores the prefix string
String prefix = "";
// Traverse the string S
for(int i = 0; i < n - 1; i++)
{
// Add the current character
// to the prefix string
prefix += s.charAt(i);
// Store the suffix string
String suffix = s.substring(n - 1 - i, n);
// Check if both the strings
// are equal or not
if (prefix.equals(suffix))
{
System.out.print(prefix.length() + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
String S = "ababababab";
int N = S.length();
countSamePrefixSuffix(S, N);
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to find the length of all
# prefixes of the given that
# are also suffixes of the same string
def countSamePrefixSuffix(s, n):
# Stores the prefix string
prefix = ""
# Traverse the S
for i in range(n - 1):
# Add the current character
# to the prefix string
prefix += s[i]
# Store the suffix string
suffix = s[n - 1 - i: 2 * n - 2 - i]
# Check if both the strings
# are equal or not
if (prefix == suffix):
print(len(prefix), end = " ")
# Driver Code
if __name__ == '__main__':
S = "ababababab"
N = len(S)
countSamePrefixSuffix(S, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the length of all
// prefixes of the given string that
// are also suffixes of the same string
static void countSamePrefixSuffix(string s, int n)
{
// Stores the prefix string
string prefix = "";
// Traverse the string S
for (int i = 0; i < n - 1; i++) {
// Add the current character
// to the prefix string
prefix += s[i];
// Store the suffix string
string suffix = s.Substring(n - 1 - i, i+1);
// Check if both the strings
// are equal or not
if (prefix == suffix) {
Console.Write(prefix.Length + " ");
}
}
}
// Driver Code
public static void Main()
{
string S = "ababababab";
int N = S.Length;
countSamePrefixSuffix(S, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of all
// prefixes of the given string that
// are also suffixes of the same string
void countSamePrefixSuffix(string s, int n)
{
// Stores the prefixes of the string
map, int> cnt;
// Stores the prefix & suffix strings
deque prefix, suffix;
// Iterate in the range [0, n - 2]
for (int i = 0; i < n - 1; i++) {
// Add the current character to
// the prefix and suffix strings
prefix.push_back(s[i]);
suffix.push_back(s[i]);
// Mark the prefix as 1 in
// the HashMap
cnt[prefix] = 1;
}
// Add the last character to
// the suffix
suffix.push_back(s[n - 1]);
int index = n - 1;
// Iterate in the range [0, n - 2]
for (int i = 0; i < n - 1; i++) {
// Remove the character from
// the front of suffix deque
// to get the suffix string
suffix.pop_front();
// Check if the suffix is
// present in HashMap or not
if (cnt[suffix] == 1) {
cout << index << " ";
}
index--;
}
}
// Driver Code
int main()
{
string S = "ababababab";
int N = S.size();
countSamePrefixSuffix(S, N);
return 0;
}
输出:
2 4 6 8
时间复杂度: O(N 2 )
辅助空间: O(N)
高效的方法:上述方法也可以通过使用散列来存储给定字符串的前缀来优化。然后,遍历所有后缀并检查它们是否存在于哈希映射中。请按照以下步骤解决问题:
- 初始化两个双端队列,比如prefix和suffix来存储S的前缀字符串和后缀字符串。
- 初始化一个 HashMap,比如M来存储S 的所有前缀。
- 使用变量i在[0, N – 2]范围内遍历给定字符串S
- 将当前字符推到前缀和后缀双端队列的后面。
- 在 HashMap M中将前缀标记为true 。
- 在循环之后,将字符串的最后一个字符,例如S[N – 1] 添加到后缀。
- 迭代范围[0, N – 2]并执行以下步骤:
- 删除后缀的前面字符。
- 现在,检查当前双端队列是否存在于 HashMap M 中。如果发现为true ,则打印双端队列的大小。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of all
// prefixes of the given string that
// are also suffixes of the same string
void countSamePrefixSuffix(string s, int n)
{
// Stores the prefixes of the string
map, int> cnt;
// Stores the prefix & suffix strings
deque prefix, suffix;
// Iterate in the range [0, n - 2]
for (int i = 0; i < n - 1; i++) {
// Add the current character to
// the prefix and suffix strings
prefix.push_back(s[i]);
suffix.push_back(s[i]);
// Mark the prefix as 1 in
// the HashMap
cnt[prefix] = 1;
}
// Add the last character to
// the suffix
suffix.push_back(s[n - 1]);
int index = n - 1;
// Iterate in the range [0, n - 2]
for (int i = 0; i < n - 1; i++) {
// Remove the character from
// the front of suffix deque
// to get the suffix string
suffix.pop_front();
// Check if the suffix is
// present in HashMap or not
if (cnt[suffix] == 1) {
cout << index << " ";
}
index--;
}
}
// Driver Code
int main()
{
string S = "ababababab";
int N = S.size();
countSamePrefixSuffix(S, N);
return 0;
}
输出:
8 6 4 2
时间复杂度: O(N * log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live