给定字符串S ,将给定字符串分成尽可能多的子字符串,以使给定字符串中的每个字符出现在单个子字符串中并打印所有这些可能的部分。任务是打印那些子字符串。
例子:
Input: S = “ababcbacadefegdehijhklij”
Output:
ababcbaca defegde hijhklij
Explanation:
a, b, c are only present in the first string.
d, e, f, g are only present in the second string.
h, i, j, k, l are only present in the third string.
Input: S = “acbbcc”
Output:
a cbbcc
Explanation:
a are only present in the first string.
b, c are only present in the second string.
方法:请按照以下步骤解决问题:
- 将所有字符的最后出现索引存储在字符串。
- 由于字符串仅包含小写字母,因此只需使用固定大小为26的数组来存储每个字符的最后一个索引。
- 初始化一个空字符串ans =“”,并遍历给定的字符串,然后执行以下步骤:
- 当前字符添加到字符串ANS如果字符的最后位置比当前索引,并增加分区的长度。
- 如果当前字符的最后位置等于当前索引,则打印存储在ans中的当前字符串,并将ans初始化为“”以存储字符串的下一个分区。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print all the substrings
void print_substring(string s)
{
int n = s.size();
// Stores the substrings
string str = "";
// Stores last index of
// charcters of string s
vector ans;
if (n == 0) {
cout << "-1";
return;
}
// Find the last position of
// each character in the string
vector last_pos(26, -1);
for (int i = n - 1; i >= 0; --i) {
// Update the last index
if (last_pos[s[i] - 'a'] == -1) {
last_pos[s[i] - 'a'] = i;
}
}
int minp = -1;
// Iterate the given string
for (int i = 0; i < n; ++i) {
// Get the last index of s[i]
int lp = last_pos[s[i] - 'a'];
// Extend the current partition
// characters last pos
minp = max(minp, lp);
// If the current pos of
// character equals the min pos
// then the end of partition
if (i == minp) {
// Add the respective character first
str += s[i];
// Store the partition's
// len and reset variables
cout << str << ' ';
// Update the minp and str
minp = -1;
str = "";
}
else {
str += s[i];
}
}
}
// Driver Code
int main()
{
// Input string
string S = "ababcbacadefegdehijhklij";
// Function call
print_substring(S);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to print all the substrings
public static void print_substring(String s)
{
int n = s.length();
// Stores the substrings
String str = "";
// Stores last index of
// charcters of string s
Vector ans = new Vector();
if (n == 0) {
System.out.print("-1");
return;
}
// Find the last position of
// each character in the string
int[] last_pos = new int[26];
Arrays.fill(last_pos, -1);
for (int i = n - 1; i >= 0; --i) {
// Update the last index
if (last_pos[s.charAt(i) - 'a'] == -1) {
last_pos[s.charAt(i) - 'a'] = i;
}
}
int minp = -1;
// Iterate the given string
for (int i = 0; i < n; ++i) {
// Get the last index of s[i]
int lp = last_pos[s.charAt(i) - 'a'];
// Extend the current partition
// characters last pos
minp = Math.max(minp, lp);
// If the current pos of
// character equals the min pos
// then the end of partition
if (i == minp) {
// Add the respective character first
str += s.charAt(i);
// Store the partition's
// len and reset variables
System.out.print(str + ' ');
// Update the minp and str
minp = -1;
str = "";
}
else {
str += s.charAt(i);
}
}
}
// Driver Code
public static void main(String[] args)
{
// Input string
String S = "ababcbacadefegdehijhklij";
// Function call
print_substring(S);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach
# Function to print all the substrings
def print_substring(s):
n = len(s)
# Stores the substrings
str = ""
# Stores last index of
# charcters of string s
ans = []
if (n == 0):
print("-1")
return
# Find the last position of
# each character in the string
last_pos = [-1] * 26
for i in range(n - 1, -1, -1):
# Update the last index
if (last_pos[ord(s[i]) - ord('a')] == -1):
last_pos[ord(s[i]) - ord('a')] = i
minp = -1
# Iterate the given string
for i in range(n):
# Get the last index of s[i]
lp = last_pos[ord(s[i]) - ord('a')]
# Extend the current partition
# characters last pos
minp = max(minp, lp)
# If the current pos of
# character equals the min pos
# then the end of partition
if (i == minp):
#Add the respective character to the string
str += s[i]
# Store the partition's
# len and reset variables
print(str, end = " ")
# Update the minp and str
minp = -1
str = ""
else:
str += s[i]
# Driver Code
# Input string
S = "ababcbacadefegdehijhklij"
# Function call
print_substring(S)
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print all the substrings
public static void print_substring(String s)
{
int n = s.Length;
// Stores the substrings
String str = "";
// Stores last index of
// charcters of string s
//List ans = new List();
if (n == 0)
{
Console.Write("-1");
return;
}
// Find the last position of
// each character in the string
int[] last_pos = new int[26];
for(int i = 0; i < 26; i++)
last_pos[i] = -1;
for(int i = n - 1; i >= 0; --i)
{
// Update the last index
if (last_pos[s[i] - 'a'] == -1)
{
last_pos[s[i] - 'a'] = i;
}
}
int minp = -1;
// Iterate the given string
for(int i = 0; i < n; ++i)
{
// Get the last index of s[i]
int lp = last_pos[s[i] - 'a'];
// Extend the current partition
// characters last pos
minp = Math.Max(minp, lp);
// If the current pos of
// character equals the min pos
// then the end of partition
if (i == minp)
{
//Add respective character to the string
str += s[i];
// Store the partition's
// len and reset variables
Console.Write(str + ' ');
// Update the minp and str
minp = -1;
str = "";
}
else
{
str += s[i];
}
}
}
// Driver Code
public static void Main(String[] args)
{
// Input string
String S = "ababcbacadefegdehijhklij";
// Function call
print_substring(S);
}
}
// This code is contributed by Amit Katiyar
输出
ababcbaca defegde hijhklij
时间复杂度: O(N)
辅助空间: O(N)