微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift Weekly Contest 126]LeetCode1003. 检查替换后的词是否有效 | Check If Word Is Valid After Substitutions

We are given that the string "abc" is valid.

From any valid string V,we may split V into two pieces X and Y such that X + Y (X concatenated with Y) is equal to V.  (X or Y may be empty.)  Then, X + "abc" + Y is also valid.

If for example S = "abc",then examples of valid strings are: "abc","aabcbc","abcabc","abcabcababcc".  Examples of invalid strings are: "abccba""ab""cababc""bac".

Return true if and only if the given string S is valid. 

Example 1:

Input: "aabcbc"
Output: true Explanation: We start with the valid string "abc". Then we can insert another "abc" between "a" and "bc",resulting in "a" + "abc" + "bc" which is "aabcbc". 

Example 2:

Input: "abcabcababcc"
Output: true Explanation: "abcabcabc" is valid after consecutive insertings of "abc". Then we can insert "abc" before the last letter,resulting in "abcabcab" + "abc" + "c" which is "abcabcababcc". 

Example 3:

Input: "abccba"
Output: false 

Example 4:

Input: "cababc"
Output: false 

Note:

  1. 1 <= S.length <= 20000
  2. S[i] is ‘a‘‘b‘,or ‘c‘

给定有效字符串 "abc"

对于任何有效的字符串 V,我们可以将 V 分成两个部分 X 和 Y,使得 X + YX 与 Y 连接)等于 V。(X 或 Y 可以为空。)那么,X + "abc" + Y 也同样是有效的。

例如,如果 S = "abc",则有效字符串的示例是:"abc""aabcbc""abcabc""abcabcababcc"。无效字符串的示例是:"abccba""ab""cababc""bac"

如果给定字符串 S 有效,则返回 true;否则,返回 false。 

示例 1:

输入:"aabcbc"
输出:true
解释:
从有效字符串 "abc" 开始。
然后我们可以在 "a" 和 "bc" 之间插入另一个 "abc",产生 "a" + "abc" + "bc",即 "aabcbc"。

示例 2:

输入:"abcabcababcc"
输出:true
解释:
"abcabcabc" 是有效的,它可以视作在原串后连续插入 "abc"。
然后我们可以在最后一个字母之前插入 "abc",产生 "abcabcab" + "abc" + "c",即 "abcabcababcc"。

示例 3:

输入:"abccba"
输出:false

示例 4:

输入:"cababc"
输出:false 

提示

  1. 1 <= S.length <= 20000
  2. S[i] 为 ‘a‘‘b‘、或 ‘c‘
Runtime: 100 ms
Memory Usage: 20.6 MB
 1 class Solution {
 2     func isValid(_ S: String) -> Bool {
 3         var n:Int = S.count
 4         var arr:[Character] = Array(S)
 5         var h:[Int] = [Int](repeating:0,count:3)
 6         for i in 0..<n
 7         {
 8             h[arr[i].ascii - 97] += 1
 9         }
10         if !S.contains("abc")
11         {
12             return false
13         }
14         if h[0] != h[1] || h[1] != h[2]
15         {
16             return false
17         }
18         var cc:[Int] = [Int](repeating:0,count:3)
19         for i in 0..<n
20         {
21             cc[arr[i].ascii - 97] += 1
22             if cc[0] < cc[1] || cc[1] < cc[2]
23             {
24                 return false
25             }
26         }
27         return true
28     }
29 }
30 
31 //Character扩展 
32 extension Character  
33 {  
34   //转ASCII整数值(定义小写为整数值)
35    var ascii: Int {
36        get {
37            return Int(self.unicodeScalars.first!.value)
38        }       
39     }    
40 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐