Skip to content

Commit

Permalink
[String] Refactor solution to Longest Substring with At Most K Distin…
Browse files Browse the repository at this point in the history
…ct Characters
  • Loading branch information
soapyigu committed Jan 4, 2020
1 parent 45126d3 commit 301c6df
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions String/LongestSubstringMostKDistinctCharacters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@

class LongestSubstringMostKDistinctCharacters {
func lengthOfLongestSubstringKDistinct(_ s: String, _ k: Int) -> Int {
var start = 0, longest = 0, charsFreq = [Character: Int]()
let sChars = Array(s)

guard k > 0 else {
return longest
return 0
}

for (i, char) in sChars.enumerated() {
let s = Array(s)
var start = 0, longest = 0, charsFreq = [Character: Int]()

for (i, char) in s.enumerated() {
if let freq = charsFreq[char] {
charsFreq[char] = freq + 1
} else {
if charsFreq.count == k {
longest = max(longest, i - start)
while charsFreq.count == k {
longest = max(i - start, longest)

while charsFreq.count == k {
let charStart = sChars[start]
charsFreq[charStart]! -= 1

if charsFreq[charStart] == 0 {
charsFreq[charStart] = nil
}

start += 1
guard let freq = charsFreq[s[start]] else {
fatalError()
}
charsFreq[s[start]] = freq == 1 ? nil : freq - 1

start += 1
}

charsFreq[char] = 1
Expand Down

0 comments on commit 301c6df

Please sign in to comment.