Swift刷LeetCode 之 1544-Make The String Great-Easy

14 篇文章 0 订阅
12 篇文章 0 订阅

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.

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

一个好的字符串是没有两个相邻字符s[i]和s[i + 1]的字符串,其中:

0 < =i< =。长度- 2
s[i]是小写字母,s[i + 1]是大写字母,反之亦然。
要使字符串良好,您可以选择两个相邻的使该字符串糟糕的字符并删除它们。你可以继续这样做,直到字符串就好。

使它良好后返回字符串。答案是在给定约束下保证是唯一的。

请注意,空字符串也很好。

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.

Idea:

The general idea is: insert each character of given strings into an empety string, if the lowcase of current char and the lowcase of previous char is same and they are not the same character, which means one of them is lowcase and another is uppercase. Then we remove these two characters to make the string great. We loop through the given string until arrive to its end.

The hardest point for the question is: Ususally, we user string[X] to get the character of a string at the position of X. But in Swift language, this is wrong and will get a error message: 'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead. So I wrote an extension to use subscript and provided us convinence to directly get a char of a string without using String.Index.

一般的思想是:如果当前字符的小写和之前的字符是相同的,并且它们不是相同的字符,也就是说其中一个是小写的,另一个是大写的,那么将给定字符串中的每个字符插入到一个empety字符串中。然后我们删除这两个字符,使字符串更好。我们对给定的字符串进行循环,直到到达它的末尾。
这个问题最困难的一点是:通常,我们使用string[X]来得到一个字符串的字符在X的位置,但在Swift语言中,这是错误的,并会得到一个错误消息:'subscript(_:)'是不可用的:不能下标字符串与Int,使用字符串。指数。所以我写了一个扩展来使用下标,并为我们提供了方便,直接获得一个字符的字符串,而不使用string . index。

   func makeGood(_ s: String) -> String {
            var result = ""
            var resultIndex = -1
            for (_,char) in s.enumerated(){
                result.append(char)
                resultIndex += 1
                if result.count >= 2{
                    if result[resultIndex].lowercased() == result[resultIndex-1].lowercased() && (result[resultIndex] != result[resultIndex-1]){
                       
                        result.removeSubrange(result.index(result.startIndex, offsetBy: resultIndex-1)...result.index(result.startIndex, offsetBy: resultIndex))
                        resultIndex -= 2
                    }
                }
            }
            return result
}

    extension StringProtocol {
        subscript(offset: Int) -> Character {
            return self[index(startIndex, offsetBy: offset)]
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值