LeetCode 899.有序队列|字符串最小表示法的使用|求循环字符串的最小值

Tags: #String

For a string s, each time you can choose one of first k letters and put it the end of string s. What is the lexicographically smallest string you can obtain?

Example1:

Input: s="dbbca", k=1
Output: s="adbbc"
Explanation:
Continuously put the first letter to end of s till 'a' is the first letter.

Example2:

Input: s="baaca", k=3
Output: s="aaabc"
Explanation:
First move: 'b' to end, now s="aacab"
Second move: 'c' to end, now s="aaabc"

Let’s divide this problem into two situations:

k>1(Simple)

Let’s consider k=2, we can move either the first letter or second letter to the end. If we move the second letter to the end, that is equal to move the letter advance one space in the string cycle: "bacc", move "a", then the string becomes "bcca", which is equal to ” abcc“(just cycle 3 times). So that means we can move any character to any where in the string. Then we can always obtain a sorted lexicographically smallest string.

Any other case where k>2 is also the same.

    // if k>1
    char[] arr=s.toCharArray();
    Arrays.sort(arr);
    return String.valueOf(arr);

k=1(Interesting)

Each time we can only move one character to string end. That is Cyclic isomorphism(循环同构):

[S_i=S[i,n]+S[0,i]=T ]

(S) and (T) are called cyclic isomorphism.

To find the smallest cyclic isomorphism, we just need to index where the answer string starts in the string s. So we need to compare those n=s.length() strings.

Choose the i=0, j=1 as two strings’ beginning, compare them.

Compare each letter at same position of them. If s[i+k]<s[j+k]< code>, <span class="math inline">\(k\in(0,n)\)</span>, them string <span class="math inline">\(S_j\)</span> must bigger than string <span class="math inline">\(S_i\)</span>. For example:<br><code>s="aaabc", i=0, j=1</code>:</s[j+k]<>

  • k=0,1, s[i+k]=s[j+k]='a'
  • k=2, s[i+k]='a' < s[j+k]='b', so string (S_i) start with i=0: "aaabc" is smaller than (S_j) "aabca" which is start with j=1.

So string (S_j) j=1 must not be the answer. Then we should j start next?

Consider string (A) and (B), they are (S_i) and (S_j) of string s, which means they start at (i) and (j). The first (k) letters of them are equal:

[S[i,i+k-1]=S[j,j+k-1] ]

W.o.l.g, consider (S[i+k]

We only need to go through the string one time(till the bigger one of i or j reach the end). So the time complexity is (O(n)), that’s quick!

Code:

public String orderlyQueue(String s, int m) {
    if (m == 1) {
        int i = 0, j = 1, k = 0, n = s.length();
        while (i < n && j < n && k < n) {
            char iCh = s.charAt((i + k) % n), jCh = s.charAt((j + k) % n);
            if (iCh == jCh)
                k++;
            else {
                if (iCh > jCh) {
                    i += k + 1;
                } else {
                    j += k + 1;
                }
                if (i == j)
                    ++i;
                k = 0;
            }
        }
        i = Math.min(i, j);
        return s.substring(i) + s.substring(0, i);
    } else {
        char[] arr = s.toCharArray();
        Arrays.sort(arr);
        return String.valueOf(arr);
    }
}

Original: https://www.cnblogs.com/liuzhch1/p/16555486.html
Author: liuzhch1
Title: LeetCode 899.有序队列|字符串最小表示法的使用|求循环字符串的最小值

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606692/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 【Javaweb】在项目中添加MyBatis依赖等

    pom.xml 仓库 如果你没有配置阿里云仓库镜像源,可以到这里来找 https://mvnrepository.com/ 如果你配置了阿里云仓库镜像源,可以来这里找 https:…

    Linux 2023年6月14日
    087
  • C语言静态库与动态库

    库 是一种代码的二进制的封装形式,将.o文件打包封装就成了库。库可以在任何地方使用,但用户却不能看见他的具体实现。库有利于代码模块化,只要接口设计得合理,改变库的内部实现,不会影响…

    Linux 2023年6月7日
    084
  • 虚拟机网络地址配置你不知道的事儿-服务器的种类

    想必大家在初学Linux过程中,应该都是跟我一样白嫖一台虚拟机进行使用把,但是在大家白嫖的同时知不知道我们公司内是使用的什么样的服务器呢?公司肯定不会跟我们一样在自己电脑进行安装虚…

    Linux 2023年5月27日
    080
  • 数据结构 单链表

    cpp;gutter:true;</p> <h1>include</h1> <h1>define null 0;</h1&gt…

    Linux 2023年6月13日
    072
  • SSH的 Write failed: Broken pipe 问题

    问题现象: 表示连接管道已经断开 解决方法: 方法一:客户端配置在客户端的 ~/.ssh/ config文件(如不存在请自行创建)中添加下面内容:ServerAliveInterv…

    Linux 2023年6月8日
    078
  • muduo源码分析之muduo简单运用

    今天不先实现 muduo项目,我们先来看下 muduo库的基本使用,只有了解了如何用,才能在写代码的时候知道自己写的找个函数是干嘛的,实际上是怎么使用的这个函数。首先说简单点,就是…

    Linux 2023年6月13日
    077
  • 华为学习笔记一初识VRP

    VRP简介 VRP是Versatile Routing Platform的简称,是华为公司从低端到高端的全系列路由器、交换机等数据通信产品的通用网络操作系统。华为网络设备功能的配置…

    Linux 2023年6月7日
    0120
  • SlugRelatedField字段

    该字段用于外键字段该字段在序列化的时候多用于反向查询,在反序列化的时候用于接收关联表的唯一字段来生成该关联对象eg: 序列化 class PublishListSerializer…

    Linux 2023年6月14日
    087
  • SLF4J 日志门面

    SLF4J( Simple Logging Facade For Java),即 简单日志门面。主要是为了给 Java 日志访问提供一套标准、规范的 API 框架,其主要意义在于提…

    Linux 2023年6月8日
    082
  • OpenStack cinder对接NFS后端存储

    配置NFS服务 安装NFS服务 查询是否安装 [root@nfs ~]# rpm -qa |grep nfs nfs-utils-1.3.0-0.8.el7.x86_64 如没有安…

    Linux 2023年6月8日
    0123
  • 关于如何在window下执行SQLSERVER的定时备份

    引言 在使用SqlServer Express 版本的时候发现,这个版本不支持通过数据库的代理方式进行数据库的维护。 解决方案 使用SQL语句加windows任务计划的方式解决具体…

    Linux 2023年6月14日
    087
  • Hadoop伪分布式的搭建

    1.准备Linux环境1.1 开启网络,ifconfig指令查看ip 1.2 修改主机名为自己名字(hadoop) 1.3修改主机名和IP的映射关系 1.4关闭防火墙 1.5重启L…

    Linux 2023年5月27日
    063
  • pyQt的对话框

    1. 在对话框中输入文字 from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, Q…

    Linux 2023年6月7日
    080
  • 洛谷P3372–线段树代码模板1

    时空限制:1000ms,128M 数据规模: 对于30%的数据:N Original: https://www.cnblogs.com/ygsworld/p/11279732.ht…

    Linux 2023年6月7日
    0106
  • Identity Server 4资源拥有者密码认证控制访问API(二)

    基于上一篇文章中的代码进行继续延伸,只需要小小的改动即可,不明白的地方可以先看看本人上一篇文章及源码: Identity Server 4客户端认证控制访问API 一、 Quick…

    Linux 2023年6月13日
    0103
  • Ubuntu常用命令

    Ubuntu(18.04)下更改用户名和主机名 更改主机名字: (1)修改hostname文件 这个文件中的内容是用来显示主机名的,修改这个文件后,立刻重启 (2)修改hosts文…

    Linux 2023年6月13日
    074
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球