📌  相关文章
📜  通过从给定字符串删除字符,可以在词典上实现最小的子序列

📅  最后修改于: 2021-06-25 17:35:27             🧑  作者: Mango

给定长度为N的字符串S ,任务是找到字典上长度最小的子序列(N – 1) ,即从给定的字符串删除单个字符。

例子:

天真的方法:最简单的方法是从给定的字符串生成所有可能的长度为(N – 1)的子序列,并将所有子序列存储在一个数组中。现在,对数组进行排序,并在0位置打印字符串,以显示最小的字典顺序子序列。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the lexicographically
// smallest subsequence of length N-1
void firstSubsequence(string s)
{
    vector allsubseq;
 
    string k;
 
    // Generate all subsequence of
    // length N-1
    for (int i = 0; i < s.length(); i++) {
 
        // Store main value of string str
        k = s;
 
        // Erasing element at position i
        k.erase(i, 1);
        allsubseq.push_back(k);
    }
 
    // Sort the vector
    sort(allsubseq.begin(),
         allsubseq.end());
 
    // Print first element of vector
    cout << allsubseq[0];
}
 
// Driver Code
int main()
{
    // Given string S
    string S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the lexicographically
// smallest subsequence of length N-1
static void firstSubsequence(String s)
{
    Vector allsubseq = new Vector<>();
     
    // Generate all subsequence of
    // length N-1
    for(int i = 0; i < s.length(); i++)
    {
        String k = "";
         
        // Store main value of String str
        for(int j = 0; j < s.length(); j++)
        {
            if (i != j)
            {
                k += s.charAt(j);
            }
        }
        allsubseq.add(k);
    }
 
    // Sort the vector
    Collections.sort(allsubseq);
 
    // Print first element of vector
    System.out.print(allsubseq.get(0));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String S
    String S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the lexicographically
# smallest subsequence of length N-1
def firstSubsequence(s):
 
    allsubseq = []
 
    k = []
 
    # Generate all subsequence of
    # length N-1
    for i in range(len(s)):
 
        # Store main value of string str
        k = [i for i in s]
 
        # Erasing element at position i
        del k[i]
        allsubseq.append("".join(k))
 
    # Sort the vector
    allsubseq = sorted(allsubseq)
 
    # Print first element of vector
    print(allsubseq[0])
 
# Driver Code
if __name__ == '__main__':
     
    # Given string S
    S = "geeksforgeeks"
 
    # Function Call
    firstSubsequence(S)
 
# 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 lexicographically
// smallest subsequence of length N-1
static void firstSubsequence(string s)
{
    List allsubseq = new List();
     
    // Generate all subsequence of
    // length N-1
    for(int i = 0; i < s.Length; i++)
    {
        string k = "";
         
        // Store main value of string str
        for(int j = 0; j < s.Length; j++)
        {
            if (i != j)
            {
                k += s[j];
            }
        }
        allsubseq.Add(k);
    }
 
    // Sort the vector
    allsubseq.Sort();
 
    // Print first element of vector
    Console.WriteLine(allsubseq[0]);
}
 
// Driver Code
public static void Main()
{
     
    // Given string S
    string S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
}
}
 
// This code is contributed by ipg2016107


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the lexicographically
// smallest subsequence of length N-1
void firstSubsequence(string s)
{
    // Store index of character
    // to be deleted
    int isMax = -1;
 
    // Traverse the string
    for (int i = 0;
         i < s.length() - 1; i++) {
 
        // If ith character > (i + 1)th
        // character then store it
        if (s[i] > s[i + 1]) {
            isMax = i;
            break;
        }
    }
 
    // If any character found in non
    // alphabetical order then remove it
    if (isMax >= 0) {
        s.erase(isMax, 1);
    }
 
    // Otherwise remove last character
    else {
        s.erase(s.length() - 1, 1);
    }
 
    // Print the resultant subsequence
    cout << s;
}
 
// Driver Code
int main()
{
    // Given string S
    string S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the lexicographically
// smallest subsequence of length N-1
static void firstSubsequence(String s)
{
     
    // Store index of character
    // to be deleted
    int isMax = -1;
 
    // Traverse the String
    for(int i = 0; i < s.length() - 1; i++)
    {
         
        // If ith character > (i + 1)th
        // character then store it
        if (s.charAt(i) > s.charAt(i + 1))
        {
            isMax = i;
            break;
        }
    }
 
    // If any character found in non
    // alphabetical order then remove it
    if (isMax >= 0)
    {
        s = s.substring(0, isMax) +
            s.substring(isMax + 1);
        // s.rerase(isMax, 1);
    }
 
    // Otherwise remove last character
    else
    {
        //s.erase(s.length() - 1, 1);
        s = s.substring(0, s.length() - 1);
         
    }
 
    // Print the resultant subsequence
    System.out.print(s);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String S
    String S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
 
# Function to find the lexicographically
# smallest subsequence of length N-1
def firstSubsequence(s):
 
    # Store index of character
    # to be deleted
    isMax = -1
 
    # Traverse the String
    for i in range(len(s)):
 
        # If ith character > (i + 1)th
        # character then store it
        if (s[i] > s[i + 1]):
            isMax = i
            break
 
    # If any character found in non
    # alphabetical order then remove it
    if (isMax >= 0):
        s = s[0 : isMax] + s[isMax + 1 : len(s)]
         
    # s.rerase(isMax, 1);
 
    # Otherwise remove last character
    else:
         
        # s.erase(s.length() - 1, 1);
        s = s[0: s.length() - 1]
 
    # Print the resultant subsequence
    print(s)
 
# Driver Code
if __name__ == '__main__':
     
    # Given String S
    S = "geeksforgeeks"
 
    # Function Call
    firstSubsequence(S)
 
# This code is contributed by Princi Singh


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the lexicographically
// smallest subsequence of length N-1
static void firstSubsequence(String s)
{
     
    // Store index of character
    // to be deleted
    int isMax = -1;
 
    // Traverse the String
    for(int i = 0; i < s.Length - 1; i++)
    {
         
        // If ith character > (i + 1)th
        // character then store it
        if (s[i] > s[i + 1])
        {
            isMax = i;
            break;
        }
    }
 
    // If any character found in non
    // alphabetical order then remove it
    if (isMax >= 0)
    {
        s = s.Substring(0, isMax) +
            s.Substring(isMax + 1);
        // s.rerase(isMax, 1);
    }
 
    // Otherwise remove last character
    else
    {
        //s.erase(s.Length - 1, 1);
        s = s.Substring(0, s.Length - 1);
         
    }
 
    // Print the resultant subsequence
    Console.Write(s);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String S
    String S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
}
}
 
// This code is contributed by Amit Katiyar


输出:

eeksforgeeks

时间复杂度: O(N log N)
辅助空间: O(N)

高效的方法:为了优化上述方法,其思想是遍历字符串并检查第i字符是否大于(i + 1)字符,然后简单地删除第i字符并打印剩余的字符串。否则,请删除最后一个元素并打印所需的子序列。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the lexicographically
// smallest subsequence of length N-1
void firstSubsequence(string s)
{
    // Store index of character
    // to be deleted
    int isMax = -1;
 
    // Traverse the string
    for (int i = 0;
         i < s.length() - 1; i++) {
 
        // If ith character > (i + 1)th
        // character then store it
        if (s[i] > s[i + 1]) {
            isMax = i;
            break;
        }
    }
 
    // If any character found in non
    // alphabetical order then remove it
    if (isMax >= 0) {
        s.erase(isMax, 1);
    }
 
    // Otherwise remove last character
    else {
        s.erase(s.length() - 1, 1);
    }
 
    // Print the resultant subsequence
    cout << s;
}
 
// Driver Code
int main()
{
    // Given string S
    string S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the lexicographically
// smallest subsequence of length N-1
static void firstSubsequence(String s)
{
     
    // Store index of character
    // to be deleted
    int isMax = -1;
 
    // Traverse the String
    for(int i = 0; i < s.length() - 1; i++)
    {
         
        // If ith character > (i + 1)th
        // character then store it
        if (s.charAt(i) > s.charAt(i + 1))
        {
            isMax = i;
            break;
        }
    }
 
    // If any character found in non
    // alphabetical order then remove it
    if (isMax >= 0)
    {
        s = s.substring(0, isMax) +
            s.substring(isMax + 1);
        // s.rerase(isMax, 1);
    }
 
    // Otherwise remove last character
    else
    {
        //s.erase(s.length() - 1, 1);
        s = s.substring(0, s.length() - 1);
         
    }
 
    // Print the resultant subsequence
    System.out.print(s);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String S
    String S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
}
}
 
// This code is contributed by Princi Singh

Python3

# Python3 program for the above approach
 
# Function to find the lexicographically
# smallest subsequence of length N-1
def firstSubsequence(s):
 
    # Store index of character
    # to be deleted
    isMax = -1
 
    # Traverse the String
    for i in range(len(s)):
 
        # If ith character > (i + 1)th
        # character then store it
        if (s[i] > s[i + 1]):
            isMax = i
            break
 
    # If any character found in non
    # alphabetical order then remove it
    if (isMax >= 0):
        s = s[0 : isMax] + s[isMax + 1 : len(s)]
         
    # s.rerase(isMax, 1);
 
    # Otherwise remove last character
    else:
         
        # s.erase(s.length() - 1, 1);
        s = s[0: s.length() - 1]
 
    # Print the resultant subsequence
    print(s)
 
# Driver Code
if __name__ == '__main__':
     
    # Given String S
    S = "geeksforgeeks"
 
    # Function Call
    firstSubsequence(S)
 
# This code is contributed by Princi Singh

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the lexicographically
// smallest subsequence of length N-1
static void firstSubsequence(String s)
{
     
    // Store index of character
    // to be deleted
    int isMax = -1;
 
    // Traverse the String
    for(int i = 0; i < s.Length - 1; i++)
    {
         
        // If ith character > (i + 1)th
        // character then store it
        if (s[i] > s[i + 1])
        {
            isMax = i;
            break;
        }
    }
 
    // If any character found in non
    // alphabetical order then remove it
    if (isMax >= 0)
    {
        s = s.Substring(0, isMax) +
            s.Substring(isMax + 1);
        // s.rerase(isMax, 1);
    }
 
    // Otherwise remove last character
    else
    {
        //s.erase(s.Length - 1, 1);
        s = s.Substring(0, s.Length - 1);
         
    }
 
    // Print the resultant subsequence
    Console.Write(s);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String S
    String S = "geeksforgeeks";
 
    // Function Call
    firstSubsequence(S);
}
}
 
// This code is contributed by Amit Katiyar

输出:  

eeksforgeeks

时间复杂度: O(N)
辅助空间: O(1)