Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * You are part of the back-end development team of the next Facebook.
- * You are given a line of usernames, between one of the following symbols: space, “/”, “\”, “(“, “)”.
- * First you have to export all valid usernames. A valid username starts with a letter and can contain only letters, digits and “_”.
- * It cannot be less than 3 or more than 25 symbols long.
- * Your task is to sum the length of every 2 consecutive valid usernames and print on the console
- * the 2 valid usernames with biggest sum of their lengths, each on a separate line.
- *
- * The Input comes from the console. One line will hold all the data. The line will hold characters in the range [0 … 9999].
- * It will hold usernames, divided by the symbols: space, “/”, “\”, “(“, “)”.
- * The input data will always be valid and in the format described. There is no need to check it explicitly.
- *
- * Print at the console the 2 consecutive valid usernames with the biggest sum of their lengths each on a separate line.
- * If there are 2 or more couples of usernames with the same sum of their lengths, print he left most.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class ValidUsernames {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String[] usernames = reader.readLine().split("[\\s()/\\\\]+");
- ArrayList<String> validNames = new ArrayList<>(usernames.length);
- Pattern pattern = Pattern.compile("\\b[a-zA-Z]\\w{2,24}\\b");
- Matcher matcher;
- for (String username : usernames) {
- matcher = pattern.matcher(username);
- if (matcher.find()) {
- validNames.add(username);
- }
- }
- int maxSum = 0;
- int indexSecondName = 1;
- for (int i = 1; i < validNames.size(); i++) {
- int currentSum = validNames.get(i).length() + validNames.get(i - 1).length();
- if (currentSum > maxSum) {
- maxSum = currentSum;
- indexSecondName = i;
- }
- }
- System.out.println(validNames.get(indexSecondName - 1));
- System.out.println(validNames.get(indexSecondName));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement