Problem
Objective
Today, we're working with regular expressions. Check out the Tutorial tab for learning materials and an instructional video!
Task
Consider a database table, Emails, which has the attributes First Name and Email ID. Given rows of data simulating the Emails table, print an alphabetically-ordered list of people whose email address ends in .
Input Format
The first line contains an integer, , total number of rows in the table.
Each of the subsequent lines contains space-separated strings denoting a person's first name and email ID, respectively.
Constraints // 조건
- Each of the first names consists of lower case letters only.
- Each of the email IDs consists of lower case letters , and only.
- The length of the first name is no longer than 20.
- The length of the email ID is no longer than 50.
Output Format
Print an alphabetically-ordered list of first names for every user with a gmail account. Each name must be printed on a new line.
Sample Input
6
riya riya@gmail.com
julia julia@julia.me
julia sjulia@gmail.com
julia julia@gmail.com
samantha samantha@gmail.com
tanya tanya@gmail.com
Sample Output
julia
julia
riya
samantha
tanya
How I solved the problem
# 정규표현식을 사용할 때, 자바는 Pattern과 Matcher 클래스를 사용함.
# Sorting까지 해야하는 문제!
# List에 넣기 위해서 클래스를 추가 해줌!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int N = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); List<Address> addressList = new ArrayList<Address>(); for (int NItr = 0; NItr < N; NItr++) { String[] firstNameEmailID = scanner.nextLine().split(" "); String firstName = firstNameEmailID[0]; String emailID = firstNameEmailID[1]; Address address = new Address(firstName, emailID); addressList.add(address); } scanner.close(); getNameList(addressList); } public static void getNameList (List<Address> addressList) { int addressListCount = addressList.size(); List<String> matchedList = new ArrayList<String>(); for (int i = 0; i < addressListCount; i++) { Address address = addressList.get(i); String firstName = address.firstName; String emailID = address.emailID; boolean isMatch = false; //Each of the first names consists of lower case letters only. // The length of the first name is no longer than 20. Pattern firstNamePattern = Pattern.compile("[a-z]{1,20}"); Matcher firstNameMatcher = firstNamePattern.matcher(firstName); //Each of the email IDs consists of lower case letters[a-z] ,@ and . only. //The length of the email ID is no longer than 50. Pattern emailIdPattern = Pattern.compile("([a-z]{1,40})@gmail\\.com"); Matcher emailIdMatcher = emailIdPattern.matcher(emailID); if (firstNameMatcher.find() && emailIdMatcher.find()) { isMatch = true; } else { isMatch = false; } if (isMatch) { matchedList.add(firstName); } } //alphabetically sorting String[] nameList = new String[matchedList.size()]; nameList = matchedList.toArray(nameList); Arrays.sort(nameList); for(String name : nameList) { System.out.println(name); } } } class Address { String firstName; String emailID; Address (String firstName, String emailID) { this.firstName = firstName; this.emailID = emailID; } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.01.24] Birthday Cake Candles (0) | 2019.01.24 |
---|---|
[2019.01.23] Bitwise AND (1) | 2019.01.23 |
[2019.01.21] Testing (0) | 2019.01.21 |
[2019.01.17] Nested Logic (0) | 2019.01.17 |
[2019.01.16] Running Time and Complexity (0) | 2019.01.16 |
댓글