[Swift]LeetCode420. 強密碼檢驗器 | Strong Password Checker

原文地址:http://www.javashuo.com/article/p-kvigzkkz-bd.html html

A password is considered strong if below conditions are all met:git

  1. It has at least 6 characters and at most 20 characters.
  2. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit.
  3. It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met).

Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM change required to make s a strong password. If s is already strong, return 0.app

Insertion, deletion or replace of any one character are all considered as one change.ide


一個強密碼應知足如下全部條件:函數

  1. 由至少6個,至多20個字符組成。
  2. 至少包含一個小寫字母,一個大寫字母,和一個數字。
  3. 同一字符不能連續出現三次 (好比 "...aaa..." 是不容許的, 可是 "...aa...a..." 是能夠的)。

編寫函數 strongPasswordChecker(s),s 表明輸入字符串,若是 s 已經符合強密碼條件,則返回0;不然返回要將 s 修改成知足強密碼條件的字符串所須要進行修改的最小步數。ui

插入、刪除、替換任一字符都算做一次修改。spa


Runtime: 4 ms
Memory Usage: 19.5 MB
 1 class Solution {
 2     func strongPasswordChecker(_ s: String) -> Int {
 3         var digitNum = 0
 4         var upperNum = 0
 5         var lowerNum = 0
 6 
 7         var repeatArr = Array<Int>()
 8         var lastChar = 0
 9         var repeatnum = 1
10         for c in s.unicodeScalars{
11             let num = c.value
12             if num >= 48 && num < 58{
13                 digitNum += 1
14             }
15             if num >= 65 && num < 91{
16                 upperNum += 1
17             }
18             if num >= 97 && num < 123{
19                 lowerNum += 1
20             }
21             if num == lastChar{
22                 repeatnum += 1
23             }else{
24                 if repeatnum >= 3{
25                     repeatArr.append(repeatnum)
26                 }
27                 repeatnum = 1
28             }
29             lastChar = Int(num)
30         }
31         if repeatnum >= 3{
32             repeatArr.append(repeatnum)
33         }
34         //補全三種字符須要的步驟 (插入,替換)
35         var step1 = 0
36         step1 += digitNum == 0 ? 1:0
37         step1 += upperNum == 0 ? 1:0
38         step1 += lowerNum == 0 ? 1:0
39 
40         //插入
41         if s.count < 6{
42             //不滿6位 與 種類不齊均可以經過 插入同時結局 因此 取大值
43             return max(step1,6-s.count)
44         }
45         //大於6位只進行替換 和 刪除,二者互斥 因此求和
46         else{
47             //至少須要的刪除操做
48             let deleteStep = (s.count - 20) > 0 ? s.count - 20 : 0
49             var deleteStepTemp = deleteStep
50             for i in 0...2{
51                 if deleteStepTemp < (i+1){
52                     break
53                 }
54                 for index in 0..<repeatArr.count{
55                     if deleteStepTemp < (i+1){
56                         break
57                     }
58                     let num = repeatArr[index]
59                     if num % 3 == i && num >= 3{
60                         repeatArr[index] = num - i - 1
61                         deleteStepTemp -= (i + 1)
62                     }
63                 }
64             }
65             //須要經過替換消除3連的部署(每n個數相連 須要替換n/3個)
66             var changeStep = 0
67             for i in 0..<repeatArr.count{
68                 let num = repeatArr[i]
69                 changeStep += num/3
70             }
71             //消除3連的替換changeStep步數 和 step1步數 能夠同事知足 因此取大值
72             var res = max(changeStep,step1)
73             //總步數 = 必須刪除的步數 + 須要替換的步數
74             res = deleteStep + res
75             return res
76         }
77     }
78 }

Runtime: 8 mscode

Memory Usage: 19.5 MB
 1 class Solution {
 2     func strongPasswordChecker(_ s: String) -> Int {
 3         var requiredChar:Int = GetRequiredChar(s)
 4         if s.count < 6
 5         {
 6             return max(requiredChar, 6 - s.count)
 7         }
 8         var replace:Int = 0
 9         var oned:Int = 0
10         var twod:Int = 0
11 
12         var i:Int = 0
13         var arrS:[Character] = Array(s)
14         while(i < s.count)
15         {
16             var len:Int = 1
17             while(i + len < s.count && arrS[i + len] == arrS[i + len - 1])
18             {
19                 len += 1
20             }
21             if len >= 3
22             {
23                 replace += len / 3
24                 if len % 3 == 0 {oned += 1}
25                 if len % 3 == 1 {twod += 2}
26             }
27             i += len
28         }
29         if s.count <= 20
30         {
31             return max(requiredChar, replace)
32         }
33         var deleteCount:Int = s.count - 20
34         replace -= min(deleteCount, oned)
35         replace -= min(max(deleteCount - oned, 0), twod) / 2
36         replace -= max(deleteCount - oned - twod, 0) / 3
37         return deleteCount + max(requiredChar, replace)
38     }
39 
40     func GetRequiredChar(_ s:String) -> Int
41     {
42         var lowercase:Int = 1
43         var uppercase:Int = 1
44         var digit:Int = 1
45         for c in s
46         {
47             if c >= "a" && c <= "z" {lowercase = 0}
48             else if c >= "A" && c <= "Z" {uppercase = 0}
49             else if c >= "0" && c <= "9" {digit = 0}
50         }
51         return lowercase + uppercase + digit
52     }
53 }
相關文章
相關標籤/搜索