The problem
When given a string of space-separated phrases, return the phrase with the longest size.
If there are a number of phrases with the longest size, return the final occasion of the phrase with the longest size.
Instance:
'crimson white blue' // returns string worth of white
'crimson blue gold' // returns gold
The answer in Java code
Choice 1:
public class Resolution {
public static String longestWord(String wordString) {
String longest = "";
for (String phrase: wordString.cut up(" "))
if (phrase.size()>=longest.size())
longest = phrase;
return longest;
}
}
Choice 2:
import java.util.Arrays;
public class Resolution {
public static String longestWord(String phrases) {
return Arrays.stream(phrases.cut up(" "))
.cut back((x, y) -> x.size() <= y.size() ? y : x).get();
}
}
Choice 3:
import java.util.*;
public class Resolution {
public static String longestWord(String wordString) {
ultimate String [] ary = wordString.cut up(" ");
Arrays.type(ary, (a,b) -> a.size() >= b.size() ? -1 : 1);
return ary[0];
}
}
Take a look at instances to validate our answer
import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class ExampleSolutionTests {
@Take a look at
public void assessments() {
assertEquals("fgh", Resolution.longestWord("a b c d e fgh"));
assertEquals("three", Resolution.longestWord("one two three"));
assertEquals("gray", Resolution.longestWord("crimson blue gray"));
}
}