public class Solution {
int len = input.length();
int i = len-1;
int j = len-1;
StringBuilder ans = new StringBuilder();
while (i >= 0) {
while (i >= 0 && input.charAt(i) == ' ')
i--;
j = i;
while (i >= 0 && input.charAt(i) != ' ')
i--;
String word
= input.
substring(i
+1, j
+1);
if (ans.length() == 0) {
ans.append(word);
}
else {
ans.append(" " + word);
}
}
return ans.toString();
}
}
cHVibGljIGNsYXNzIFNvbHV0aW9uIHsKCXB1YmxpYyBzdGF0aWMgU3RyaW5nIHJldmVyc2VXb3JkV2lzZShTdHJpbmcgaW5wdXQpIHsKCQkKCQlpbnQgbGVuID0gaW5wdXQubGVuZ3RoKCk7CgoJCWludCBpID0gbGVuLTE7CgkJaW50IGogPSBsZW4tMTsKCgkJU3RyaW5nQnVpbGRlciBhbnMgPSBuZXcgU3RyaW5nQnVpbGRlcigpOwoKCgkJd2hpbGUgKGkgPj0gMCkgewoKCgkJCXdoaWxlIChpID49IDAgJiYgaW5wdXQuY2hhckF0KGkpID09ICcgJykKCQkJCWktLTsKCgoJCQlqID0gaTsKCgoJCQl3aGlsZSAoaSA+PSAwICYmIGlucHV0LmNoYXJBdChpKSAhPSAnICcpCgkJCQlpLS07CgoJCQkKCQkJU3RyaW5nIHdvcmQgPSBpbnB1dC5zdWJzdHJpbmcoaSsxLCBqKzEpOwoKCQkJaWYgKGFucy5sZW5ndGgoKSA9PSAwKSB7CgoJCQkJYW5zLmFwcGVuZCh3b3JkKTsKCQkJfQoKCQkJZWxzZSB7CgoJCQkJYW5zLmFwcGVuZCgiICIgKyB3b3JkKTsKCQkJfQoJCQoJCX0KCgkJcmV0dXJuIGFucy50b1N0cmluZygpOwoJfQp9Cg==