Reverse String | Leetcode #344

preview_player
Показать описание
This video explains a basic and tricky 2 pointer technique for reversing a string.I have explained it using an example and CODE walkthrough at the end of the video. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

=================================================================
=================================================================

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

I would say this one was the easiest of all the challenges till date :B

subham-raj
Автор

man you deserve much more subscriber than erichito.I mean he is good in programming himself but not not good at teaching.But you are just brilliant teaching.

souvikpaul
Автор

Two pointers are not needed, loop from o to length/2 is enough and then values can be swapped. The only thing to take care that length/2 is int eo it works for even and odd both.
n=len(s)
for i in range(n//2):
s[i], s[n-1-i] = s[n-1-i], s[i]

PrinceKumar-prro
Автор

Alternatively you could also use a for loop from 0 to middle of string.
Next, swap current with total - current index
```js
function swap(arr, a, b) {
[ arr[a], arr[b] ] = [ arr[b], arr[a] ];
}

function reverseString(s) {
const { length } = s;

for (let i = 0; i < length / 2; i += 1) {
swap(s, i, length - 1 - i);
}

return s;
};
```

MarcoBiedermann
Автор

Nice 1, I came long time back here. But got new experience(u shown the code).

vinit__rai
Автор

One more tiny optimization would be if left and right values are same, don't swap them and move on
Edit: that way we won't have to swap characters which are same

sudhanshukumar
Автор

Sir, iwill be going in college this year
I have doubt that all are doing competative programing for getting placed in tech giants by following tourist, errictho etc and do some project at the end time to get placed

But i want to develop softwares
That's why i opt CSE
Is it good to focus more on development and leetcode and gfg questions(not like errictho aur other coders) for only placement level?

Agyaatr
Автор

reverse(s.begin(), s.end());
stl reverse fun.

NikhilKumar-oymx
Автор

From yesterday itself I am in a dilemma that whether you upload this video or not....😁😁😁😁😅😅😅😅

Voice_Of_Thoughts
Автор

Python code:
s.reverse() # Could it be any more easier?

akshitarora
Автор

can you explain and solve the problem of Google Hash Code 2020

muhammedjack
Автор

First comment and first viewer, , but today it is two hours late

punjabicodingstyle
Автор

Java Solution
class Solution {
public void reverseString(char[] s) {
int i=0, j=s.length-1;
while(i<j)
{
char temp;
temp=s[i];
s[i]=s[j];
s[j]=temp;
i++;
j--;
}
}
}

md_aaqil
Автор

For(i=strlen(str) - 1;i>=0;i++)
print(s[i])
My soln

anshshah