@@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);
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.
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.
Recursive functions is never considered as optimised approach
Why is that?? Recursion is a great way of solving problems with recurring sub-problem
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'));
@@gourabmukherjee4848exactly!! In the video he mistyped the return statement
@@paritoshchauhan3266 yeah 👍
Love you mere bhaii❤
Use 2 pointer and swap like a pro 😎
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)
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?
Good one but not optimised use one loop for this o(n)
It's working
What if string is null. It will throw error
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.
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
str.split('').reverse().join('')
not optimised
Sir are you using computer glass or a power glass?
both
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);
Can't we use split and reverse
Thats not a optimised solution
No need to right else
this is a bruh moment
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
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
No need to write else
The else is unnecessary.
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!