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.
RegEx Patterns and Database problem solution | 30 days of code | HackerRank

Problem solution in Java programming language.

import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.regex.*;

class Email{
    public String name;
    public String email;
    
    public Email(String name, String email){
        this.name = name;
        this.email = email;
    }
}

public class Solution {
    // Simulate the Emails table
    static List<Email> emailList;
    
    public static List<String> getGmailUsers(){
        List<String> names = new ArrayList<String>();
        Pattern p = Pattern.compile(".*@gmail.com");
        
        for(Email e : emailList){
            if(p.matcher(e.email).matches()){
                names.add(e.name);
            }
        }
        Collections.sort(names);
        return names;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        emailList  = new ArrayList<Email>();
        int n = in.nextInt();
        for(int i = 0; i < n; i++){
            emailList.add(new Email(in.next(), in.next()));
        }
        in.close();
        
        for(String name : getGmailUsers()){
            System.out.println(name);
        }
    }
}

Second solution



import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<String> nameList  = new ArrayList<String>();
        
        int n = in.nextInt();
        for(int i = 0; i < n; i++){
            String name = in.next();
            String[] email = in.next().split("@");
            if(email[1].equals("gmail.com")){
                nameList.add(name);
            }
        }
        in.close();
        Collections.sort(nameList);

        for(String name : nameList){
            System.out.println(name);
        }
    }
}

Program solution in Python programming language.

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    N = int(input())
    nw = []

    for N_itr in range(N):
        firstNameEmailID = input().split()

        firstName = firstNameEmailID[0]

        emailID = firstNameEmailID[1]
        if '@gmail.com' in emailID:
            nw.append(firstName)
    
    nw = sorted(nw)
    for item in nw:
        print(item)