📌  相关文章
📜  由不包含任何重复子串的前 K 个字母组成的最大长度的字典序最小字符串

📅  最后修改于: 2021-09-06 17:47:48             🧑  作者: Mango

给定一个正整数K ,任务是按字典顺序查找使用前K个小写字母可以生成的最小字符串,这样在生成的字符串中没有重复长度至少为 2 的子字符串。  

例子:

方法:根据以下观察可以解决给定的问题:

  • 如果所有长度为 2 的子串都是唯一的,那么所有长度大于 2 的子串也将是唯一的。
  • 因此,最大长度字符串应包含按字典顺序排列的所有长度为 2 的唯一子字符串,以便字符串中没有3 个连续字符是相同的。

请按照以下步骤解决问题:

  • 初始化字符串,说S作为空字符串存储结果字符串。
  • 使用变量i迭代小写字母表的所有前K 个字符,并执行以下步骤:
    • 将当前字符i附加到字符串S
    • 从第(i + 1)字符迭代到第K字符,并将字符i后跟字符j附加到字符串S
    • 将字符‘a’添加到字符串S 中,使得由最后一个和第一个字母组成的子字符串也出现在结果字符串。
  • 完成上述步骤后,将字符串S打印为结果字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the lexicographically
// smallest string of the first K lower
// case alphabets having unique substrings
void generateString(int K)
{
    // Stores the resultant string
    string s = "";
 
    // Iterate through all the characters
    for (int i = 97; i < 97 + K; i++) {
 
        s = s + char(i);
 
        // Inner Loop for making pairs
        // and adding them into string
        for (int j = i + 1;
             j < 97 + K; j++) {
            s += char(i);
            s += char(j);
        }
    }
 
    // Adding first character so that
    // substring consisting of the last
    // the first alphabet is present
    s += char(97);
 
    // Print the resultant string
    cout << s;
}
 
// Driver Code
int main()
{
    int K = 4;
    generateString(K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the lexicographically
// smallest string of the first K lower
// case alphabets having unique substrings
static void generateString(int K)
{
     
    // Stores the resultant string
    String s = "";
   
    // Iterate through all the characters
    for(int i = 97; i < 97 + K; i++)
    {
        s = s + (char)(i);
   
        // Inner Loop for making pairs
        // and adding them into string
        for(int j = i + 1; j < 97 + K; j++)
        {
            s += (char)(i);
            s += (char)(j);
        }
    }
   
    // Adding first character so that
    // substring consisting of the last
    // the first alphabet is present
    s += (char)(97);
   
    // Print the resultant string
    System.out.println(s);
}
 
// Driver code
public static void main(String []args)
{
    int K = 4;
     
    generateString(K);
}
}
 
// This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the lexicographically
// smallest string of the first K lower
// case alphabets having unique substrings
static void generateString(int K)
{
     
    // Stores the resultant string
    string s = "";
 
    // Iterate through all the characters
    for(int i = 97; i < 97 + K; i++)
    {
        s = s + (char)(i);
 
        // Inner Loop for making pairs
        // and adding them into string
        for(int j = i + 1; j < 97 + K; j++)
        {
            s += (char)(i);
            s += (char)(j);
        }
    }
 
    // Adding first character so that
    // substring consisting of the last
    // the first alphabet is present
    s += (char)(97);
 
    // Print the resultant string
    Console.Write(s);
}
 
// Driver Code
public static void Main()
{
    int K = 4;
    generateString(K);
}
}
 
// This code is contributed by ukasp


Python3
# python 3 program for the above approach
 
# Function to find the lexicographically
# smallest string of the first K lower
# case alphabets having unique substrings
def generateString(K):
   
    # Stores the resultant string
    s = ""
 
    # Iterate through all the chracters
    for i in range(97,97 + K,1):
        s = s + chr(i);
 
        # Inner Loop for making pairs
        # and adding them into string
        for j in range(i + 1,97 + K,1):
            s += chr(i)
            s += chr(j)
 
    # Adding first chracter so that
    # substring consisting of the last
    # the first alphabet is present
    s += chr(97)
 
    # Print the resultant string
    print(s)
 
# Driver Code
if __name__ == '__main__':
    K = 4
    generateString(K)
 
    # This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
aabacadbbcbdccdda

时间复杂度: O(K 2 )
辅助空间: O(K 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live