Problem Statement - 1
Mr. Agoji given a shuffled string made by randomly shuffling a special string.
A string will be called special only if it is formed by joining some special words any number of times. Special words are mapping of numbers (0 <= number < 10) to their words, for example, mapping of '0' to 'zero', mapping of '1' to 'one', and so on.
Mr. Agoji is asked to convert the shuffled string into smallest special number. A special number is a number formed using numbers (0 <= number < 10) without any leading zeroes.
Mr. Agoji being not so good with numbers and strings ask for your help.
Input Format
The first line of the input T contains the number of test cases,
1 <= T <= 100 For each test case.
There will be a, s shuffled string, on a separate line
1 <= s.length <= 100000
Output Format
For each test case, on the new line print the special number and if all the numbers are zero print zero.
Sample Input
ewtooetzrowon
ttnrwoooeeefurh
Sample Output
1022
1234
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class numbersToWords {
public static int getSpecialNumber(String inpStr) {
HashMap<String, Integer> map = new HashMap<>();
ArrayList<Integer> res = new ArrayList<>();
map.put("zero", 0);
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
map.put("five", 5);
map.put("six", 6);
map.put("seven", 7);
map.put("eight", 8);
map.put("nine", 9);
for (String numStr : map.keySet()) {
if (inpStr.length() != 0) {
String shuffStr = inpStr;
int flag = 1;
while (flag == 1) {
int count = 0;
for (Character letter : numStr.toCharArray()) {
if (shuffStr.contains(letter + "")) {
count++;
shuffStr = shuffStr.replaceFirst(letter + "", "");
}
}
if (count == numStr.length()) {
res.add(map.get(numStr));
inpStr = shuffStr;
} else {
flag = 0;
}
}
}else{
break;
}
}
String resStr = "";
Collections.sort(res);
if (Collections.frequency(res, 0) == res.size()) {
return 0;
}
while (res.get(0)==0) {
res.remove(0);
res.add(1, 0);
}
for (Integer integer : res) {
resStr+=integer;
}
return Integer.parseInt(resStr);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
System.out.println(getSpecialNumber(sc.next()));
}
}
}
Comments
Post a Comment