给定一个字符串S ,任务是找到按字典顺序最小的字符串,而不是给定字符串S 的子序列。
例子:
Input: S = “abcdefghijklmnopqrstuvwxyz”
Output:
aa
Explanation:
String “aa” is the lexicographically smallest string which is not present in the given string as a subsequence.
Input: S = “aaaa”
Output:
aaab
Explanation:
String “aaa” is the lexicographically smallest string which is not present in the given string as a subsequence.
方法:解决给定问题的想法是找到给定字符串字符‘a’的频率(比如f )。如果f的值小于字符串的长度,那么答案将是通过连接‘a’ f+1次而形成的字符串,因为aaa….(f+1 times)将是所需的字典序最小字符串这不是一个子序列。否则,答案将是通过在字符串连接 ‘ a’(f-1 次)和‘b’形成的字符串。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find lexicographically smallest string that
// is not a subsequence of S
string smallestNonSubsequence(string S, int N)
{
// variable to store frequency of 'a'
int freq = 0;
// calculate frequency of 'a'
for (int i = 0; i < N; i++)
if (S[i] == 'a')
freq++;
// Initialize string consisting of freq number of 'a'
string ans(freq, 'a');
// change the last digit to 'b'
if (freq == N)
ans[freq - 1] = 'b';
// add another 'a' to the string
else
ans += 'a';
// return tha answer string
return ans;
}
// Driver code
int main()
{
// Input
string S = "abcdefghijklmnopqrstuvwxyz";
int N = S.length();
// Function call
cout << smallestNonSubsequence(S, N) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find lexicographically smallest string that
// is not a subsequence of S
static String smallestNonSubsequence(String S, int N)
{
// variable to store frequency of 'a'
int freq = 0;
// calculate frequency of 'a'
for (int i = 0; i < N; i++)
if (S.charAt(i) == 'a')
freq++;
// Initialize string consisting of freq number of 'a'
String ans="";
for(int i = 0; i < f req; i++){
ans += 'a';
}
// change the last digit to 'b'
if (freq == N)
ans = ans.replace(ans.charAt(freq - 1), 'b');
// add another 'a' to the string
else
ans += 'a';
// return tha answer string
return ans;
}
// Driver Code
public static void main(String args[])
{
String S = "abcdefghijklmnopqrstuvwxyz";
int N = S.length();
// Function call
System.out.println(smallestNonSubsequence(S, N));
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to find lexicographically smallest
# string that is not a subsequence of S
def smallestNonSubsequence(S, N):
# Variable to store frequency of 'a'
freq = 0
# Calculate frequency of 'a'
for i in range(N):
if (S[i] == 'a'):
freq += 1
# Initialize string consisting
# of freq number of 'a'
ans = ""
for i in range(freq):
ans += 'a'
# Change the last digit to 'b'
if (freq == N):
ans = ans.replace(ans[freq - 1], 'b')
# Add another 'a' to the string
else:
ans += 'a'
# Return tha answer string
return ans
# Driver code
if __name__ == '__main__':
# Input
S = "abcdefghijklmnopqrstuvwxyz"
N = len(S)
# Function call
print(smallestNonSubsequence(S, N))
# This code is contributed by SURENDRA_GANGWAR
输出
aa
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。