String manipulation with swift

I spent few hours this weekend playing around with Swift. A few weeks ago I implemented a simple integer BST and it was so much fun, I decided to do something new this weekend. I’m still new to the language and figured the best way to get better is to continue implementing classic algorithms. In my opinion, the easiest non-trivial problems to solve while learning a new language are string manipulations. They tend to show up frequently in technical interviews because they are universal across platforms, easy to explain and require minimal knowledge of a language to solve.

I tried not to use any of the built in libraries or frameworks and dig deep with the syntax and patterns of the language.

After a few hours I managed to implement the following

  • Reverse characters in a string
  • Reverse characters in a string without using extra space (reverse in place)
  • Reverse words in a string
  • Reverse words in a string without using extra space (reverse in place)
  • Reverse words in a string where words are separated by a specific character
  • Is a string a palindrome
  • Are two string anagrams
  • Are two string anagrams using sort
  • Return an array of all duplicated characters
  • Return an array of all characters repeated n times
  • Return all unique characters
  • Return first non repeated character in a string
  • Count number of occurrences of a character in a string
  • Count number of occurrences of a character in a string using higher functions (filter)
  • Return all permutations of a string (not an atomic function)
  • Is a string a substring of another (naive implementation)
  • Return the most common character
  • Remove a specific character from string
  • Remove a specific character from a string using higher functions (filter)
  • Remove a specific character from a string in place
  • Return longest palindrome in string
  • Rotate string left by n characters
  • Rotate string right by n characters


Swift is beautiful, simple and easy to pick up. If you’re interested in the project, the repo is here: Swift Algorithms Strings. I suggest implementing some of these for yourself.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s