A string S of lowercase letters is given.  Then, we may make any number of moves.

In each move, we choose one of the first K letters (starting from the left), remove it, and place it at the end of the string.

Return the lexicographically smallest string we could have after 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".

Note:

  1. 1 <= K <= S.length <= 1000
  2. S consists of lowercase letters only.

Approach #1: String. [Java]

class Solution {
public String orderlyQueue(String S, int K) {
if (K >= 2) {
char[] arr = S.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
String ret = S;
int len = S.length();
for (int i = 0; i < len; ++i) {
String temp = S.substring(1) + S.charAt(0);
if (temp.compareTo(ret) < 0)
ret = temp;
S = temp;
}
return ret;
}
}

  

Analysis:

1. When K == 1:

We can only rotate the whole string. There are S.length different states and we return the lexicographically smallest string.

2. When K >= 2 you can swap any 2 character in the string:

Assume u have "abcdefg", put "b" into tail, and ten paut "acdefg" into the tail. Then you have "bacdefg". Based on this, u can swap first a and b.

Because the string is a ring, u can sliding it. This means you can swap any 2 character in the string. e.g. "abcdefg" -> "acdefgb" -> "cadefgb".

Reference:

https://leetcode.com/problems/orderly-queue/discuss/165857/Java-Simple-Solution-12-ms

899. Orderly Queue的更多相关文章

  1. LeetCode 899. Orderly Queue

    899. Orderly Queue(有序队列) 题目: 给出了一个由小写字母组成的字符串 S.然后,我们可以进行任意次数的移动. 在每次移动中,我们选择前 K 个字母中的一个(从左侧开始),将其从原 ...

  2. [LeetCode] 899. Orderly Queue 有序队列

    A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we c ...

  3. 【LeetCode】899. Orderly Queue 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/orderly- ...

  4. [Swift]LeetCode899. 有序队列 | Orderly Queue

    A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we c ...

  5. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  6. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  9. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

随机推荐

  1. linux系统学习(一)

    查看linux版本信息:http://distrowatch.com/ linux根目录下系统文件夹的含义 /boot 该目录下默认存放的是linux的启动文件和内核,一般200M swap交换分区, ...

  2. 'for each' statements are only available if source level is 5.0

    在用foreach的时候,出现以下错误: 错误:Syntax error, 'for each' statements are only available if source level is 5. ...

  3. ScreenCapturePro2 for Joomla_3.4.7-tinymce4x

    1.1. 与Joomla_3.4.7-tinymce4x整合 示例下载:Joomla_3.4.7,   1.1.1. 添加screencapture文件夹   1.1.2. 2.添加插件文件夹 路径: ...

  4. 深入探讨 Java 类加载器(转载)

    类加载器(class loader)是 Java™中的一个很重要的概念.类加载器负责加载 Java 类的字节代码到 Java 虚拟机中.本文首先详细介绍了 Java 类加载器的基本概念,包括代理模式. ...

  5. 面试题: 已知一个含有n个不同元素的集合,要求打印其所有具有k个元素的子集(不允许有重复的)

    TX面试题2: 已知一个含有n个元素的集合,要求打印其所有具有k个元素的子集(不允许有重复的) 题目分析, 为了便于说明,不妨将问题简化一下: 已知一个盒子中有n个不同的球,分别标记为{a1,a2,. ...

  6. 面试题:给定一个函数rand()能产生1到m之间的等概率随机数,产生1到n之间等概率的随机数?

    虽然TX的面试已经过去好几天了,然而惨痛的过程还历历在目.人生中第一次正式job面试就这么挂掉了.在于面试官的交流过程中,被问及了几个算法设计题,在今后几篇博文中,我一一总结与诸君分享. 1. 给定一 ...

  7. jquery页面加载效果

    此为有时页面加载很慢时体验效果很不好而写的加载动画 CSS样式: #loading{position:%;left:%;width:124px;height:124px;overflow:hidden ...

  8. Linq to SQL 中将数字转换为字符串

    使用LINQ to Entities中的SqlFunctions调用数据库中的函数 添加引用System.Data.Entity 引用命名空间 using System.Data.Objects.Sq ...

  9. AJAX方式调用百度天气

    后台代码: [HttpPost] public string AjaxWeather() { string CityName = string.IsNullOrEmpty(Request.Form[& ...

  10. VS 2015 IDE 不支持 MS SQL 2000 生成 dbml

    解决办法: 通过命令手动生成 然后把生成的ERendering.dbml 文件,通过工程项目-添加-现有项,加入项目.