Комментарии:
Ultimate i would say hats off sir👌👌👏👏
Ответитьwhat is the point of writing 7th line python . when we placed a condition i<n
Ответитьclass Solution(object):
def reverseWords(self, s):
sp = s.strip().split()
res = ""
i,j=0,len(sp)-1
while i<j:
sp[i],sp[j]=sp[j],sp[i]
i+=1
j-=1
return " ".join(sp)
EASIEST SOLUTION C++ NO EXTRA SPACE
string revWords(string s) {
int i=0;
while(i<s.size() && s[i]==' ')
{
i++;
}
int j=s.size()-1;
while(j>=0 && s[j]==' ')
{
j--;
}
int cnt=0;
string ans="";
string res="";
for(int x=i;x<=j;x++)
{
if(s[x]==' ')
{
cnt++;
continue;
}
if(cnt)
{
if(res=="")
{
res=ans;
}
else
{
res=ans+" "+res;
}
cnt=0;
ans="";
}
ans+=s[x];
}
res=ans+" "+res;
return res;
}
I write a main method after your code. and it doesn't work. It only returns one word of my sentence.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the sentence:");
String sentence = keyboard.next();
String result = reverseWords(sentence);
System.out.println(result);
}
Did I do anything wrong with my main method?
thanks for the explanation!!
ОтветитьAwesome explaination !!
Ответитьbest video i have found on how to reverse an word
thank u sir...
Sir it is difficult for Me to understand the concept why we take subs from (i, j-i) why not from (i,j) in c++
ОтветитьHow to skip last two words in sentence?
My input string is: Hi there. My name is Kalp. This is my lucky day. I like coding so much. This is it.
Expected output is: Hi there. name My is Kalp. my is This lucky day. coding like I so much. This is it.
Code for no extra space
class Solution {
public:
string reverseWords(string s) {
int k=s.size();
int n=k;
s+=" ";
while(n>=0)
{
while(n>=0 && s[n]==' ')
{
n--;
}
if(n<0) break;
int j=n-1;
while(j>=0 && s[j]!=' ')
{
j--;
}
s+=s.substr(j+1,n-j)+" ";
n=j-1;
}
s=s.substr(k+1,s.size()-(k+2));
return s;
}
};
While(i<n&&s[i]==' '){
i++;
}
Why is it giving empty character constant error and empty expression error . Please tell . I have written the code exactly like u but it doesn't run
This isn't working for me.
The output is:
"blue is sky the the"
"world hello hello"
"example good a a".
The output always duplicate the last word.
Edit*
Never mind, I miss-placed the i = j + 1 into the final else statement
I REALLY LIKED THE PART WHERE HE EXPLAINED THE CODE IN C++ AND PYTHON
Ответитьcommented using API.
Ответитьvideo are very helpful.
Ответитьs = sentence.split()[::-1]
l = []
for i in s:
l.append(i[::-1])
print('-'.join(l))
Thank you, was very helpful
ОтветитьGood video sir,
Can you write this Java program too:
Input: "This is, the most; particular example!"
Output: "example particular, most the; is This!"
I don't understand why we can't just use trim in java and strip in python to get rid of the leading and trailing spaces.
Ответитьthanks.........
ОтветитьHow to write complext code 101
Ответитьcan anyone tell what is the significance of writing if(i>=n)break; here?
it will be really helpful
public static void test2() {
String str = " My name is Aman ";
StringBuilder strb = new StringBuilder(str);
String str2 = strb.reverse().toString();
int i=0; int j=0;
while(i<str2.length() && str2.charAt(i)==' ') {
i++;
}
String str3="";
while(j<str2.length()) {
j=i;
while(i<str2.length() && str2.charAt(i)!=' ') {
i++;
}
StringBuilder strb1 = new StringBuilder(str2.substring(j, i));
str3=str3+" "+strb1.reverse().toString();
i++;
System.out.println(str3);
}
System.out.println(str3);
}
always try to show runtime speed and space after code is submitted
Ответитьthank you bro, your explanations are so clear
ОтветитьIn Leetcode, it gives error for Python3
Time Limit Exceeded
this code is not working in leetcode
ОтветитьYour vedios are really great. In this solution, we append and then copy result string in every iteration which is not optimal, TC will become O(n^2). It is better to start from end of input string, reverse each sub-string and then append it to result string, this will give us TC as O(n)
ОтветитьThanku sir
ОтветитьHi @Abhishek the video is very good, but I am thinking can we split the string using spaces and rearranging in the reverse order it will do our requirement right.
ОтветитьC++ Code is not working
ОтветитьThanks
Ответитьplease provide the desired code in description box
ОтветитьThank You Sir.
Ответитьpython
class Solution:
def reverseWords(self, s: str) -> str:
return " ".join(list(filter(lambda x: len(x)>0,s.split(" ")))[::-1])
I am wondering can we use stringtok in this question? Yes, it will be space O(n) but can be solved quicker I guess!
ОтветитьCode is not reversing , please help
ОтветитьPerfect
ОтветитьNice explanation
ОтветитьNice explanation. Although I was expecting in-place solution.
ОтветитьJaduu!
ОтветитьIf you need O(1) space sol:
class Solution {
public:
void reverseword(string &s, int i, int j){ //reverse string from i to j
while(i<j){
char t = s[i];
s[i++] = s[j];
s[j--] = t;
}
}
string reverseWords(string s) {
int i=0, j=0;
int l=0;
int len = s.size();
int wordcount = 0;
while(true){
while(i<len && s[i] == ' ') //skip spaces in front
i++;
if(i==len)
break;
if(wordcount) //add space between the middle words
s[j++] = ' ';
l=j;
while(i<len && s[i] != ' '){ //place the word at the starting, removing spaces
s[j] = s[i];
j++, i++;
}
reverseword(s,l,j-1); //reverse word
wordcount++;
}
s.resize(j); //resize s to smaller string after removing spaces
reverse(s.begin(), s.end()); //Finally, reverse entire string to get the result
return s;
}
};
Wow man... Quality content every time
ОтветитьThank you. You did in a very clear way.
ОтветитьReally you are the best!. Especially those python solutions
ОтветитьExcellent explanation
ОтветитьTime complexity should be
O(n) We are still iterating through the array linearly, and when the j loop is activated,
it still linearly searches in the same array but when it reaches a letter/nospace until it reaches '_' (_ is 1 space). , then continues from outer main loop again, the inner loop if it satisfies the condition all the way to the end of len(array).
Space should be
O(n) since we create a result array, it cannot be O(1) because we are not manipulating the input array in place.
Please feel free to correct me if I'm wrong but I am thinking this is correct.
Sir, there's no doubt that you make a problem look very easy but it will be great if you write code in C language also...plzzzzz
ОтветитьExcellent
Thank you very much
Changing the index value after spaces is like magic
Really awesome 😎