Skip to content

Instantly share code, notes, and snippets.

@abin0992
Last active August 26, 2018 23:19
Show Gist options
  • Save abin0992/2487928e6c4f9ba2f88604a47a63d27c to your computer and use it in GitHub Desktop.
Save abin0992/2487928e6c4f9ba2f88604a47a63d27c to your computer and use it in GitHub Desktop.
Code to compress a string in Swift. Example : AAABBACC to A3B2AC2
// String to compress
let stringtoCompress = "AAABBACC"
// Empty string to store compressed value
var compressedString = ""
// Store the letter count to append in compressed string
var letterCountStored:Int
//Initially letterCountStored is set to 0
letterCountStored = 0
// function the compress given string
func compressString(string2Compress: String) ->(String) {
// get string length
let letterCount = string2Compress.count
// convert to int
let letterCountInt = Int(letterCount)
// check if string has more than 1 letter
if letterCountInt == 1 {
compressedString = string2Compress
} else {
//reduce 1 from characters count
let stringlenth = letterCountInt-1
//check all letters in string
for i in 0 ..< stringlenth {
// fetching first letter to compare
let index = string2Compress.index(string2Compress.startIndex,offsetBy:i)
let firstLetterToCompare = string2Compress[index]
// fetching second letter to compare
let secondIndex = string2Compress.index(string2Compress.startIndex,offsetBy:i+1)
let secondLetterToCompare = string2Compress[secondIndex]
//pass the above 2 letters to function to compare
compareTwoConsecutiveLetters(firstLetter: firstLetterToCompare, secondLetter: secondLetterToCompare)
//Check if comparing the last two letters
if i == stringlenth - 1 {
// append last letter count to compressed string
appendLetterCount()
}
}
}
// return compressed string
return compressedString
}
// function to compare two consecutive letters
func compareTwoConsecutiveLetters(firstLetter : Character, secondLetter : Character){
// chcek if letters are same
if firstLetter == secondLetter {
// add one to the letter count ie 0 to 1
letterCountStored += 1
//check if string is empty
if compressedString.isEmpty == true {
//add first letter to compressed string
compressedString = compressedString + String(firstLetter)
// since first 2 letters are same add one to the letter count ie 1 to 2
letterCountStored += 1
}
}
else {
// check if compressed string is empty
if compressedString.isEmpty == true {
// add one to the letter count
letterCountStored += 1
// append first letter to compressed string
compressedString = compressedString + String(firstLetter)
// append first letter count to compressed string
appendLetterCount()
// append second letter to compressed string
compressedString = compressedString + String(secondLetter)
// reset the letter count
letterCountStored = 1
}
// if compressed string alredy has first letter
else if compressedString.count==1 {
// add one to the letter count
// letterCountStored += 1
/// append first letter count to compressed string
appendLetterCount()
// append second letter to compressed string
compressedString = compressedString + String(secondLetter)
// reset the letter count
letterCountStored = 1
}
else {
// append first letter count to compressed string
appendLetterCount()
// append second letter to compressed string
compressedString = compressedString + String(secondLetter)
// reset the letter count
letterCountStored = 1
}
}
}
// function to append letter count
func appendLetterCount(){
// check if letter occurs more than once
if letterCountStored > 1 {
//append to compressed string
compressedString = compressedString + String(letterCountStored)
}
}
//pass the string to compress to compressString function
var compressedStringOutput = compressString(string2Compress: stringtoCompress)
print(compressedStringOutput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment