Created
March 2, 2021 10:28
-
-
Save vitorventurin/5b674b92aa9f9a93338759489561f3a5 to your computer and use it in GitHub Desktop.
Hackerrank - Repeated String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func getLetterCount(s: String) -> Int { | |
return s.filter { $0 == "a" }.count | |
} | |
func repeatedString(s: String, n: Int) -> Int { | |
let stringCount = s.count | |
let remainder = n % stringCount | |
let multiplier = n / stringCount | |
let prefix = String(s.prefix(remainder)) | |
let patternPrefix = getLetterCount(s: prefix) | |
let aValue = getLetterCount(s: s) * multiplier | |
return aValue + patternPrefix | |
} | |
repeatedString(s: "abcac", n: 10) // expected 4 | |
repeatedString(s: "aba", n: 10) // expected 7 | |
repeatedString(s: "gfcaaaecbg", n: 547602) // expected 164280 | |
repeatedString(s: "aab", n: 882787) // expected 588525 | |
repeatedString(s: "ceebbcb", n: 817723) // expected 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment