Compress String

Question A simple compression algorithm would be to replace repeating characters with their count. Example aaa --> a3 aaabb --> a3b2 aaabbaa --> a3b2a2 Want to stand out? Ask if thereโ€™s a difference between a & A and if you need to add uppercase & lowercase together or need to separate them. In my implementation I assumed theyโ€™re different. Code // start counting, then upon seeing a diff, write the count. func compress(_ str: String) -> String { let chars = Array(str) guard !chars.isEmpty else { return str } var currentChar = chars.first! var currentCount = 1 var ans: String = "" guard chars.count > 1 else { return "\(currentChar)\(currentCount)"} for i in 1..<chars.count { if chars[i] == currentChar { // increment currentCount += 1 } else { // append subsection ans.append("\(currentChar)\(currentCount)") // reset currentChar = chars[i] currentCount = 1 } } ans.append("\(currentChar)\(currentCount)") return ans } Tests print(compress("aaabbbbccaA")) // a3b4c2a1A1 print(compress("a")) // a1 print(compress("")) //

Check if Two Strings Are One Edit Away

Question You can edit a string in three different ways: insert, remove or replace a character. Write a function to see if two strings are one or zero edits away. Example like, like --> true (zero edits) like, likes --> true (one edit: remove/insert) like, life --> true (one edit, replace) like, lik --> true (one edit: remove/insert) like, pine --> false (two edits) like, lion --> false (two edits) Strategy For most questions that require you to return true or false, you can reduce the scope of the question by removing noise. Often that means exiting early or having conditions that make it impossible to have a true resultโ€ฆ Example: If the string lengths differ by 2 or more characters, then you can exit / return an answer. Try not to solve this problem in one go. Break it down into two checks: ...