Task description
Given a string, write a function that accepts this string as input and returns compressed string. Algorithm of compression is following: every sequence of the same character should be replaced with that character followed by the number of repetitions. If the compressed string is longer than the original string, then function should return the original string.
Algorithm for this challenge also known as Run Length Encoding.
Examples of input and output
- a becomes a (because a1 is not shorter than a)
- aa becomes aa (because a2 is not shorter than aa)
- aaa becomes a3
- abc becomes abc (because a1b1c1 is not shorter than abc)
- aabbbdcccc becomes a2b3d1c4
- aaabbbccc becomes a3b3c3
- aabbbcccdeee becomes a2b3c3d1e3
Swift Implementation Prerequisites
In Swift we cannot get character of string by it's integer index. If we try to do this we will have an error.
'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion
For getting character we must calculate "String.Index" value first.
For simplifying this we can make subscript method for String type and get character by index.
Algorithm
- Create variables
- result for compressed string
- sum for counting each character
- For loop from 0 to n-2 (where n = length of string, n - 2 because we will compare current and next characters)
- Compare current and next characters
- If they are equal add 1 to sum
- Else append current character and sum to result and reset sum to 0
- Append to result last character and sum (because in for loop we update result only in case of characters are changed).
- Compare lengths of initial and compressed strings and return one with less length.
Swift Implementation
We can improve algorithm
- Initially assign 0 to sum
- For loop from from 0 to n-1
- Increase sum by 1 in each iteration
- In each iteration check if it is the end of string or current and next characters are different. If Condition is true than append current character and sum to result and reset sum to 0.
More "Swifty" Implementation
But there could be more "swifty" solution without using string index. We can use for each loop for string characters.
Algorithm
- Create three variables:
- currentCharacter - optional variable for current character
- sum - counter for for each character
- result - compressed string
- For loop for each character in string
- If character is equal to currentCharacter then add 1 to sum
- Else do following
- Check that currentCharacter has value and if it has then add this value and sum to result
- Save character into currentCharacter
- Reset sum to 1
- After loop finished, append currentCharacter and sum to the result string. (This step can be easily forgotten, because in for loop we only modify result string in case of character changes.)
- Compare lengths of initial and compressed strings and return one with less length.
Complexity
Because we have only one for loop - space complexity of this algorithm will be linear - O(n).
Source Code
Source code of from this post could be found here on GitHub
Thank you so much for sharing your articles with us. Hopefully, you will be able to benefit us with more informative article.
ReplyDelete-Custom Website Design
Thank you very much for bringing this to my attention. Your blog is chock-full of useful information. I'd have no idea unless I read that. I'll return for more excellent content. Best wishes and best of success to you. Best Custom Websites
ReplyDeleteThank you for providing me with such unique content.
ReplyDeleteYou must provide such type of content constantly. I used to read your content regularly.
Custom Website Building
Thank you Sir, you made it easy as pie. Now i am able to understand and have enough knowledge about this. It is only because of you.
ReplyDeleteCustom Designed Websites
Thank you so much for sharing your articles with us. Hopefully, you will be able to benefit us with more informative article.
ReplyDeleteMobile Performance Meter Hack
I adore your work, and it greatly motivates me.
ReplyDelete<a href="https://www.iptvfilms.com/things-to-think-about-before-hiring-a-sem-agency/> SEM Agency</a>
I adore your work, and it greatly motivates me.
ReplyDeleteMobile App Testing Services
This is one of the nicest blogs I've ever seen, and it's really
ReplyDeletepleasant. This is a very valuable blog for me, and one of the most
helpful blogs I've ever seen.
Search Engine Marketing Services