📜  M操作后按字典顺序最小的字符串

📅  最后修改于: 2021-04-25 00:38:45             🧑  作者: Mango

给定字符串S和整数M。该任务是精确执行M个操作以获得字典上最小的字符串。

  • 在每个操作中,从字符串最佳选择一个字符,然后用紧接的下一个字符(aaa-> aab)更新它,以使字符串在字典上保持最小。
  • 允许对字符串的单个字符进行多次操作。

注意:将“ z”的下一个视为“ a”。
例子:

方法:我们的想法是要实现一个简单的贪婪方法,迭代从字符串的开始字符串,并同时字符串的电流元素上进行迭代的重点,使之尽可能小。

  • 假设当前元素为r ,要使r最小,即a ,它需要9次运算,因此将该值称为distance
  • 现在,检查M是否大于该距离,然后将当前字符更新为’a’并将M的值减小distance 。否则,请继续下一次迭代。
  • 由于z的下一个是a ,因此形成了26个周期。因此,字符串的最后一个字符可以用最后一个字符+(M%26)更新

下面是上述方法的实现:

C++
// C++ implementation to find the
// lexicographical smallest string
// after performing M operations
  
#include 
using namespace std;
  
// Function to find the
// lexicographical smallest string
// after performing M operations
void smallest_string(string s, int m)
{
  
    // Size of the given string
    int n = s.size();
  
    // Declare an array a
    int a[n];
  
    // For each i, a[i] contain number
    // of operations to update s[i] to 'a'
    for (int i = 0; i < n; i++) {
        int distance = s[i] - 'a';
        if (distance == 0)
            a[i] = 0;
  
        else
            a[i] = 26 - distance;
    }
  
    for (int i = 0; i < n; i++) {
        // Check if m >= ar[i],
        // then update s[i] to 'a'
        // decrement k by a[i]
        if (m >= a[i]) {
            s[i] = 'a';
            m = m - a[i];
        }
    }
  
    // Form a cycle of 26
    m = m % 26;
  
    // update last element of
    // string with the value
    // s[i] + (k % 26)
    s[n - 1] = s[n - 1] + m;
  
    // Return teh answer
    cout << s;
}
  
// Driver code
int main()
{
    string str = "aazzx";
    int m = 6;
    smallest_string(str, m);
    return 0;
}


Java
// Java implementation to find the
// lexicographical smallest String
// after performing M operations
class GFG{
  
// Function to find the
// lexicographical smallest String
// after performing M operations
static void smallest_String(char []s, int m)
{
  
    // Size of the given String
    int n = s.length;
  
    // Declare an array a
    int []a = new int[n];
  
    // For each i, a[i] contain number
    // of operations to update s[i] to 'a'
    for (int i = 0; i < n; i++) 
    {
        int distance = s[i] - 'a';
        if (distance == 0)
            a[i] = 0;
  
        else
            a[i] = 26 - distance;
    }
  
    for (int i = 0; i < n; i++) 
    {
        // Check if m >= ar[i],
        // then update s[i] to 'a'
        // decrement k by a[i]
        if (m >= a[i]) 
        {
            s[i] = 'a';
            m = m - a[i];
        }
    }
  
    // Form a cycle of 26
    m = m % 26;
  
    // update last element of
    // String with the value
    // s[i] + (k % 26)
    s[n - 1] = (char) (s[n - 1] + m);
  
    // Return teh answer
    System.out.print(String.valueOf(s));
}
  
// Driver code
public static void main(String[] args)
{
    String str = "aazzx";
    int m = 6;
    smallest_String(str.toCharArray(), m);
}
}
  
// This code is contributed by Princi Singh


Python3
# Python3 implementation to find the 
# lexicographical smallest string 
# after performing M operations 
  
# Function to find the
# lexicographical smallest string
# after performing M operations
def smallest_string(s, m):
      
    # Size of the given string
    n = len(s);
      
    l = list(s)
  
    # Declare an array a
    a = [0] * n;
  
    # For each i, a[i] contain number
    # of operations to update s[i] to 'a'
    for i in range(n): 
        distance = ord(s[i]) - ord('a');
          
        if (distance == 0):
            a[i] = 0;
        else:
            a[i] = 26 - distance;
      
    for i in range(n):
          
        # Check if m >= ar[i],
        # then update s[i] to 'a'
        # decrement k by a[i]
        if (m >= a[i]):
            l[i] = 'a';
            m = m - a[i];
          
    # Form a cycle of 26
    m = m % 26;
  
    # update last element of
    # with the value
    # s[i] + (k % 26)
      
    # Return teh answer
    for i in range(len(l) - 1):
        print(l[i], end = "")
      
    print(chr(ord(l[n - 1]) + m))
  
# Driver code
str = "aazzx";
m = 6;
  
smallest_string(str, m);
  
# This code is contributed by grand_master


C#
// C# implementation to find the
// lexicographical smallest String
// after performing M operations
using System;
  
class GFG{
  
// Function to find the
// lexicographical smallest String
// after performing M operations
static void smallest_String(char []s, int m)
{
      
    // Size of the given String
    int n = s.Length;
  
    // Declare an array a
    int []a = new int[n];
  
    // For each i, a[i] contain number
    // of operations to update s[i] to 'a'
    for(int i = 0; i < n; i++) 
    {
        int distance = s[i] - 'a';
        if (distance == 0)
            a[i] = 0;
        else
            a[i] = 26 - distance;
    }
  
    for(int i = 0; i < n; i++)
    {
          
        // Check if m >= ar[i],
        // then update s[i] to 'a'
        // decrement k by a[i]
        if (m >= a[i]) 
        {
            s[i] = 'a';
            m = m - a[i];
        }
    }
  
    // Form a cycle of 26
    m = m % 26;
  
    // Update last element of
    // String with the value
    // s[i] + (k % 26)
    s[n - 1] = (char)(s[n - 1] + m);
  
    // Return teh answer
    Console.Write(String.Join("", s));
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "aazzx";
    int m = 6;
      
    smallest_String(str.ToCharArray(), m);
}
}
  
// This code is contributed by Princi Singh


输出:
aaaab

时间复杂度: O(N),其中N是给定字符串的长度
辅助空间: O(N),其中N是给定字符串的长度