Swift: Fun Algorithms - Counting Palindromes

preview_player
Показать описание
We get all kinds of fancy questions tossed at us during coding interviews. In today's lesson, I'll go over one of the more common algorithm challenges in detecting palindromes. For those that are wondering, a palindrome is a word that you can spell frontwards and backwards using the same character sequence. To make this problem a little more challenging, let's also count the occurrences of palindromes in a sentence. Enjoy.

Instagram Firebase Course

Facebook Group

iOS Basic Training Course

Completed Source Code

Рекомендации по теме
Комментарии
Автор

Hey Brian, nice videos!
As an optimization for the algorithm, you can check first if the string exists in the dictionary, if it does, you know it is a palindrome and you don't need to call the isPalindrome function.

luisguitar
Автор

Wow this was very informative and fun to watch ! Talking your process out loud helps a lot with following along and comprehending the concept

jordantaylor
Автор

func findPallinDromes(sentence: String) -> Int {
return sentence.split{$0==" ==
}

Dj_Realest
Автор

Great video! Algorithm lectures are back! It is good to learn string handling in Swift. Thank you!

jincat
Автор

Very Simple neat clean powerful tutorial ! LOVED it !!

tariqAlmazyad
Автор

Loved this. I've never heard of Swift before until now though.

theoneofeight
Автор

I am trying to move to swift from objective C. I appreciate every swift programming video that you do. Thanks to you, I just learned a new language feature today. "??" to set a default value. I would have used an unnecessary "if let".

isaacclark
Автор

I will back to review videos that I missed it included this video. Anyway I really appreciated it thank you Brain

lyheangibell
Автор

I'm pretty sure "anna kayak" is also a palindrome if I "count the occurrences of palindroms in a sentence." :-) Which means there are a pile more, per that non-clarified requirement. Could be the next step in the exercise...

Keep 'em coming.

jlangr
Автор

can you make a video that teaches us how to record, playback, and save video with a custom camera? thanks

JordanCT
Автор

Loving this!!
I noticed that this works only in Swift 4.0: Let characters = Array(word).

In swift 3.0, an error msg shows: Argument type 'String' does not conform to expected type 'Sequence'.

So even trying these 3 alternatives in Swift 3.0 or 4.0 did not work.
1)
let characters: Array = [word]
OR
2)
var characters = [String]()
characters.append(word)
OR
3)
let characters: [String] = [word]

Nevertheless, loving the logic you implemented without relying on the reverse function. Thanks Brian!

JustVictor
Автор

These are great videos, thanks! Did anyone else end up creating their own reverse function, I was pretty pleased with this one. It allowed me to just say if word == reverseString(word)

func reverseString(_ word: String) -> String {
var newWord = ""
var tempWord = word
for i in word.characters.indices {
newWord.characters.insert(tempWord.characters.last!, at: i)

}
return newWord
}

bensullivan
Автор

Hey! Great Clip. IMO [ words.characters.reversed() == Array(words.characters) ] in Swift3 is more simple for checking palindromes.

coffeetoast
Автор

Pali!
Fun fact: Palindrome is actually the name of the most popular weapon(pistol) in the console video game Destiny. !!

RuhkcusTV
Автор

Thanks for your clear and precise explanation.

I think your isPalindrome method can be improved by taking the "characters.count" before the while loop and assigning it to a variable. Because the length of the word does not change every iteration and your code would look neater.

muhsinzahidugur
Автор

Nice, I really like your algorithm videos. I'm wondering if I could use swift 4 during an interview? It would be more difficult (more time consumption) to try to implement the separatedByWhitespace( ) function in some other languages like c++

jackgong
Автор

Hey Brian, great video as always. A quick suggestion, these kind of exercises would work much better and faster in Playground just so you don't have to build all the time.

DeaconuDanAndrei
Автор

You brian! Can you make a sneak peak video of your next paid course?

ParthAnand
Автор

great tutorial thank you sir

one question
should i use firebase for document, image sharing app .
and those file will be visible to all users.

starslife
Автор

very good video! Do you recommend Swift, Python or Java for interview if a person is comfortable with all 3?

Joanna