Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
unique substrings
import Cocoa
func fewestCoins(coins: String) -> Int {
let coinsArray = Array(coins)
let checkSet :Set = Set(coinsArray)
let countedSet :NSCountedSet = NSCountedSet()
// validation when the lenght of the string == amount uniq characters
if(checkSet.count == coins.count || checkSet.count == 0) {
return coins.count
}
// brute force with sliding window
var left = 0, right = 0, smallResult = coins.count
while (right < coinsArray.count || left < coinsArray.count ) {
if (countedSet.count == checkSet.count) {
smallResult = min(smallResult, right - left)
left += 1
countedSet.remove(coinsArray[left])
} else if right < coinsArray.count {
countedSet.add(coinsArray[right])
right += 1
} else {
countedSet.remove(coinsArray[left])
left += 1
}
}
return Int(smallResult)
}
fewestCoins(coins: "asdfkjeghfalawefhaef")
fewestCoins(coins: "bab")
fewestCoins(coins: "cbbbdcbba")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment