Skip to content

Instantly share code, notes, and snippets.

@Roshankumar350
Last active December 11, 2020 03:42
Show Gist options
  • Save Roshankumar350/2e84492b620af829a8a201a62e985cc3 to your computer and use it in GitHub Desktop.
Save Roshankumar350/2e84492b620af829a8a201a62e985cc3 to your computer and use it in GitHub Desktop.
func isvalidPasswordPolicy(_ password: String) -> Bool {
// Output
var isValidPasswordPolicy = false
// Get the component of each password policy
let components = password.components(separatedBy: CharacterSet.whitespaces)
// From 1st component get the lower and upper bound
let first = components[0].components(separatedBy: "-")
guard let lowerBound = Int(first.first ?? "0") else { return isValidPasswordPolicy }
guard let upperBound = Int(first.last ?? "0") else { return isValidPasswordPolicy }
// From 2nd component get the character upon which policy can be applied
let second = components[1].first
// From 3rd Component get the entire string
let third = components[2]
// Store the character count
var countOfChar = 0
// Check if there is match
for eachChar in third {
if eachChar == second {
countOfChar += 1
}
}
// check if count lies in between or including lower or upper bound
if countOfChar >= lowerBound && countOfChar <= upperBound {
isValidPasswordPolicy = true
}
// return it
return isValidPasswordPolicy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment