One more Approach : Split the String sentence into String array based on white spaces. Reverse iterate over that String array and keep concatenating another result String with the "i"th String and a space and return the result.
Easier way: use the string tokenizer with space denim and print each token but backwards. if there are more than 1 space between the words u can first replace all multiple spaces with a single space(Object String replace method)
Whaaaaaat 😂 that is so complicated for no reason. You can just create an array split by spaces using the string and then use a for loop to print from highest index to the lowest. Let me know if I’m wrong haha.
For all those who think he made things too complicated by not using split: This is about solving the problem without allocating any extra space. Split works on String which you need to create first from char[]. Also it returns an array of Strings. This is about working with the char[] and reversing indexes. So yeah split will work and probably be more readable but does not meet the conditions.
//what about this..I am using split and it meet the condition import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc = new Scanner(System.in); String test = sc.nextLine(); String arr[] = test.split(" "); String result = ""; for(int i=arr.length-1; i>=0; i--){ if(i==0) { result += arr[i]; break; } result+=arr[i]+" "; } System.out.println(result); } }
@@spectrouts695 Hi dude, Nope, this is pretty much the same solution all of the Python comments suggested :) You are solving a different problem as your input is a String instead of char[]. In addition you create an array of String for each line you get, which is exactly what he is avoiding. Are you familiar with big O notation? Not a good enough solution for this question my dude.
@@HaKazzaz hello bro, I am just send the code which shows the use of split() and also more readable. Here off course I am taking String as input but one can convert easily to String from char[]. I am not saying that his code is wrong but proving my point that we can also use split() to make it more readable. Thanks for your time.
@@spectrouts695 I see. I guess I just misunderstood because you said "meets the condition" but the condition is to reverse words without any extra space (converting to String, and then splitting into an array is extra space).
for the 1st example(reverse String) you could also do this: public static String reverseString(String word){ String result = ""; for(int i = word.length() - 1; i >= 0; i--){ result += word.charAt(i);; } return result; }
Here this code is too complicated.... here I have Done just like that as Java Does not have reverse() method_____________________--> public class ReverseStringLine { public static void main(String[] args) { String s="the sky is blue"; System.out.println(reverse(s)); } static String reverse(String s){ String result=""; String[] word=s.split(" "); for(int i=0;i
This is how I did it. I recommend using StringBuilder, it's a really awesome tool to work with Strings. try (Scanner scan = new Scanner(System.in)) { System.out.println("Enter a string to be reversed: "); String s = scan.nextLine(); String[] words = s.split("\\s+"); StringBuilder sb = new StringBuilder(); for (int i = words.length - 1; i >= 0; i--) { if (i == 0) { sb.append(words[i]); } else { sb.append(words[i] + " "); } } System.out.println(" Reversed: " + sb.toString()); }
This was my solution. I have been using java for 1 week. public class Solution { public String reverseWords(String s) { String[] stringSplit = s.split(" "); String ans = ""; for (int i = stringSplit.length -1; i >= 0; i--) ans = ans + " " + stringSplit[i]; return ans.trim().replaceAll("\\s+"," "); } }
@TheSkepticSkwerl i think you miss the last point which says could you do it with out allocating extra space? Thus you can't allocate a new string array and use split method that is the only reason which makes this question a bit hard
Split works on String which you need to create first from char[]. Also it returns an array of Strings and this is about working with the char[] without allocating any extra space. So yeah it will work and probably be more readable but does not meet the conditions.
The languages for lazy pple lol😀. The problem is many do not know whats happening inside. You just gave the computer .reverse.join and it did all the work for you. Here, you are doing the job and its fun because you learn something.
Asha and Amar are playing SpaceKings a video game. It is a two player game where the second player is the helper. Asha needs your help maximizing her gold while playing her favorite game. Both are facing N aliens. Asha and Amar are both at a single location and the aliens are in lined up in front of them. Asha and Amar take turns shooting the aliens, and she goes first. During her turn, Asha may choose any alien to shoot at (this means Asha may choose to skip a turn). During his turn, Amar always shoots the alien closest to him to help Asha maximize her gold. Asha and Amar can not shoot dead alien. If Asha shoots at an alien, its hit points are reduced by P. If Amar shoots at an alien, its hit points are reduced by Q. If an alien's hit points goes below 1, it is killed. The ith alien starts with Hi hit points. Asha is awarded Gi gold if her shot kills the ith alien, but none if Amar's shot kills it. What is the maximum amount of gold Asha can obtain? Input Each case begins with one line containing three space-separated integers representing P, Q and N. N lines then follow, with the ith line containing two space-separated integers representing Hi and Gi. The aliens are given in the order of their distance from Asha and Amar. In other words, Amar will shoot at the ith alien only if all aliens < i are dead. Output Maximum amount of gold that Asha can get Input 20 60 3 80 100 80 200 120 300 Output 500 Explanation Asha should give up the first alien. During her first two turns she should soften up the third alien bringing it down to 80 hp, allowing her to easily get the last shot on the second and the third aliens Input 50 60 2 40 100 40 90 Output 100 Input 50 60 2 40 100 40 200 Output 200 Input 50 100 2 60 100 60 200 Output 200 Input 50 400 2 60 100 190 200 Output 0 solve it plzz
Hi Yusen, thank you for this great content. I have one question. Do you maybe know - when people are on live coding technical interviews of this type, do they need to solve this kind of problems completely from their head? Or it is allowed to check the answers on the internet? Regards.
the easiest way for beginner without any extra methods package Programming; import java.util.Scanner; public class ReverseWordsInSentence { public static void main(String[] args) { try (Scanner in = new Scanner(System.in)) { System.out.print("Enter a sentence: "); String str = in.nextLine(); int len = str.length(); String word = ""; for (int i = len - 1; i >= 0; i--) { char c = str.charAt(i); if (c != ' ') { word = c + word; } else { System.out.print(word + " "); word = ""; } } System.out.print(word); // Print the last word } } } Please reply what do you think and if you have any better program than this please share
Hai im from the future, which 2021, jk how about my solve public static void main(String[] args) { String a = "andreo friendly timorence manukoa"; String hasil[] = a.split(" "); String c; for (int i = 0; i < hasil.length; i++) { a = hasil[(hasil.length-1)-i]; System.out.print(a + " "); } }
Bonjour une autre façon de faire et merci pour le tuto public static String reverse(String chaine) { String[] parts = chaine.split(" "); for(int i = 0; i < parts.length / 2; i++) { String temp = parts[i]; parts[i] = parts[parts.length - i - 1]; parts[parts.length - i - 1] = temp; } String chaine2 = String.join(" ", parts); System.out.println(chaine2); return chaine2; }
Good solution but this is not what the question wants you to do tho. First, you allocated new space for the string. Second, you did not modify the provided string.
Yusen Zhang only watched the first 10 seconds of your video so assumed this was what the question was asking. I'm sure you could adapt my answer to fit however you like.