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. MVC各个层的作用

    (1)控制器的作用是调用模型,并调用视图,将模型产生的数据传递给视图.并让相关视图去显示.(2)模型的作用是获取数据并处理数据.(3)视图的作用是将取得的数据进行组织.美化等,并最终向用户终端输出.

  2. 编写高质量代码改善C#程序的157个建议——建议155:随生产代码一起提交单元测试代码

    建议155:随生产代码一起提交单元测试代码 首先提出一个问题:我们害怕修改代码吗?是否曾经无数次面对乱糟糟的代码,下决心进行重构,然后在一个月后的某个周一,却收到来自测试版的报告:新的版本,没有之前的 ...

  3. javascript 多个frame之间的交互

    主页面 Web.html  (加载了两个frame 分别为A.html) <html lang="en"> <head> <meta charset= ...

  4. Getting Started with Node.js on Heroku

    NodeJS应用托管平台 https://devcenter.heroku.com/articles/getting-started-with-nodejs#dyno-sleeping-and-sca ...

  5. 关于linq to sql调用存储过程,出现"无法枚举查询结果多次"的问题

    DBML: [Function(Name="dbo.p_GetStudyStageSubjectGroup")] public ISingleResult<STUDYSTAG ...

  6. C++ 内敛函数

    在主调函数调用函数时,先将现场压入栈以保存现场-转去执行被掉函数-返回主调函数.现场出栈以恢复现场-继续往下执行. 为了减少函数调用的成本,特别是对于小型函数,C++提供了内敛函数(inline).C ...

  7. C++ 的写好库编译好,DELPHI或者Java做界面,iOS 和 Android 就都搞定。

    当然也可以使用BCB和相关的开发库来开发App,只是别人没法帮助你. 摘自<想到做到-Android开发关键技术与精彩案例>.(詹建飞) p40

  8. Logiccode GSM SMS .Net Library 3.3

    下载 Mega 百度  密码:5pvb

  9. NET Core 简介

    1. 前言 .NET发行至今已经过了十四个年头.随着版本的不断迭代更新,.NET在Windows平台上的表现也是越来越好,可以说Windows平台上所有的应用类型.NET几乎都能完成. 只是成也Win ...

  10. Commons包详解

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.项目地址http://commons.apache.org/ Commons BeanUtils 提供 ...