给定一个字符串str,由小写字母的,任务是找到这不是给定的字符串的一个子最短的字符串。如果存在多个字符串,则打印其中任何一个。
例子:
Input: str = “abaabcc”
Output: d
Explanation:
One of the possible shortest string which is not a subsequence of the given string is “d”. Therefore, the required output is “d”.
Input: str = “abcdefghijklmnopqrstuvwxyzaabbccdd”
Output: ze
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 初始化字符串,说shortestString,存储这不是给定的字符串的一个子最短的字符串。
- 初始化一个 Set,比如segments ,以存储每个子段的所有可能的字符。
- 遍历字符串和插入字符串的字符成段,并检查段包含了所有的小写字母或没有。如果发现为真,则将当前字符附加到shortestString 中并从segments 中删除所有元素。
- 遍历[a – z]范围内所有可能的小写字母,并检查当前字符是否存在于该段中。如果发现为真,则将当前字符插入到shortestString 中。
- 最后,打印shortestString的价值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find shortest string which
// not a subsequence of the given string
string ShortestSubsequenceNotPresent(string str)
{
// Stores the shortest string which is
// not a subsequence of the given string
string shortestString;
// Stores length of string
int N = str.length();
// Stores distinct character of subsegments
unordered_set subsegments;
// Traverse the given string
for (int i = 0; i < N; i++) {
// Insert current character
// into subsegments
subsegments.insert(str[i]);
// If all lowercase alphabets
// present in the subsegment
if (subsegments.size() == 26) {
// Insert the last character of
// subsegment into shortestString
shortestString.push_back(str[i]);
// Remove all elements from
// the current subsegment
subsegments.clear();
}
}
// Traverse all lowercase alphabets
for (char ch = 'a'; ch <= 'z'; ch++) {
// If current character is not
// present in the subsegment
if (subsegments.count(ch) == 0) {
shortestString.push_back(ch);
// Return shortestString
return shortestString;
}
}
return shortestString;
}
// Driver Code
int main()
{
// Given String
string str
= "abcdefghijklmnopqrstuvwxyzaabbccdd";
cout << ShortestSubsequenceNotPresent(str);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG {
public static String
ShortestSubsequenceNotPresent(String str)
{
// Stores the shortest string which is
// not a subsequence of the given string
String shortestString = "";
// Stores length of string
int N = str.length();
// Stores distinct character of subsegments
HashSet subsegments = new HashSet<>();
// Traverse the given string
for (int i = 0; i < N; i++) {
// Insert current character
// into subsegments
subsegments.add(str.charAt(i));
// If all lowercase alphabets
// present in the subsegment
if (subsegments.size() == 26) {
// Insert the last character of
// subsegment into shortestString
shortestString
= shortestString + str.charAt(i);
// Remove all elements from
// the current subsegment
subsegments.clear();
}
}
// Traverse all lowercase alphabets
for (char ch = 'a'; ch <= 'z'; ch++) {
// If current character is not
// present in the subsegment
if (!subsegments.contains(ch)) {
shortestString = shortestString + ch;
// Return shortestString
return shortestString;
}
}
return shortestString;
}
// Driver Code
public static void main(String[] args)
{
String str = "abcdefghijklmnopqrstuvwxyzaabbccdd";
System.out.print(
ShortestSubsequenceNotPresent(str));
}
}
// This code is contributed by Manu Pathria
Python3
# Python3 program to implement
# the above approach
# Function to find shortest string which
# not a subsequence of the given string
def ShortestSubsequenceNotPresent(Str):
# Stores the shortest string which is
# not a subsequence of the given string
shortestString = ""
# Stores length of string
N = len(Str)
# Stores distinct character of subsegments
subsegments = set()
# Traverse the given string
for i in range(N):
# Insert current character
# into subsegments
subsegments.add(Str[i])
# If all lowercase alphabets
# present in the subsegment
if (len(subsegments) == 26) :
# Insert the last character of
# subsegment into shortestString
shortestString += Str[i]
# Remove all elements from
# the current subsegment
subsegments.clear()
# Traverse all lowercase alphabets
for ch in range(int(26)):
# If current character is not
# present in the subsegment
if (chr(ch + 97) not in subsegments) :
shortestString += chr(ch + 97)
# Return shortestString
return shortestString
return shortestString
# Driver code
# Given String
Str = "abcdefghijklmnopqrstuvwxyzaabbccdd"
print(ShortestSubsequenceNotPresent(Str))
# This code is contributed by divyeshrabadiya07
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
public static String ShortestSubsequenceNotPresent(
String str)
{
// Stores the shortest string which is
// not a subsequence of the given string
String shortestString = "";
// Stores length of string
int N = str.Length;
// Stores distinct character of subsegments
HashSet subsegments = new HashSet();
// Traverse the given string
for(int i = 0; i < N; i++)
{
// Insert current character
// into subsegments
subsegments.Add(str[i]);
// If all lowercase alphabets
// present in the subsegment
if (subsegments.Count == 26)
{
// Insert the last character of
// subsegment into shortestString
shortestString = shortestString + str[i];
// Remove all elements from
// the current subsegment
subsegments.Clear();
}
}
// Traverse all lowercase alphabets
for(char ch = 'a'; ch <= 'z'; ch++)
{
// If current character is not
// present in the subsegment
if (!subsegments.Contains(ch))
{
shortestString = shortestString + ch;
// Return shortestString
return shortestString;
}
}
return shortestString;
}
// Driver Code
public static void Main(String[] args)
{
String str = "abcdefghijklmnopqrstuvwxyzaabbccdd";
Console.Write(
ShortestSubsequenceNotPresent(str));
}
}
// This code is contributed by Rajput-Ji
输出
ze
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live