-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repeated String.swift
48 lines (37 loc) · 1.06 KB
/
Repeated String.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
import Foundation
// Complete the repeatedString function below.
func repeatedString(s: String, n: Int) -> Int {
var count = 0
if(n>=s.count){
var leftover = n%(n/s.count)
var iterator = 0
var extra = 0
for c in s{
iterator += 1
if(c == "a"){
count += 1
if(iterator <= leftover){
extra += 1
}
}
}
count = count*(n/s.count) + extra
}
else{
for c in s[s.startIndex..<s.index(s.startIndex, offsetBy: n)]{
if(c == "a"){
count += 1
}
}
}
return count
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let s = readLine() else { fatalError("Bad input") }
guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
let result = repeatedString(s: s, n: n)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)