You are viewing a single comment's thread. Return to all comments →
In Swift Really too much complicated problem
func reverseShuffleMerge(s: String) -> String { var counts = [Int](repeating: 0, count: 26) var remChars = counts var charsUsed = [Int](repeating: 0, count: 26) var result: [Character] = [] for char in s { counts[Int(char.asciiValue! - Character("a").asciiValue!)] += 1 } remChars = counts counts = counts.map { $0 / 2 } let sArray = Array(s) for char in sArray.reversed() { let charValue = Int(char.asciiValue! - Character("a").asciiValue!) if counts[charValue] - charsUsed[charValue] > 0 { while !result.isEmpty && result.last! > char && charsUsed[Int(result.last!.asciiValue! - Character("a").asciiValue!)] + remChars[Int(result.last!.asciiValue! - Character("a").asciiValue!)] - 1 >= counts[Int(result.last!.asciiValue! - Character("a").asciiValue!)] { charsUsed[Int(result.last!.asciiValue! - Character("a").asciiValue!)] -= 1 result.removeLast() } charsUsed[charValue] += 1 result.append(char) } remChars[charValue] -= 1 } return String(result)
}
Seems like cookies are disabled on this browser, please enable them to open this website
Reverse Shuffle Merge
You are viewing a single comment's thread. Return to all comments →
In Swift Really too much complicated problem
}