首页 > 其他分享 >LeetCode 2434. Using a Robot to Print the Lexicographically Smallest String

LeetCode 2434. Using a Robot to Print the Lexicographically Smallest String

时间:2022-10-09 17:48:10浏览次数:51  
标签:String Perform Robot robot char operation first Lexicographically string

原题链接在这里:https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/

题目:

You are given a string s and a robot that currently holds an empty string t. Apply one of the following operations until s and t are both empty:

  • Remove the first character of a string s and give it to the robot. The robot will append this character to the string t.
  • Remove the last character of a string t and give it to the robot. The robot will write this character on paper.

Return the lexicographically smallest string that can be written on the paper.

Example 1:

Input: s = "zza"
Output: "azz"
Explanation: Let p denote the written string.
Initially p="", s="zza", t="".
Perform first operation three times p="", s="", t="zza".
Perform second operation three times p="azz", s="", t="".

Example 2:

Input: s = "bac"
Output: "abc"
Explanation: Let p denote the written string.
Perform first operation twice p="", s="c", t="ba". 
Perform second operation twice p="ab", s="c", t="". 
Perform first operation p="ab", s="", t="c". 
Perform second operation p="abc", s="", t="".

Example 3:

Input: s = "bdda"
Output: "addb"
Explanation: Let p denote the written string.
Initially p="", s="bdda", t="".
Perform first operation four times p="", s="", t="bdda".
Perform second operation four times p="addb", s="", t="".

Constraints:

  • 1 <= s.length <= 105
  • s consists of only English lowercase letters.

题解:

The point is to find the remaining smallest char. e.g."bdda", we need to find "a".

We first need to count the frequency of char in s.

Have a stack, move the current char to stack, if it is equal or smaller than remaining lowest char, then add it to the result.

Time Complexity: O(n). n = s.length(). each char is only in and out of stack once.

Space: O(n).

AC Java:

 1 class Solution {
 2     public String robotWithString(String s) {
 3         Stack<Character> stk = new Stack<>();
 4         StringBuilder sb = new StringBuilder();
 5         int [] map = new int[26];
 6         char [] charArr = s.toCharArray();
 7         for(char c : charArr){
 8             map[c - 'a']++;
 9         }
10         
11         int low = 0;
12         for(char c : charArr){
13             stk.push(c);
14             map[c - 'a']--;
15             while(low < 26 && map[low] == 0){
16                 low++;
17             }
18             
19             while(!stk.isEmpty() && stk.peek() - 'a' <= low){
20                 sb.append(stk.pop());
21             }
22         }
23         
24         return sb.toString();
25     }
26 }

 

标签:String,Perform,Robot,robot,char,operation,first,Lexicographically,string
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16772994.html

相关文章

  • Mysql 插入中文错误:Incorrect string value: '\xE7\xA8\x8B\xE5\xBA\x8F...' fo
     今天mysql遇到了一点问题。 首先我说一下,mysql安装的话默认编码方式是拉丁文。不是 UTF-8. 这个错误原因就是因为编码格式不一致造成的。  简单粗暴一点,重新建一个......
  • 手写to_string()
    #include<iostream>#include<algorithm>#include<cstring>#include<vector>usingnamespacestd;stringmy_tostring(intx){vector<char>tmp;whi......
  • String、StringBuffer 、StringBuilder、StringJoiner
    一、String、StringBuffer、StringBuilder1、定义用来连接多个字符的,本质就是一个char型的数组,是一种引用类型,并且不能被继承因为是final修饰的Stringstr="abc";相当......
  • 青少年CTF平台-Web-Robots
    题目信息题目名称:Robots题目描述:昨天十三年社团讲课,讲了Robots.txt的作用,小刚上课没有认真听课正在着急,你能不能帮帮忙?题目难度:一颗星解题过程访问题目链接在这里......
  • 月薪15k-40k| LINX ROBOT招聘视觉算法工程师等职位
    公司介绍:杭州灵西机器人智能科技有限公司(LINXROBOT),国家高新技术企业,成立于2017年。公司总部位于杭州,是一家国内领先的应用3D计算机视觉技术和机器人智能规划技术提供产业无......
  • abc272_f Two Strings (后缀数组)
    https://atcoder.jp/contests/abc272/tasks/abc272_f将SS#TT在字符串中排序,看标号为1-n后面有多少2n+2-3n+1的标号然后就会注意题目要的是小于等于,那么要拼成SS......
  • 从搭建到实战,看看这篇robotframework框架深度学习笔记
    每天进步一点点,关注我们哦,每天分享测试技术文章本文章出自【码同学软件测试】码同学公众号:自动化软件测试,领取资料可加:magetest码同学抖音号:小码哥聊软件测试RobotFra......
  • robots.txt在SEO中作用
    robots.txt是什么robots.txt是一个协议,而不是一个命令。robots.txt是搜索引擎中访问网站的时候要查看的第一个文件。robots.txt文件告诉蜘蛛程序在服务器上什么文件是......
  • #yyds干货盘点#慎用JSON.stringfy
    项目中遇到一个bug,一个组件为了保留一份JSON对象,使用JSON.stringify将其转换成字符串,这样做当然是为了避免对象是引用类型造成数据源的污染。但发现后面使用JSON.pars......
  • net中c#教程 string字符串的常用操作
    无论是用net语言,还是java语言,即使用python、php语言,string字符串操作都是最基础的,本博客主要是面对string的教程,希望对小伙伴们有帮助。因为是工作经验的总结,所以博客会不......