📌  相关文章
📜  M 次操作后的字典序最小字符串

📅  最后修改于: 2021-09-07 03:02:11             🧑  作者: Mango

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

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

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

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

  • 假设,当前元素是r ,为了使r最小,即a ,需要 9 次操作,让这个值称为距离
  • 现在,检查 M 是否大于等于distance ,然后将当前字符更新为 ‘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 是给定字符串的长度

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