📌  相关文章
📜  不是给定字符串的子字符串的最小大小按字典顺序排列的最小字符串

📅  最后修改于: 2022-05-13 01:56:09.005000             🧑  作者: Mango

不是给定字符串的子字符串的最小大小按字典顺序排列的最小字符串

给定一个字符串s ,任务是找到在S中不作为子字符串存在的最小字符的字典最小字符串。

例子:

方法:可以使用BFS(广度优先搜索)算法解决问题。按字典顺序生成所有字符串,并检查它是否作为给定字符串中的子字符串存在。请按照以下步骤解决问题:

  • 初始化一个集合,比如集合来存储给定字符串s 的所有子字符串。
  • 遍历给定字符串S的字符并将所有子字符串存储到 集合。
  • 初始化一个队列,比如stringQueue ,以按字典顺序生成字符串。
  • 最初,将所有从“ a ”到“ z ”的单个数字字符串推入队列。
  • 在队列不为空时迭代:
    • 对于队列前面的当前字符串,检查它是否出现在给定的字符串中。
    • 如果它出现在给定的字符串中,则将所有字符从 ' a ' 单独附加到 ' z ' 并将它们推回队列中,否则打印当前字符串并返回

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find the lexicographically
// smallest string of minimum characters
// not present as substring in string S
void lexicographicalSmallestString(string& S, int n)
{
    // Set which stores all substrings
    // of the string S
    set collection;
 
    // Constructing all substrings of S
    for (int i = 0; i < n; ++i) {
        string cur;
        for (int j = i; j < n; ++j) {
            cur.push_back(S[j]);
 
            // Inserting the current
            // substring to set
            collection.insert(cur);
        }
    }
 
    queue q;
 
    // Initializing BFS queue
    for (int i = 0; i < 26; ++i) {
        q.push(string(1, i + 'a'));
    }
 
    // Loop for the BFS Traversal
    while (!q.empty()) {
 
        // Stores the current
        // lexicographically smallest
        // string of min characters
        auto cur = q.front();
        q.pop();
 
        // If the current string is
        // not present as a substring
        // of the given string
        if (collection.find(cur) == collection.end()) {
 
            // Print Answer
            cout << cur << endl;
            return;
        }
 
        // Append characters from [a-z]
        // to the back of string cur
        // and push into the queue.
        for (int i = 0; i < 26; ++i) {
            cur.push_back(i + 'a');
            q.push(cur);
            cur.pop_back();
        }
    }
}
 
// Driver Code
int main()
{
    string S = "aabacdefghijklmnopqrstuvwxyz";
    int N = S.length();
 
    lexicographicalSmallestString(S, N);
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG{
 
// Function to find the lexicographically
// smallest String of minimum characters
// not present as subString in String S
static void lexicographicalSmallestString(char[] S, int n)
{
   
    // Set which stores all subStrings
    // of the String S
    HashSet collection = new HashSet();
 
    // Constructing all subStrings of S
    for (int i = 0; i < n; ++i) {
        String cur="";
        for (int j = i; j < n; ++j) {
            cur+=(S[j]);
 
            // Inserting the current
            // subString to set
            collection.add(cur);
        }
    }
 
    Queue q = new LinkedList();
 
    // Initializing BFS queue
    for (int i = 0; i < 26; ++i) {
        q.add(String.valueOf((char)((i + 'a'))));
    }
 
    // Loop for the BFS Traversal
    while (!q.isEmpty()) {
 
        // Stores the current
        // lexicographically smallest
        // String of min characters
        String cur = q.peek();
        q.remove();
 
        // If the current String is
        // not present as a subString
        // of the given String
        if (!collection.contains(cur)) {
 
            // Print Answer
            System.out.print(cur +"\n");
            return;
        }
 
        // Append characters from [a-z]
        // to the back of String cur
        // and push into the queue.
        for (int i = 0; i < 26; ++i) {
            cur+=String.valueOf((char)((i + 'a')));
            q.add(cur);
            cur=cur.substring(0,cur.length()-1);
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "aabacdefghijklmnopqrstuvwxyz";
    int N = S.length();
 
    lexicographicalSmallestString(S.toCharArray(), N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# python  implementation of the above approach
from queue import Queue
 
# Function to find the lexicographically
# smallest string of minimum characters
# not present as substring in string S
def lexicographicalSmallestString(S, n):
 
    # Set which stores all substrings
    # of the string S
    collection = set()
 
    # Constructing all substrings of S
    for i in range(0, n):
        cur = ""
        for j in range(i, n):
            cur += (S[j])
 
            # Inserting the current
            # substring to set
            collection.add(cur)
 
    q = Queue()
 
    # Initializing BFS queue
    for i in range(0, 26):
        q.put(chr(i + ord('a')))
 
    # Loop for the BFS Traversal
    while (not q.empty()):
 
        # Stores the current
        # lexicographically smallest
        # string of min characters
        cur = q.get()
 
        # If the current string is
        # not present as a substring
        # of the given string
        if (not (cur in collection)):
 
            # Print Answer
            print(cur)
            return
 
        # Append characters from [a-z]
        # to the back of string cur
        # and push into the queue.
        for i in range(0, 26):
            q.put((cur + chr(i+ord('a'))))
 
# Driver Code
if __name__ == "__main__":
 
    S = "aabacdefghijklmnopqrstuvwxyz"
    N = len(S)
 
    lexicographicalSmallestString(S, N)
 
    # This code is contributed by rakeshsahni


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
// Function to find the lexicographically
// smallest String of minimum characters
// not present as subString in String S
static void lexicographicalSmallestString(char[] S, int n)
{
   
    // Set which stores all subStrings
    // of the String S
    HashSet collection = new HashSet();
 
    // Constructing all subStrings of S
    for (int i = 0; i < n; ++i) {
        String cur = "";
        for (int j = i; j < n; ++j) {
            cur += (S[j]);
 
            // Inserting the current
            // subString to set
            collection.Add(cur);
        }
    }
 
    Queue q = new Queue();
 
    // Initializing BFS queue
    for (int i = 0; i < 26; ++i) {
        q.Enqueue(String.Join("",(char)((i + 'a'))));
    }
 
    // Loop for the BFS Traversal
    while (q.Count != 0) {
 
        // Stores the current
        // lexicographically smallest
        // String of min characters
        String cur = q.Peek();
        q.Dequeue();
 
        // If the current String is
        // not present as a subString
        // of the given String
        if (!collection.Contains(cur)) {
 
            // Print Answer
            Console.Write(cur +"\n");
            return;
        }
 
        // Append characters from [a-z]
        // to the back of String cur
        // and push into the queue.
        for (int i = 0; i < 26; ++i) {
            cur += String.Join("",(char)((i + 'a')));
            q.Enqueue(cur);
            cur=cur.Substring(0,cur.Length-1);
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    String S = "aabacdefghijklmnopqrstuvwxyz";
    int N = S.Length;
 
    lexicographicalSmallestString(S.ToCharArray(), N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript
// Javascript implementation of the above approach
 
// Function to find the lexicographically
// smallest string of minimum characters
// not present as substring in string S
function lexicographicalSmallestString(S, n)
{   
 
    // Set which stores all substrings
    // of the string S
    let collection = new Set();
 
    // Constructing all substrings of S
    for (let i = 0; i < n; ++i) {
        let cur = ""
        for (let j = i; j < n; ++j) {
            cur += S[j];
 
            // Inserting the current
            // substring to set
            collection.add(cur);
        }
    }
 
    let q = [];
 
    // Initializing BFS queue
    for (let i = 0; i < 26; ++i) {
        q.push(String.fromCharCode('a'.charCodeAt(0) + i));
    }
 
    // Loop for the BFS Traversal
    while (q.length) {
 
        // Stores the current
        // lexicographically smallest
        // string of min characters
        let cur = q[0];
        q.shift();
 
        // If the current string is
        // not present as a substring
        // of the given string
        if (!collection.has(cur)) {
 
            // Print Answer
            document.write(cur + '
');             return;         }           // Append characters from [a-z]         // to the back of string cur         // and push into the queue.         for (let i = 0; i < 26; ++i) {             q.push(cur + (String.fromCharCode(i + 'a'.charCodeAt(0))));         }     } }   // Driver Code   let S = "aabacdefghijklmnopqrstuvwxyz"; let N = S.length;   lexicographicalSmallestString(S, N);   // This code is contributed by gfgking.


输出
ad

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