Hey everybody, Leo here. Today we’ll continue the series Algorithms, the Final Frontier. Always remember, this is ONE of many possible solutions to the problem. We will this a special palindrome problem and I hope you all like it.

We all know that we need some time to practice algorithms. It doesn’t come for free in your life, it takes a lot of practice to be good. Some people say that you need 10000 hours to master any skill, this way do not be disappointed with yourself if sometimes you don’t get from the first time. Continue studying and that will pay off later.

Studying algorithms is very important for anyone in an iOS development career. Being able to identify the pros and cons of something that you are writing can be the difference between having a smooth user experience or the worst one.

Let’s go!

The Problem – Special Palindrome

Given a string, find the longest substring which is a palindrome, and return its length. The two words must have the same ending letter. Ex: “LeoeL” should return 5 but “LeooeL” should return 0.

So here the solution a came was to manipulate 2 pointers inside a string. Check the solution above and you can copy/paste it into your playgrounds to play around with.

 

If you're a mid/senior iOS developer looking to improve your skills and salary level, join this 100% free online crash course. It's available only until April 28th, so click to get it now!

//test data
let return7 = "rooooor" // return 7
let return5 = "leoel" // should return 5
let return3 = "adfwefvcsadrooo" //should return 3
let return3again = "oooadfwefvcsadroooo" // should return 3
let return4 = "oooadfwefvcsadrooooo" //should return 5

func palindrome(_ word: String) -> Int {

if word.count < 3 {
   return 0
}

var tempResult = 0
var maxResult = 0
let array = Array(word)
let arrayCount = array.count

for x in 1..<arrayCount-1 {
   for y in 1...arrayCount {
   if !(x-y < 0) && !(x+y > arrayCount-1) {
      if array[x-y] == array[x+y] {
         if tempResult == 0 {
            tempResult += 3
         } else {
            tempResult += 2
         }
      } else {
      break
   }
} else {
   break
}
}
if tempResult > maxResult {
   maxResult = tempResult
}
   tempResult = 0
}
   return maxResult
}

//testing
palindrome(return5)
palindrome(return7)
palindrome(return3)
palindrome(return3again)
palindrome(return4)

And that’s it!

 

Algorithms: Continue Studying

There are three main algorithms to learn about linked lists, one of which is merging linked lists. This is so fundamental to solving linked lists problems that you will be surprised how many challenges you can tackle just knowing this pattern.

Working with arrays is a very important skill to have in an iOS career. It is impressive how many ways you can approach an array problem and when you try to solve the square of a sorted array problem you will have the exact same feeling.

Trivia time! History of the Palindrome

The word palindrome is derived from the Greek ‘Palin,’ or “back” and ‘dromos’ or “direction.” The actual Greek phrase alluded to the backward movement of the crab. Palindromes date back to about 70AD when they were first found as a graffito buried in ash at Herculaneum.

Summary

That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or just leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing! You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.

Thanks for the reading and… That’s all folks.

Credits: image