首页 > 其他分享 >[LeetCode] 1544. Make The String Great

[LeetCode] 1544. Make The String Great

时间:2022-11-08 11:36:13浏览次数:66  
标签:字符 Great string -- Make Character 1544 queue 字符串

Given a string s of lower and upper case English letters.

A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:

  • 0 <= i <= s.length - 2
  • s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

Notice that an empty string is also good.

Example 1:

Input: s = "leEeetcode"
Output: "leetcode"
Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".

Example 2:

Input: s = "abBAcC"
Output: ""
Explanation: We have many possible scenarios, and all lead to the same answer. For example:
"abBAcC" --> "aAcC" --> "cC" --> ""
"abBAcC" --> "abBA" --> "aA" --> ""

Example 3:

Input: s = "s"
Output: "s"

Constraints:

  • 1 <= s.length <= 100
  • s contains only lower and upper case English letters.

整理字符串。

给你一个由大小写英文字母组成的字符串 s 。

一个整理好的字符串中,两个相邻字符 s[i] 和 s[i+1],其中 0<= i <= s.length-2 ,要满足如下条件:

若 s[i] 是小写字符,则 s[i+1] 不可以是相同的大写字符。
若 s[i] 是大写字符,则 s[i+1] 不可以是相同的小写字符。
请你将字符串整理好,每次你都可以从字符串中选出满足上述条件的 两个相邻 字符并删除,直到字符串整理好为止。

请返回整理好的 字符串 。题目保证在给出的约束条件下,测试样例对应的答案是唯一的。

注意:空字符串也属于整理好的字符串,尽管其中没有任何字符。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/make-the-string-great
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是 stack。题意说了不能有两个相邻的字符是互为大小写的,所以当我们遍历字符串中的每一个 char 的时候,如果当前的 char 是小写字母,需要看一下之前一个字母是否是对应的大写;同样的,如果当前的 char 是大写字母,需要看一下之前一个字母是否是对应的小写。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public String makeGood(String s) {
 3         Deque<Character> queue = new ArrayDeque<>();
 4         for (char c : s.toCharArray()) {
 5             if (queue.isEmpty()) {
 6                 queue.offer(c);
 7             } else if (Character.isUpperCase(c) && Character.isLowerCase(queue.peekLast()) && Character.toLowerCase(c) == queue.peekLast()) {
 8                 queue.pollLast();
 9             } else if (Character.isLowerCase(c) && Character.isUpperCase(queue.peekLast()) && Character.toUpperCase(c) == queue.peekLast()) {
10                 queue.pollLast();
11             } else {
12                 queue.offerLast(c);
13             }
14         }
15 
16         StringBuilder sb = new StringBuilder();
17         while (!queue.isEmpty()) {
18             sb.append(queue.pollFirst());
19         }
20         return sb.toString();
21     }
22 }

 

LeetCode 题目总结

标签:字符,Great,string,--,Make,Character,1544,queue,字符串
From: https://www.cnblogs.com/cnoodle/p/16869088.html

相关文章

  • [CMake] FILE指令-Filesystem
    1FILE(GLOB<variable>2[LIST_DIRECTORIEStrue|flase])[RELETIVE<path>][CONFIGURE_DEPENDS]3[<globbing-expressions>...])4FILE(GLOB_RECURSE<......
  • OpenGL ES EGL eglMakeCurrent
    目录一.EGL前言二.EGL绘制流程简介三.eglMakeCurrent函数简介1.eglMakeCurrent简介2.eglMakeCurrent实现3.eglMakeCurrent使用四.关于多个EGLContext......
  • Xiaoning Sun-2022-OverlookedPosesActuallyMakeSence-Distilling Privileged Knowled
    #OverlookedPosesActuallyMakeSense:DistillingPrivilegedKnowledgeforHumanMotionPrediction#paper1.paper-info1.1.MetadataAuthor::[[XiaoningS......
  • Ubuntu系统中CUDA套件nvvp启动后报错Unable to make protected void java.net.URLClas
    最近在看cuda方面的内容,需要对cuda代码做一些性能分析,于是需要使用nvvp,但是启动nvvp后报错:Causedby:java.lang.reflect.InaccessibleObjectException:Unabletomakepr......
  • build opencv with qt to make debugging easier
    最近发现使用自己编译的opencv,可以增强opencv的imshow函数功能。从哪里得到的线索已经忘记了。可能是无意中发现的某一张截图吧。要实现cv::imshow函数功能增强,需要在cmak......
  • Makefile 学习二:命令和变量
    你必须非常努力,才能看起来毫不费力!微信搜索公众号[漫漫Coding路],一起FromZeroToHero!前言在Go语言开发中,我们希望能够规范代码风格,每个成员在提交时可以一键格......
  • Makefile.win recipe for target '项目1.exe' failed
    在运行代码的时候出现了这个问题,查阅了许多资料,有的说是编译器的问题,有的说是重复定义变量名称的问题,在对代码检查后发现不是这两者的问题是我前面数组定义有问题,将数组定义......
  • CMake 安装和打包
    为了方便使用项目编译的目标文件,快速部署到目标目录,可以使用CMake的安装功能;如果需要对外发布,提供头文件、库文件、或者demo的压缩包则可以使用CMake的打包功能。在本......
  • 我为什么建议不要用CMake
    作者:朱金灿CMake的优点  CMake在开源代码中最常见的应用场景之一是生成很多平台的makefile或者project文件,这种统一管理的方式确实有其方便之处。尽管CMake有一些优点,我......
  • cmake 安装文件解释
    ########################################Installationsection########################################install(DIRECTORYconfigDESTINATION${DA......