Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Order {
- public static String order(String words) {
- String[] arrayOfWords = words.split("\\s+");
- int amountOfWords = arrayOfWords.length+1;
- String[] orderedWords = new String[amountOfWords-1];
- int i = 0;
- if (words == "") return "";
- while(i < amountOfWords-1){
- int number = Integer.parseInt(extractNumber(arrayOfWords[i]));
- orderedWords[number-1] = arrayOfWords[i] + " ";
- i++;
- }
- String s = new String();
- for (String w : orderedWords){
- s += w;
- }
- return s.trim();
- }
- public static String extractNumber(String str){
- if(str == null || str.isEmpty()) return "";
- StringBuilder sb = new StringBuilder();
- boolean found = false;
- for(char c : str.toCharArray()){
- if(Character.isDigit(c) && c != '0'){
- sb.append(c);
- found = true;
- } else if(found){
- break;
- }
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement