Created
November 10, 2017 02:05
-
-
Save shailrshah/6571d6b032bebccc05944d0742dcd1d3 to your computer and use it in GitHub Desktop.
Given a sentence delimited by spaces, reverse the sentence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String reverseWords(String s) { | |
String[] a = s.replaceAll("\\s+", " ").split(" "); | |
int i = 0, j = a.length-1; | |
while(i < j) { | |
String temp = a[i]; | |
a[i++] = a[j]; | |
a[j--] = temp; | |
} | |
return String.join(" ", a).trim(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment