IQ 41: Reverse a String



hey guys welcome back I got another coding interview assignment for you and this assignment is actually title reversing a string so what I'm gonna be doing in this video is talking about how to go about reversing a string pretty much so I'm actually gonna be showing you guys two ways of reversing a string and those two ways are reversing a string none recursively and recursively and for those of you who do not know what recursive or recursion is I'm gonna be explaining this on the next slide here so as you can see here we have definitions of non recursive and recursive so non recursion or none recursive means that you're not invoking a method call upon itself so what this basically means is that let's just say you're inside of a method and inside of this method you are not going to be calling the method that you are inside us so you're not invoking a method call upon itself this basically just means that you're coming up with your own logic in order to reach the desired goal of that application whereas recursive or recursion is the exact opposite it means that you're actually invoking a method call upon itself so for example if you're inside of a method and you call or invoke the method that you're inside of that would be what recursion is so it's pretty straightforward if you have any questions about this make sure to leave them in the comments below but when I get to the coding part of this video should make a whole lot more sense but until then let's talk about the actual assignment now the actual assignment is to actually write code that recursively and none recursively reverses a string so just imagine that you have this string here hello okay this is going to be taking it going to be taken in by our method or our application and basically we want this result we want this string to be returned in reverse order should be pretty simple so what I'm gonna do now is actually go about walking you through the entire process and as I said before I'm gonna be showing you how to reverse a string recursively and recursively so let's get right to the code alright guys as you can see here I've already created myself a project as you can see I have this app that Java class I've already created my application object and then here we have our main method and what I'm gonna be doing here is showing you how to reverse a string non recursively and then after that I'm gonna show you how to reverse a string recursively now before I start let me zoom this in for you guys okay so let's get right into it the first thing that we're gonna be doing is writing a method that reverses a string none recursively so let's do that right now public static string none recursive reverse alright this is our method and this method is actually going to take in a parameter we're gonna go string word and as I said I'm going to be showing you how to implement the functionality for reversing a string non recursively so the first thing that we want to do is actually create ourselves a string called reverse string and this string is actually going to be the string that's going to be returned after we've completed the reversal and down here we just want to return reverse string because this is the string that we're altima lis gonna be returning and basically what I'm gonna do now is right after I create this reverse string it's gonna be an empty string I'm going to write myself a reverse for loop it's pretty similar to a general for loop but what we're doing is slightly different so we're not entering through through elements in a natural order we're actually doing it in a reverse order and I'm gonna show you how to write that loop right now so we're gonna go for it I equals and then word got less minus 1 I is greater than or equal to 0 and then I – – okay so what I'm gonna do is actually just explain what's going on in this loop so in this loop the first thing that we have is this eye and this eye is actually starting at the end there's word that lands minus 1 we're starting at the end of the word and we're pretty much just going to be working our way backwards as you can see I has to be greater than or equal to 0 and then I – – is here so this loop is basically saying that we're going to be iterating through this particular word in reverse order alright so next thing we want to do is to actually go inside of the for loop and then write reverse string plus equals word dot char at position I and this is pretty much it as far as the function goes now in order to see our results I'm gonna have to actually write me a system dot out dot print up here in this main method but to just go over a little bit of what's going on in this non recursive reverse method as you can see here I first start off with a reverse string which is empty and then what I'm doing is creating myself a for loop and this for loop is going to iterate backwards through this word that we're passing in after that i'm concatenating this i'm concatenated this to my reverse string this reverse notch our position i like in the character and i'm concatenating it to this empty string upon each iteration and once it reaches the end of the loop is simply going to return the reverse string after this reverse string has been properly populated with characters of this string in reverse order alright so let's go ahead and print this out sis out and then we're gonna go 9 well let's actually do this let's create ourselves a string first string word equals hello and then nine recursive reverse and in word and this is pretty much it as you can see we're simply just going to print out this method so this method is going to return a reverse string we're passing in a word which is hello and when we run the application we'll see what we get go down as you can see here in the console we're getting the word hello in reverse order oh come first followed by the two else and then the E and then the capital H so this string is being printed out in reverse order but remember that this is done none recursively so we're not invoking a method call upon itself but now that's what we're going to be doing in this next method that I'm gonna put together right now alright so now we're going to be doing the same thing but we're gonna be doing it recursively so let's start off by creating a method public static string recursive reverse and this is this method is going to be somewhat similar to the method that we just created we're gonna take in a word string word and in order to do this recursively the first thing that we need to do is have ourselves a base case now a base case is basically something that always holds true basically a conditional that always holds true or a case that always holds true so the first thing you have to do when trying to figure out what a base case you need to write would be to look at this word and try to figure out what's something that's always going to be true when you pass in this word so basically the user has the ability to enter in whatever word that they want or whatever text that they want so let's just say the user enters in a single letter so the user enters in a single letter the length of the word it's pretty much going to be 1 so what we want to do is write a conditional statement that says if the word not less is lesser than 2 they don't want to return the word because there's no way that you can reverse a single character if the user simply enters into letter A there's no way to reverse that so what's going to be printed out is that character so we want to go if word got less is lesser you too then you simply want to return word and that is our base case it's something that is always gonna hold true no matter what and let me get a little bit more space ok now what we want to do next is actually start the recursive call so what's gonna happen is I want to return and what I want to do now is to try to figure out what do I need to write in order to reverse the elements within this string okay so in order to reverse the elements in this string the first thing that should come to the top of your head is going to be grabbing the first element of the string alright so in order to do that we would just go word that char at 0 okay so this will be the first letter of the string so as you can see if we were to pass in the word hello word dot char at this would be equivalent to H all right so what we want to do is append this H to the end of the new word that we're going to be creating so what we need to do after this or actually before this is to go word dot substring okay so let's just say okay if I go word dot substring what am I gonna get here now for those of you who don't know what substring is or this function is it basically is gonna take the word at this position so whatever position that the word is in it's going to print out that character of the word as well as all the characters after this particular and of the word and for those of you who just want to see what substring is and what it looks like I'm going to show you an example of this right now so that make sure everything that you see here is that you're learning here is clear so we just want to go sis out and let's just say hello dot sub substring one just know we get okay go up here we run this you go down to the console we're gonna get e ll o alright so e ll o and then over here we're gonna get H alright so as you can see we're pretty much just returning e ll o and they were getting H now what we need to do is append the first character of this word repeatedly so this recursive call what we need to do is add some type of repetition or some type of iteration just like we did up here in our non recursive reverse with this loop so in order to perform that iteration what we need to do is actually invoke a method call upon itself so we have a recursive reverse and what we want to do is just call this method down here in this return recur the cursive reverse all right so all right so what's happening here what we call this recursive reverse what's happening is that we're going to be grabbing the word not substring at position 1 okay and as you can see the substring at position 1 on the next iteration it's pretty much going to be it's pretty yes so the first your first iteration is you're gonna get an H and then we're gonna get an e llo but we're passing this into this recursive reverse all right and what's going to happen is that on the next iteration that word that character at position 0 which is going to be e is going to be placed here next ok what's gonna be placed here since we're concatenating that and it's basically going to be removed from here and then on the next iteration or the next recursive call we're gonna get L here and L is going to be removed from here and then after that we get another L because that will currently be the word that chart at position 0 and this is going to be moved from here and then afterwards we're going to get out and then this o is gonna be removed and that's pretty much going to be it you know so that's how you would go about writing a function that reverses a string recursively so in order to see this in action let's go to the top and actually call this method recursive reverse copy now what we want to do is actually remove all this and do this okay as you can see here we're passing in the word hello we're passing it into this recursive reverse in which were invoking the method call upon itself and we simply just want to run the application see what we get as you can see here we get hello in reverse order oh it's first followed by the two else and then the e and then a capital H and that's pretty much it now if you guys have any questions about recursion or this recursive reverse function make sure to let me know in the comments below and I'll make sure to respond to your comments as soon as possible and that's pretty much it you know I showed you pretty much how to reverse a strain none recursively and recursively so hopefully you guys learned something here you have any questions leave them in the comments below don't forget to like the video if you've learned something from here also don't forget to subscribe and see you the next one

One Comment

  1. nevas sridhar said:

    3 likes for this brilliant work and millions of likes for song albums is the reason why i hate humans

    June 28, 2019
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *