[HackerRank][Regex](java) Applications 1

728x90

 


 

■  Split the Phone Numbers

https://www.hackerrank.com/challenges/split-number/problem?isFullScreen=true

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        String m[] = {"CountryCode=", ",LocalAreaCode=", ",Number="};
        String ss[] = new String[3];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            ss = (sc.nextLine()).split("\\s|\\-"); // "[\\s\\-]"
            for (int j = 0; j < 3; ++j) {
                sb.append(m[j] + ss[j]);
            }
            sb.append("\n");
        }
        sc.close();
        System.out.print(sb.toString());
    }
}
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.valueOf(sc.nextLine());
		Pattern p = Pattern.compile("(\\d)+(?=[\\s\\-])?");
		String str[] = {"CountryCode=", ",LocalAreaCode=", ",Number="};
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n; ++i) {
			Matcher m = p.matcher(sc.nextLine());
			int j = 0;					 // for (int j = 0; j < 3; ++j) {
			while(m.find()) {			 //     m.find();
				sb.append(str[j++]);	 //     sb.append(str[j]);
				sb.append(m.group());
			}
			sb.append("\n");
		}
		sc.close();
		System.out.print(sb.toString());
	}
}

 


 

■  HackerRank Tweets

https://www.hackerrank.com/challenges/hackerrank-tweets/problem?isFullScreen=true 

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine()), cnt = 0;
        Pattern p = Pattern.compile("hackerrank");
        for (int i = 0; i < n; ++i) {
            Matcher m = p.matcher(sc.nextLine().toLowerCase());
            while(m.find()) {
                cnt++;
            }
        }
        sc.close();
        System.out.println(cnt);
    }
}

 


 

■  Utopian Identification Number

https://www.hackerrank.com/challenges/utopian-identification-number/problem?isFullScreen=true 

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        Pattern p = Pattern.compile("^[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}$");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            Matcher m = p.matcher(sc.nextLine());
            sb.append(m.find() ? "VALID\n" : "INVALID\n");
        }
        sc.close();
        System.out.print(sb.toString());
    }
}

 


 

■  Find HackerRank

https://www.hackerrank.com/challenges/find-hackerrank/problem?isFullScreen=true 

import java.util.*;
import java.util.regex.Pattern;

public class Solution {	// Pattern.compile().matcher(), Matcher.find()

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            int r = -1;
            if ( Pattern.compile("^hackerrank").matcher(s).find() ) {
                r = 1;
            }
            if ( Pattern.compile("hackerrank$").matcher(s).find() ) {
                r = 2;
            }
            if ( Pattern.compile("^(hackerrank(.)+hackerrank|hackerrank)$").matcher(s).find() ) {
                r = 0;
            }
            sb.append(r + "\n");
        }
        sc.close();
        System.out.print(sb.toString());
    }
}

(+)

import java.util.*;

public class Solution {	// String.matches()

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        String p1 = "^hackerrank.*", p2 = ".*hackerrank$";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            boolean b1 = s.matches(p1);
            boolean b2 = s.matches(p2);
            int r = -1;
            if ( b1 && b2 ) {
            	r = 0;
            } else if ( b1 ) {
            	r = 1;
            } else if ( b2 ) {
            	r = 2;
            }
            sb.append(r + "\n");
        }
        sc.close();
        System.out.print(sb.toString());
    }
}
import java.util.*;
import java.util.regex.Pattern;

public class Solution {	// String.startWith()/endsWith()

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        String p = "hackerrank";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            boolean start = s.startsWith(p);
            boolean end = s.endsWith(p);
            int r = -1;
            if ( start && end ) {
                r = 0;
            } else if ( start ) {
                r = 1;
            } else if ( end ) {
                r = 2;
            }
            sb.append(r + "\n");
        }
        sc.close();
        System.out.print(sb.toString());
    }
}
import java.util.*;
import java.util.regex.Pattern;

public class Solution {	// Pattern.compile().matcher(), Matcher.find()

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        String p = "hackerrank";
        Pattern p1 = Pattern.compile("^"+p);
        Pattern p2 = Pattern.compile(p+"$");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            boolean b1 = p1.matcher(s).find();
            boolean b2 = p2.matcher(s).find();
            int r;
            if ( b1 && b2 ) {
                r = 0;
            } else if ( b1 ) {
                r = 1;
            } else if ( b2 ) {
                r = 2;
            } else {
                r = -1;
            }
            sb.append(r + "\n");
        }
        sc.close();
        System.out.print(sb.toString());
    }
}

 


 

■  Saying Hi

https://www.hackerrank.com/challenges/saying-hi/problem?isFullScreen=true 

import java.util.*;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        Pattern p = Pattern.compile("^[Hh][Ii][ ][^Dd]");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            if (p.matcher(s).find()) {
                sb.append(s + "\n");
            }
        }
        sc.close();
        System.out.print(sb.toString());
    }
}

 


 

■  Detecting Valid Latitude and Longitude Pairs

https://www.hackerrank.com/challenges/detecting-valid-latitude-and-longitude/problem?isFullScreen=true 

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        Pattern p = Pattern.compile("\\(([-+]*[1-9][0-9]*(\\.[0-9]+)*), ([-+]*[1-9][0-9]*(\\.[0-9]+)*)\\)");
        //Pattern p = Pattern.compile("\\(([-+]*(0|[1-9][0-9]*)(\\.[0-9]+)*), ([-+]*(0|[1-9][0-9]*)(\\.[0-9]+)*)\\)");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            Matcher m = p.matcher(s);
            if (m.find()) {
                Double d1 = Double.valueOf(m.group(1));
                Double d2 = Double.valueOf(m.group(3));  // m.group(4)
                sb.append( d1 >= -90 && d1 <= 90 && d2 >= -180 && d2 <= 180 ? "Valid\n" : "Invalid\n" );
            } else {
                sb.append("Invalid\n");
            }
        }
        sc.close();
        System.out.print(sb.toString());
    }
}

 


 

■  Alien Username

https://www.hackerrank.com/challenges/alien-username/problem?isFullScreen=true 

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        Pattern p = Pattern.compile("^[\\_\\.][0-9]+[a-zA-Z]*[\\_]?$");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            Matcher m = p.matcher(s);
            sb.append( m.find() ? "VALID\n" : "INVALID\n" );
        }
        sc.close();
        System.out.print(sb.toString());
    }
}

 


 

  Detect HTML Tags

https://www.hackerrank.com/challenges/detect-html-tags/problem?isFullScreen=true 

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        Pattern p = Pattern.compile("(\\<(\\w+)[.[^\\>]]*\\/?\\>)");
        TreeSet<String> r = new TreeSet<>(); // result
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            Matcher m = p.matcher(s);
            while (m.find()) {
                r.add(m.group(2));
            }
        }
        sc.close();
        System.out.println(String.join(";", r));
        // [way 1]
        /*StringBuilder sb = new StringBuilder();
        sb.append(r.pollFirst());
        for (String t : r)
            sb.append(";" + t);
        System.out.println(sb.toString());*/
        // [way 2]
        System.out.println(String.join(";", r));
        // [way 3]
        /*System.out.println(r.stream().collect(Collectors.joining(";")));*/
    }
}

 

( java string concat comma )

https://stackoverflow.com/questions/668952/the-simplest-way-to-comma-delimit-a-list
https://stackoverflow.com/questions/205555/the-most-sophisticated-way-for-creating-comma-separated-strings-from-a-collectio 
https://mkyong.com/java/java-how-to-join-list-string-with-commas/ 

 


 

■  Detect HTML Attributes

https://www.hackerrank.com/challenges/html-attributes/problem?isFullScreen=true 

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        Pattern p1 = Pattern.compile("(\\<(\\w+)[.[^\\>]]*\\/?\\>)");
        Pattern p2 = Pattern.compile("([\\w\\-]+)\\=[\\\"\\']([.[^\\\"\\']]*)[\\\"\\']");
        Map<String,TreeSet<String>> r = new TreeMap<>(); // result
        for (int i = 0; i < n; ++i) {
            String s = sc.nextLine();
            Matcher m1 = p1.matcher(s);
            while (m1.find()) {
                String l = m1.group(1);    // line
                String t = m1.group(2);    // tag
                Matcher m2 = p2.matcher(l);
                if (!r.containsKey(t)) {
                    r.put(t, new TreeSet<>());
                }
                while (m2.find()) {
                    String a = m2.group(1);    // attribute
                    r.get(t).add(a);
                }
            }
        }
        sc.close();
        //System.out.println(r);
        StringBuilder sb = new StringBuilder();
        for (String k : r.keySet()) {
            sb.append( k + ":");
            TreeSet<String> vs = r.get(k);	// values
            if (vs.size() > 0) {
                sb.append(vs.pollFirst());	// value_first
                for (String v : vs) {
                    sb.append("," + v);
                }
            }
            sb.append("\n");
        }
        System.out.print(sb.toString());
    }
}

 


 

■  Build a Stack Exchange Scraper

https://www.hackerrank.com/challenges/stack-exchange-scraper/problem?isFullScreen=true 

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = "";
        while ( sc.hasNextLine() ) { // sc.hasNext()
            s += sc.nextLine();
        }
        sc.close();
        List<String> r1 = new ArrayList<>();
        List<String> r2 = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        Matcher m = Pattern.compile("(\\<div\\s+class\\s*\\=\\s*\\\"summary\\\"\\>\\s*\\<h3\\>\\s*\\<a\\s+href\\s*\\=\\s*\\\"\\/questions\\/)(\\d+)(\\/[\\w\\-]+\\\"\\s+class\\s*\\=\\s*\\\"question\\-hyperlink\\\"\\s*\\>\\s*)([\\w\\s~!@\\#$%^&*()\\-_=+,./?:;\\\"'{}|\\[\\]\\\\]+)(\\s*\\<\\/a\\>\\s*\\<\\/h3\\>)").matcher(s);
        while ( m.find() ) {
            r1.add( m.group(2) + ";" + m.group(4) + ";" );
        }
        m = Pattern.compile("(asked\\s*\\<span\\s+title\\s*\\=\\s*\\\"[\\s\\w\\-\\:]+\\\"\\s+class\\s*\\=\\s*\\\"relativetime\\\"\\s*\\>\\s*)([\\w\\s\\-\\:]+)(?=\\s*\\<\\/span\\>)").matcher(s);
        while ( m.find() ) {
            r2.add( m.group(2) + "\n" );
        }
        int c = r1.size();
        for ( int i = 0; i < c; ++i ) {
            sb.append( r1.get(i) + r2.get(i) );
        }
        System.out.print( sb.toString() );
    }
}

( .. 그냥 나눠서 하자 )

(+)

import java.util.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Pattern p1 = Pattern.compile("<a href=\\\"/questions/([0-9]+).*>(.*)</a>");
        Pattern p2 = Pattern.compile("<span.*class=\\\"relativetime\\\">(.*)</span>");
        List<String> r_id = new ArrayList<>();
        List<String> r_ques = new ArrayList<>();
        List<String> r_time = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        while ( sc.hasNext() ) {
            String s = sc.nextLine();
            Matcher m = p1.matcher(s);
            while (m.find()) {
                m.groupCount();
                r_id.add(m.group(1));
                r_ques.add(m.group(2));
            }
            Matcher m2 = p2.matcher(s);
            while (m2.find()) {
                m2.groupCount();
                r_time.add(m2.group(1));
            }
        }
        sc.close();
        int c = r_id.size();
        for (int i = 0; i < c; ++i) {
            sb.append( r_id.get(i) + ";" + r_ques.get(i) + ";" + r_time.get(i) + "\n" );
        }
        System.out.print(sb.toString());
    }
}

 


 

반응형