返回介绍

solution / 0800-0899 / 0899.Orderly Queue / README_EN

发布于 2024-06-17 01:03:33 字数 4456 浏览 0 评论 0 收藏 0

899. Orderly Queue

中文文档

Description

You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string.

Return _the lexicographically smallest string you could have after applying the mentioned step any number of moves_.

 

Example 1:

Input: s = "cba", k = 1
Output: "acb"
Explanation: 
In the first move, we move the 1st character 'c' to the end, obtaining the string "bac".
In the second move, we move the 1st character 'b' to the end, obtaining the final result "acb".

Example 2:

Input: s = "baaca", k = 3
Output: "aaabc"
Explanation: 
In the first move, we move the 1st character 'b' to the end, obtaining the string "aacab".
In the second move, we move the 3rd character 'c' to the end, obtaining the final result "aaabc".

 

Constraints:

  • 1 <= k <= s.length <= 1000
  • s consist of lowercase English letters.

Solutions

Solution 1: Case-by-case Judgment

If $k = 1$, we can only move the first character of the string to the end of the string each time, resulting in $|s|$ different states. We return the string with the smallest lexicographic order.

If $k > 1$, for a string like $abc[xy]def$, we can move $a$, $b$, and $c$ to the end in order, resulting in $[xy]defabc$. Then we move $y$ and $x$ to the end, resulting in $defabc[yx]$. Finally, we move $d$, $e$, and $f$ to the end, resulting in $abc[yx]def$. This way, we have swapped $y$ and $x$.

Therefore, as long as $k > 1$, we can swap any two adjacent characters in the string, eventually obtaining a string sorted in ascending order.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

class Solution:
  def orderlyQueue(self, s: str, k: int) -> str:
    if k == 1:
      ans = s
      for _ in range(len(s) - 1):
        s = s[1:] + s[0]
        ans = min(ans, s)
      return ans
    return "".join(sorted(s))
class Solution {
  public String orderlyQueue(String s, int k) {
    if (k == 1) {
      String ans = s;
      StringBuilder sb = new StringBuilder(s);
      for (int i = 0; i < s.length() - 1; ++i) {
        sb.append(sb.charAt(0)).deleteCharAt(0);
        if (sb.toString().compareTo(ans) < 0) {
          ans = sb.toString();
        }
      }
      return ans;
    }
    char[] cs = s.toCharArray();
    Arrays.sort(cs);
    return String.valueOf(cs);
  }
}
class Solution {
public:
  string orderlyQueue(string s, int k) {
    if (k == 1) {
      string ans = s;
      for (int i = 0; i < s.size() - 1; ++i) {
        s = s.substr(1) + s[0];
        if (s < ans) ans = s;
      }
      return ans;
    }
    sort(s.begin(), s.end());
    return s;
  }
};
func orderlyQueue(s string, k int) string {
  if k == 1 {
    ans := s
    for i := 0; i < len(s)-1; i++ {
      s = s[1:] + s[:1]
      if s < ans {
        ans = s
      }
    }
    return ans
  }
  t := []byte(s)
  sort.Slice(t, func(i, j int) bool { return t[i] < t[j] })
  return string(t)
}
function orderlyQueue(s: string, k: number): string {
  if (k > 1) {
    return [...s].sort().join('');
  }
  const n = s.length;
  let min = s;
  for (let i = 1; i < n; i++) {
    const t = s.slice(i) + s.slice(0, i);
    if (t < min) {
      min = t;
    }
  }
  return min;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文