Untitled
unknown
plain_text
3 years ago
2.7 kB
2
Indexable
Never
import Foundation class Solution { var password = "" var stepsNeeded = 0 let minimumChar = 6 let maximumChar = 20 func strongPasswordChecker(_ password: String) -> Int { self.password = password if !oneUpperLetterExist() { stepsNeeded += 1 } if !oneLowerLetterExist() { stepsNeeded += 1 } if !oneDigitExist() { stepsNeeded += 1 } print(stepsNeeded) let array = threeRepeatingCharacterCount() print(array) for rep in array { stepsNeeded += (rep - 2) } print(stepsNeeded) if !minimumCharacterExists() { let count = password.count if count > maximumChar { stepsNeeded = count - maximumChar } else if count < minimumChar { stepsNeeded = minimumChar - count } } return stepsNeeded } func minimumCharacterExists()-> Bool { return self.password.count >= 6 && password.count <= 20 } func oneUpperLetterExist()-> Bool { let uppercaseLetters = CharacterSet.uppercaseLetters let lettersRange = self.password.rangeOfCharacter(from: uppercaseLetters) return lettersRange != nil } func oneLowerLetterExist()-> Bool { let lowercaseLetters = CharacterSet.lowercaseLetters let lettersRange = self.password.rangeOfCharacter(from: lowercaseLetters) return lettersRange != nil } func oneDigitExist()-> Bool { let decimalCharacters = CharacterSet.decimalDigits let decimalRange = self.password.rangeOfCharacter(from: decimalCharacters) return decimalRange != nil } func threeRepeatingCharacterCount()-> [Int] { var repeatingCharactersCount = [Int]() var repeatingCharacters = [Character]() var passRef = self.password var firstIndex = passRef.startIndex while (firstIndex != passRef.endIndex) { var secondIndex = passRef.index(after: firstIndex) var count = 0 while (secondIndex != passRef.endIndex) { if (passRef[firstIndex] == passRef[secondIndex]) { count += 1 } secondIndex = passRef.index(after: secondIndex) } repeatingCharactersCount.append(count) count = 0 firstIndex = passRef.index(after: firstIndex) } return repeatingCharactersCount } } let count = Solution.init().strongPasswordChecker("1337C0d3")