ZOHO Interview Question Solution in Java.
CODE
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String input = br.readLine();
List words = Arrays.asList(input.split(" "));
Map positional = words.stream()
.map(word -> {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(word);
Integer pos = null;
if (m.find()) {
pos = Integer.parseInt(m.group());
}
String text = word.replaceAll("\\d+", "");
return new AbstractMap.SimpleEntry<>(pos, text);
})
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue
));
String result = IntStream.range(0, words.size())
.mapToObj(positional::get)
.collect(Collectors.joining(" "));
System.out.println(result);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
