You are viewing a single comment's thread. Return to all comments →
Same idea in Swift times out in some of the test cases. Any optimization suggestions?
var t = Int(readLine()!)! for _ in (0..<t) { var strArr = readLine()!.characters.map{String($0)} if strArr.count % 2 != 0 { print("-1") } else { var middle = strArr.count / 2 var map = [String : Int]() for i in (0..<middle) { if map[strArr[i]] == nil { map[strArr[i]] = 1 } else { map[strArr[i]] = map[strArr[i]]! + 1 } if map[strArr[middle + i]] == nil { map[strArr[middle + i]] = -1 } else { map[strArr[middle + i]] = map[strArr[middle + i]]! - 1 } } var res = 0 for (key, value) in map { res = res + abs(value) } print(String(res/2)) } }
Seems like cookies are disabled on this browser, please enable them to open this website
Anagram
You are viewing a single comment's thread. Return to all comments →
Same idea in Swift times out in some of the test cases. Any optimization suggestions?