Strings & Palindromes | Recursion Series

WilliamFiset · Intermediate ·⚡ Algorithms & Data Structures ·3y ago

Key Takeaways

The video demonstrates how to reverse a string using recursion and check if a string is a palindrome using the 'outside-in' method, covering two main problems: string reversal and palindrome identification.

Full Transcript

foreign [Music] welcome back I'm William and this is a continuation of the recursion Series today I want to begin looking at some string manipulation problems since recursion isn't all about just numbers we use strings all the time in programming and it's essential that we also know how to manage them when using recursion I want to start off by looking at a very simple problem you probably already know how to solve reversing a string so that it reads backwards for example the string three two one would read as one two three and ABC as CBA if you were to implement this problem iteratively the code would probably look something like this you would start by initializing an empty string and then iterate through the characters of the original string backwards adding each character to the new string as you go along and return the result however if you wanted to achieve the same thing recursively how would we go about it well here's one way we could do it our approach would involve using the same strategy of building the string backwards except that we would reverse the string character by character in the return statement notice that on the last line we add the character to the current string position after the recursive call to reverse the string what happens if we invert to the return statement so that the current character is before the recursive call to the reverse function I'll give you a moment to think about that the answer is that that would make a copy of the string instead of reversing the string since we would be constructing the string in order instead of in reverse order we just looked at probably the simplest way to reverse a string however there's another method we can use to reverse a string by reversing it outside in rather than linearly let's take the string Dragon for instance if we take the front and the back characters of the string then we can swap them once you have swapped the outer parts of the string the only remaining portion that has not yet been reversed is the intersection highlighted in blue we can then recursively apply to the same process to that intersection of the string we pick out the first and last characters swap their positions identify the remaining portion of the string that hasn't yet been reversed and recursively apply the same process eventually there are no more characters to swap so the recursion would unwind and we would construct the Reversed string and that's another way to reverse a string using the outside in method this is the pseudocode for the outside in method let's take a short walk through it in the base case we verify if the String's length is less than or equal to one in which case we would return the current string this base case handles the scenarios where the remaining string has a length of zero or one if the remaining string is empty which occurs in even length strings we return an empty string however if there is one character left after reducing the string using the outside inner approach we return that character since it represents the middle character of the string and the middle character is the reverse of itself so if we're not in a base case situation we want to swap the lactomose and the rightmost characters of the string the highlighted line gets the left and the right characters from the string s in the following line we extract a substring of the inner string so that we can pass it down to the reverse function we specify that the substring should start at index one and end at index n minus 1 to exclude the first and the last characters if you were to visualize the variable assignments from the pseudo code left corresponds to the leftmost character substring to the inner blue segment and right to the rightmost character of the string afterwards we would recursively call the reverse function swapping the left and the right characters and the return statement as we build the string and that's pretty much how we would reverse the string using the outside in method however just one question for you guys what happens if we update the code to check if the length of the string is zero instead of less than or equal to one will this still reverse the string as intended I'll give you a moment to think about it the answer is that the reverse function will continue to work for even length strings but will fail to produce the right answer for odd length strings what happens is that the middle character would get duplicated if we don't handle the N equals one case since both the left and the right variables would point to the same character all right we just finished looking at how to reverse a string recursively now let's shift our attention to something slightly different but related which is the problem of identifying whether a string is a palindrome or not palindromic strings are strings which read the same backwards and forwards meaning you can reverse the string and you get the original string you started with for example the word Rotator is read the same whether you read it forwards or backwards however a string like raccoon is not a palindrome since reading the string backwards doesn't match the original string and one more example the string of numbers one two three four three two one is a palindrome because it reads the same backwards and forwards if you've been paying attention you may have noticed that we can easily create a function to check if a string is a palindrome or Not by reusing the previously implemented reverse function simply check whether the string is equal to the reverse of itself now I have a challenge for you guys to implement the ispondrome function but by using the outside method we discussed previously to check if the string is a palindrome So Below I have the is palindrome method with some simple examples of whether or not the following strings should be considered a palindrome or not I'm going to reveal the solution in the next slide so be sure to stop the video and give this challenge a try I'm going to give you a short moment all right I hope you guys took the time to try out that mini challenge the way we're going to verify if the string is a pound drum or not is by working outside in we're going to check if the first and the last characters of the string match and recursively apply this technique to the inner substring if at any point the first and the last characters don't match we can stop the recursion and return false to indicate that the string is not a palindrome first we check if the outermost characters match after verifying that they do recursively check if the inner string is also a palindrome check that the first and the last characters match then repeat the same process once we reach the end we know that this string is indeed a palindrome and can return true here's some pseudocodone how you would check that the string is a pound drone using the outside in method we just looked at first we check our base case to see if the length of the string is less than or equal to one this indicates that the recursion has come to an end and that the string is now either a single character or an empty string both of which are trivially palindromes then we check if the first and the last characters match if they do then recursively call the ispoundra method to check it if the inner substring not including the first and the last characters is also a palindrome otherwise if the first and the last characters do not match immediately return false and end the recursion awesome that's all I have for you guys on strings and palindromes for now thank you for watching please like this video And subscribe if you learned something and I'll catch you in the next one

Original Description

In this video we explore how to reverse a string using recursion and check whether or not a string is a palindrome using the 'outside-in' method. Source code repository: https://github.com/williamfiset/algorithms Video slides: https://github.com/williamfiset/algorithms/tree/master/slides
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 0 of 60

← Previous Next →
1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

This video teaches how to reverse a string and check if it's a palindrome using recursion, covering the outside-in method for both problems.

Key Takeaways
  1. Initialize an empty string to store the reversed string
  2. Use recursion to reverse the string character by character
  3. Apply the outside-in method to reverse a string
  4. Check if a string is a palindrome by comparing it to its reverse
  5. Use the outside-in method to check if a string is a palindrome
💡 The outside-in method can be used to reverse a string and check if it's a palindrome, providing an alternative approach to traditional iterative methods.

Related Reads

Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →