📌  相关文章
📜  通过连接任何前缀及其镜像形式形成的字典上最小的字符串

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

通过连接任何前缀及其镜像形式形成的字典上最小的字符串

给定一个包含N个字符的字符串str ,任务是找到可以通过连接任何前缀及其镜像形式形成的字典上最小的字符串。

例子:

方法:给定的问题可以使用贪心方法来解决。这个想法是选择最大的前缀,它们的 ASCII 值按降序排列。因此,遍历字符串str并将具有其 ASCII字符值的最大前缀按降序存储到字符串prefix中。前缀与其反向的串联是必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find lexicographically
// smallest string formed by concatenating
// any prefix and its mirrored form
string lexicographicallySmallest(string str)
{
    // Stores the largest prefix
    // with character in decreasing
    // order of ascii value
    string prefix = "";
 
    // Initialize prefix string
    prefix += str[0];
 
    // Loop to traverse the string
    for (int i = 1; i < str.length(); i++) {
 
        // If current character is
        // in decreasing order
        if (str[i] < prefix.back()) {
            prefix += str[i];
        }
        else if (str[i] == prefix.back()
                 && prefix.size() > 1) {
            prefix += str[i];
        }
        else {
            break;
        }
    }
 
    // Stores the reversed prefix string
    string rev = prefix;
    reverse(rev.begin(), rev.end());
 
    // Return Answer
    return prefix + rev;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << lexicographicallySmallest(str);
    return 0;
}


Java
// Java program for the above approach
 
class GFG
{
 
  static String reverse(String str) { 
    char[] chars = str.toCharArray(); 
    for (int i = 0, j = str.length() - 1; i < j; i++, j--) { 
      char c = chars[i]; 
      chars[i] = chars[j]; 
      chars[j] = c; 
    } 
    return new String(chars); 
  }
 
  // Function to find lexicographically
  // smallest string formed by concatenating
  // any prefix and its mirrored form
  static String lexicographicallySmallest(String str)
  {
 
    // Stores the largest prefix
    // with character in decreasing
    // order of ascii value
    String prefix = "";
 
    // Initialize prefix string
    prefix += str.charAt(0);
 
    // Loop to traverse the string
    for (int i = 1; i < str.length(); i++) {
 
      // If current character is
      // in decreasing order
      if (str.charAt(i) < prefix.charAt(prefix.length() - 1)) {
        prefix += str.charAt(i);
      }
      else if (str.charAt(i) == prefix.charAt(prefix.length() - 1) && prefix.length() > 1) {
        prefix += str.charAt(i);
      }
      else {
        break;
      }
    }
 
    // Stores the reversed prefix string
    String rev = reverse(prefix);
 
 
    // Return Answer
    return prefix + rev;
  }
 
  // Driver code
  public static void main(String args[])
  {
    String str = "geeksforgeeks";
    System.out.println(lexicographicallySmallest(str));
  }
}
 
// This code is contributed by gfgking


Python3
# Python 3 program for the above approach
 
# Function to find lexicographically
# smallest string formed by concatenating
# any prefix and its mirrored form
def lexicographicallySmallest(st):
 
    # Stores the largest prefix
    # with character in decreasing
    # order of ascii value
    prefix = ""
 
    # Initialize prefix string
    prefix += st[0]
 
    # Loop to traverse the string
    for i in range(1, len(st)):
 
        # If current character is
        # in decreasing order
        if (st[i] < prefix[len(prefix)-1]):
            prefix += st[i]
 
        elif (st[i] == prefix[len(prefix)-1]
              and len(prefix) > 1):
            prefix += st[i]
 
        else:
            break
 
    # Stores the reversed prefix string
    rev = prefix
    rev = list(rev)
    rev.reverse()
    rev = ''.join(rev)
 
    # Return Answer
    return prefix + rev
 
# Driver code
if __name__ == "__main__":
 
    st = "geeksforgeeks"
    print(lexicographicallySmallest(st))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
class GFG
{
 
  static string reverse(string str) { 
    char[] chars = str.ToCharArray(); 
    for (int i = 0, j = str.Length - 1; i < j; i++, j--) { 
      char c = chars[i]; 
      chars[i] = chars[j]; 
      chars[j] = c; 
    } 
    return new string(chars); 
  }
 
  // Function to find lexicographically
  // smallest string formed by concatenating
  // any prefix and its mirrored form
  static string lexicographicallySmallest(string str)
  {
 
    // Stores the largest prefix
    // with character in decreasing
    // order of ascii value
    string prefix = "";
 
    // Initialize prefix string
    prefix += str[0];
 
    // Loop to traverse the string
    for (int i = 1; i < str.Length; i++) {
 
      // If current character is
      // in decreasing order
      if (str[i] < prefix[prefix.Length - 1]) {
        prefix += str[i];
      }
      else if (str[i] == prefix[prefix.Length - 1]
               && prefix.Length > 1) {
        prefix += str[i];
      }
      else {
        break;
      }
    }
 
    // Stores the reversed prefix string
    string rev = reverse(prefix);
 
 
    // Return Answer
    return prefix + rev;
  }
 
  // Driver code
  public static void Main()
  {
    string str = "geeksforgeeks";
    Console.Write(lexicographicallySmallest(str));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
geeeeg

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