15 min Java Coding Challenge - Reverse Words in a String

preview_player
Показать описание

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

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.

officialdeyj
Автор

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)

hakanpwg
Автор

Sorry but i have to say i am mindblown rn, i always asked myself why the moveable thing was there for, now after almost normal headphones died out i finally found out the reason, and i am mindblown and i love it, because i love design that works and is intelligent, like i believe everybody took that microphone with their hands towards their mouth to speak to it, while it was all the time a hands free design

a
Автор

😂 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.

pooblock
Автор

I think using functions in such simple task is not a challenge. I appreciate what u do man.

dorjderemnamsraijav
Автор

very nice channel, im sure many people will join competitive programming by seeing your content

jakirodragon
Автор

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;
}

scarzsad
Автор

public class shaikaffan{
public static void main(String args[]){
String s1="the sky is blue";
String[] s2=s1.split(" ");
for(int i=(s2.length-1);i>=0;i--) {
System.out.print(s2[i]+" ");
}
}
}

CodeCraftWithQubais
Автор

Here this code is too complicated.... here I have Done just like that as Java Does not have reverse()
public class ReverseStringLine {
public static void main(String[] args) {
String s="the sky is blue";

}
static String reverse(String s){
String result="";
String[] word=s.split(" ");
for(int i=0;i<word.length;i++){
if (i == word.length - 1)
result = word[i] + result;

else
result = " " + word[i] + result;
}
return result;
}
}

swarnalibhattacharjee
Автор

You could've used strig tokenizer to extract each word and store in an array and reverse the array and print it.

mrj
Автор

bro.. this is the longest reverse string logic i ever seen lol

SamiSabirIdrissi
Автор

String phrase = "the sky is blue";
String[] words = phrase.split(" ");
Optional<String> result = Arrays.asList(words)
.stream()
.reduce((w1, w2 ) -> (w2+" ").concat(w1));

armandopaulino
Автор

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.

Klasican
Автор

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.

HaKazzaz
Автор

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 We can solve this using split function in Java and use for loop

mohandgesmelkhalig
Автор

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

mypc
Автор

Thank you for sharing this with us. Really helpful tips.

theblindprogrammer
Автор

In Javascript, you just need several seconds to do that :
'blue is sky the'.split(' ').reverse().join(' ');

davidmahbubi
Автор

I think the comment section need to learn their isn’t always one answer to a coding question 😂

chaudharyzafar