@@RoadsideCoder I did it as shown below, but your method always rocks. Thanks! let str = "Hello, World!"; let reversedString = str.split('').reverse().join(''); console.log(reversedString);
Hello bro..with this kind of speed..you missed to show us, adding of closed bracket after substr...at start you havn't added. later, on adding the base case it was there..
It's pointless to use all these methods. Readalibilty is poor. Just run a for loop in reverse. let newStr= ' '; for (let i = str.length -1; i>=0;i--){ newStr += str[i]; } return newStr; Very simple way. Why trying all this?
Your algo: TC O(n), SC O(n) Algo in video: TC O(n) SC O(1) You just increased space complexity to O(n) by using a new string. The mentioned way in video has space complexity O(1) and time complexity O(n)
@@paritoshchauhan3266no the recursive solution is not of space complexity O(1) its also O(n) because for a string of length n there will be n recursive calls to the function which all individually add a layer to the call stack which uses up memory
This is overcomplicating something very simple. Even if you had 10mil characters in one string and passed it into this function it would make such a minimal difference
return str.split("").reverse().join("")
It may be more optimized but higher JS payload size compared to the array approach.
Love you mere bhaii❤
Use 2 pointer and swap like a pro 😎
Recursive functions is never considered as optimised approach
Why is that?? Recursion is a great way of solving problems with recurring sub-problem
@@paritoshchauhan3266 I guess time or space complexity might be the reason
It may cause stack overflow
We can convert string to array then reverse it and then again convert it into string like following str.split("").reverse().join("")
but it wont be optimised
@@RoadsideCoderyou're writing in javascript, if you want things to be optimised code in a real language
@@RoadsideCoder idiot take a loop and reverse for i = string.length-1 i>=0 i-- ;
You made it 100x harder than it needed to be lmao @@RoadsideCoder
@@aneesh1701
Dude 😂😂😂 who told you JS is not a real language.
Real coders just code the ideas, they don't make excuses.
So why does the code stope running when it reaches “Olleh” why doesn’t it just run forever I don’t understand
because we gave it the stopping case or base case
Using pointer and using while loop we can solve this right?
By swapping, which will reduce memory space as well
can you enter that code here?
let newStr= ' ';
for (let i = str.length -1; i>=0;i--){
newStr += str[i];
}
return newStr;
@@armorkinggaming1933you increased time complexity by using a new string. TC now is O(n)
It's working
We can use reverse() method as well
there's no such method as reverse() for a string
@@RoadsideCoder I did it as shown below, but your method always rocks. Thanks!
let str = "Hello, World!";
let reversedString = str.split('').reverse().join('');
console.log(reversedString);
@@vijaykumarjeeyeah there is no reverse method, u converted it into array and then reversed array.
This is correct, but using recursion will be more optimised
@@RoadsideCoder
How?
Me: Simple for loop with string length , just decrease the count 😂😂😂
Sir are you using computer glass or a power glass?
both
When will recurrsion stop?
when it hits the base case i.e., when string becomes empty
I am here to write in comment why it is working without loop. Now I understand it is recurrsion.@@RoadsideCoder
Can't we use split and reverse
Thats not a optimised solution
in python just use str[::-1]
What if string is null. It will throw error
str.split('').reverse().join('')
not optimised
Good one but not optimised use one loop for this o(n)
this is a bruh moment
Bro it won't work...there is no possibility for str as empty string...how it is working at your side...
bro u need to learn js again, here u go - ua-cam.com/play/PLKhlp2qtUcSZtJefDThsXcsAbRBCSTgW4.html
Hello bro..with this kind of speed..you missed to show us, adding of closed bracket after substr...at start you havn't added. later, on adding the base case it was there..
bro, just watch the full video from the link. shorts only allow 1 min
Brother i have typed the same code but it gave the o/p like --> Maximum call stack size exceeded....why ????
did u write the base case?
@RoadsideCoder i'm just a beginner in javascript so may I know what is meant by base case ????
The “str.charAt(0)” should be outside of function call
return reverseString(str.substr(1)) + str.charAt(0);
function Arr(val){
if(val===''){
return '';
}else{
return Arr(val.substr(1)+val.charAt(0))
}
}
console.log(Arr('hello'))
it showing maximum call stack size exceed
cause the code is wrong in the video
function Arr(val) {
if (val === '') {
return '';
} else {
return Arr(val.substr(1)) + val.charAt(0);
}
}
console.log(Arr('hello'));
@@GourabMukherjee3011exactly!! In the video he mistyped the return statement
@@paritoshchauhan3266 yeah 👍
It's pointless to use all these methods. Readalibilty is poor.
Just run a for loop in reverse.
let newStr= ' ';
for (let i = str.length -1; i>=0;i--){
newStr += str[i];
}
return newStr;
Very simple way. Why trying all this?
Your algo: TC O(n), SC O(n)
Algo in video: TC O(n) SC O(1)
You just increased space complexity to O(n) by using a new string. The mentioned way in video has space complexity O(1) and time complexity O(n)
@@paritoshchauhan3266no the recursive solution is not of space complexity O(1) its also O(n) because for a string of length n there will be n recursive calls to the function which all individually add a layer to the call stack which uses up memory
@@furyzenblade3558 ohh yes, got it, my bad. I did not count that in.
@@paritoshchauhan3266not only that when returning it creates a new string....which also takes O(n) space
@@soumyadeepdash4805 bro if question asks to return a string then that is not considered in space complexity.
No need to right else
This is overcomplicating something very simple. Even if you had 10mil characters in one string and passed it into this function it would make such a minimal difference
Welcome to world of DSA
Don't ever write code like this. No company would accept this code in a pull request. It's very difficult to understand what it's doing
This is how senior devs write the code, more optimised way. Every company will accept it!
this is not working even
whats the issue?
@@RoadsideCoder why are u typing the same function name reverseString inside the return statement?
recursion
No need to write else
The else is unnecessary.