-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2015 Contest. LongestPassword.swift
50 lines (40 loc) · 2.41 KB
/
2015 Contest. LongestPassword.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Foundation
import Glibc
// Solution @ Sergey Leschev, Belarusian State University
// 2015 Contest. LongestPassword.
// You would like to set a password for a bank account. However, there are three restrictions on the format of the password:
// it has to contain only alphanumerical characters (a−z, A−Z, 0−9);
// there should be an even number of letters;
// there should be an odd number of digits.
// You are given a string S consisting of N characters. String S can be divided into words by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.
// For example, given "test 5 a0A pass007 ?xy1", there are five words and three of them are valid passwords: "5", "a0A" and "pass007". Thus the longest password is "pass007" and its length is 7. Note that neither "test" nor "?xy1" is a valid password, because "?" is not an alphanumerical character and "test" contains an even number of digits (zero).
// Write a function:
// class Solution { public int solution(String S); }
// that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.
// For example, given S = "test 5 a0A pass007 ?xy1", your function should return 7, as explained above.
// Assume that:
// N is an integer within the range [1..200];
// string S consists only of printable ASCII characters and spaces.
// In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
public func solution(_ S: inout String) -> Int {
let passwords = S.components(separatedBy: " ").sorted { (l, r) -> Bool in l.count > r.count }
let letters = CharacterSet.letters
let digits = CharacterSet.decimalDigits
for password in passwords {
var lettersCount = 0
var digitsCount = 0
for c in password.unicodeScalars {
if letters.contains(c) {
lettersCount += 1
} else if digits.contains(c) {
digitsCount += 1
} else {
break
}
}
if lettersCount + digitsCount == password.count {
if lettersCount % 2 == 0 && digitsCount % 2 == 1 { return password.count }
}
}
return -1
}