首页 > 其他分享 >leetcode-1544-easy

leetcode-1544-easy

时间:2023-03-11 13:45:09浏览次数:34  
标签:peek 1544 string -- stack easy sb leetcode String

Make The String Great

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.

思路一:用栈模拟字符消除

    public String makeGood(String s) {
        char[] chars = s.toCharArray();
        Deque<Character> stack = new ArrayDeque<>();

        for (char c : chars) {
            Character peek = stack.peek();

            if (peek != null && Math.abs(peek - c) == 32) {
                stack.poll();
            } else {
                stack.push(c);
            }
        }

        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pollLast());
        }
        return sb.toString();
    }

标签:peek,1544,string,--,stack,easy,sb,leetcode,String
From: https://www.cnblogs.com/iyiluo/p/17205781.html

相关文章

  • leetcode-977-easy
    SquaresofaSortedArrayGivenanintegerarraynumssortedinnon-decreasingorder,returnanarrayofthesquaresofeachnumbersortedinnon-decreasingor......
  • leetcode-1021-easy
    RemoveOutermostParenthesesAvalidparenthesesstringiseitherempty"","("+A+")",orA+B,whereAandBarevalidparenthesesstrings,and+represe......
  • leetcode-1496-easy
    PathCrossingGivenastringpath,wherepath[i]='N','S','E'or'W',eachrepresentingmovingoneunitnorth,south,east,orwest,respectively.Youstart......
  • leetcode-1523-easy
    CountOddNumbersinanIntervalRangeGiventwonon-negativeintegerslowandhigh.Returnthecountofoddnumbersbetweenlowandhigh(inclusive).Example......
  • LeetCode|2457. 美丽整数的最小增量
    题目链接:2457.美丽整数的最小增量给你两个正整数n和target。如果某个整数每一位上的数字相加小于或等于target,则认为这个整数是一个美丽整数。找出并返回满......
  • 【LeetCode回溯算法#07】子集问题I+II,巩固解题模板并详解回溯算法中的去重问题
    子集力扣题目链接给你一个整数数组nums,数组中的元素互不相同。返回该数组所有可能的子集(幂集)。解集不能包含重复的子集。你可以按任意顺序返回解集。示例1:输......
  • eNSP的使用4  NAT网络地址转换 easyip
    静态转换:一对一绑定 (双向通信)路由器靠地址区分内部主机easy ip :多对一  (单向通信)路由器靠端口(临时端口)区分内部主机easy ip的配置:1.配置aclacl  2000rule perm......
  • easyexcel填坑-校验表头为空,或者不符合预期
    背景:easyexcelv3.1.5实体类已经使用注解@ExcelProperty标注需要导入的属性正文开始关闭忽略空行,防止第一行是空跳过校验 ignoreEmptyRow(false)。此处如果未关闭,第......
  • Leetcode24. 两两交换链表中的节点
    题目描述:给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 思路:使用虚拟头结点,这样会......
  • 【LeetCode回溯算法#06】复原IP地址详解(练习如何处理边界条件,判断IP合法性)
    复原IP地址力扣题目链接(opensnewwindow)给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式。有效的IP地址正好由四个整数(每个整数位于0到255之......