02. Rage Quit - Java
Здравейте, решението ми работи за дадените inputs, но ми дава само 20/100. Моля за работещо решение на Java!
Условие:
Every gamer knows what rage-quitting means. It’s basically when you’re just not good enough and you blame everybody else for losing a game. You press the CAPS LOCK key on the keyboard and flood the chat with gibberish to show your frustration.
John is a gamer and a bad one at that. He wants to be the most annoying kid on his team, so when he rage-quits, he wants something truly spectacular. He asks for your help. He’ll give you a series of strings followed by non-negative numbers, e.g., "a3"; you need to print on the console each string repeated N times; convert the letters to uppercase beforehand. In the example, you need to write back "AAA".
On the output, print first a statistic of the number of unique symbols used (the casing of letters is irrelevant, meaning that 'a' and 'A' are the same); the format should be "Unique symbols used {0}". Then, print the rage message itself.
The strings and numbers will not be separated by anything. The input will always start with a string, and for each string, there will be a corresponding number. The entire input will be given on a single line; John is too lazy to make your job easier.
Input
- The input data should be read from the console.
- It consists of a single line holding a series of string-number sequences.
- The input data will always be valid and in the format described. There is no need to check it explicitly.
Output
- The output should be printed on the console. It should consist of exactly two lines.
- On the first line, print the number of unique symbols used in the message.
- On the second line, print the resulting rage message itself.
Constraints
- The count of string-number pairs will be in the range [1 … 20 000].
- Each string will contain any character except digits. The length of each string will be in the range [1 … 20].
- The repeat count for each string will be an integer in the range [0 … 20].
- Allowed working time for your program: 0.3 seconds. Allowed memory: 64MB.
Examples
Input |
Output |
Comments |
a3 |
Unique symbols used: 1 AAA |
We have just one string-number pair. The symbol is 'a', convert it to uppercase and repeat 3 times: AAA. Only one symbol is used ('A'). |
aSd2&5s@1 |
Unique symbols used: 5 ASDASD&&&&&S@ |
"aSd" is converted to "ASD" and repeated twice; "&" is repeated 5 times; "s@" is converted to "S@" and repeated once.
|
Моят код:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RageQuit {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine().toUpperCase();
String regex = "\\D+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
List<String> result = new ArrayList<>();
while (matcher.find()) {
result.add(matcher.group());
}
//collect symbols
List<String> symbols = new ArrayList<String>();
int countSymbols = 0;
for(String eachSymbol : result){
String[] currentItem = eachSymbol.split("");
int itemLenght = currentItem.length;
//if not used
for(int i = 0; i < itemLenght; i++){
if(!symbols.contains(currentItem[i])){
symbols.add(currentItem[i]);
countSymbols++;
}
}
}
//
if(symbols.contains(" ")){
countSymbols--;
}
System.out.printf("Unique symbols used: %d\n", countSymbols);
//we get numbers
String regexNumbers = "\\d+";
Pattern patternN = Pattern.compile(regexNumbers);
// Create a matcher for the input string
Matcher matcherN = patternN.matcher(input);
List<Integer> resultN = new ArrayList<>();
while (matcherN.find()) {
resultN.add(Integer.parseInt(matcherN.group()));
}
//add to output
for(int j = 0; j < result.size(); j++){
//repeat for each word
for(int n = 0; n < resultN.get(j); n++){
System.out.printf("%s", result.get(j));
}
}
}
}