■ Valid PAN format
https://www.hackerrank.com/challenges/valid-pan-format/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("^[A-Z]{5}[0-9]{4}[A-Z]$");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; ++i) {
sb.append( p.matcher(sc.nextLine()).find() ? "YES\n" : "NO\n" );
}
sc.close();
System.out.print(sb.toString());
}
}
■ HackerRank Language
https://www.hackerrank.com/challenges/hackerrank-language/problem?isFullScreen=true
import java.util.*;
public class Solution {
public static void main(String[] args) {
String l[] = "C:CPP:JAVA:PYTHON:PERL:PHP:RUBY:CSHARP:HASKELL:CLOJURE:BASH:SCALA: ERLANG:CLISP:LUA:BRAINFUCK:JAVASCRIPT:GO:D:OCAML:R:PASCAL:SBCL:DART: GROOVY:OBJECTIVEC"
.split(" *: *");
List<String> ls = new ArrayList<>();
for (String ll : l) {
ls.add(ll);
}
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().toUpperCase().split(" ")[1];
sb.append(ls.contains(s) ? "VALID\n" : "INVALID\n");
}
sc.close();
System.out.print(sb.toString());
}
}
■ IP Address Validation
https://www.hackerrank.com/challenges/ip-address-validation/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);
int n = Integer.valueOf(sc.nextLine());
Pattern p1 = Pattern.compile("^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$");
Pattern p2 = Pattern.compile("^[0-9a-fA-F]{1,4}\\:[0-9a-fA-F]{1,4}\\:[0-9a-fA-F]{1,4}\\:[0-9a-fA-F]{1,4}\\:[0-9a-fA-F]{1,4}\\:[0-9a-fA-F]{1,4}\\:[0-9a-fA-F]{1,4}\\:[0-9a-fA-F]{1,4}$");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; ++i) {
String s = sc.nextLine();
Matcher m1 = p1.matcher(s);
Matcher m2 = p2.matcher(s);
String r = "";
if (m1.find()) {
boolean v = true;
for (int j = 1; j <= 4; ++j) {
int t = Integer.valueOf(m1.group(j));
if (t < 0 || t > 255) {
v = false;
break;
}
}
r = v ? "IPv4\n" : "Neither\n";
} else if (m2.find()) {
r = "IPv6\n";
} else {
r = "Neither\n";
}
sb.append(r);
}
sc.close();
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);
int n = Integer.valueOf(sc.nextLine());
Pattern p1 = Pattern.compile("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");
Pattern p2 = Pattern.compile("(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; ++i) {
String s = sc.nextLine();
Matcher m1 = p1.matcher(s);
Matcher m2 = p2.matcher(s);
String r = "";
if (m1.matches()) {
r = "IPv4\n";
} else if (m2.matches()) {
r = "IPv6\n";
} else {
r = "Neither\n";
}
sb.append(r);
}
sc.close();
System.out.print(sb.toString());
}
}
( Matcher 의 find() 와 matches() 의 차이 )
* find() : attempts to find the next subsequence of the input sequence that matches the pattern
* matches() : attempts to match the entire region against the pattern
■ The British and American Style of Spelling
https://www.hackerrank.com/challenges/uk-and-us/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 c = Integer.valueOf(sc.nextLine());
String l[] = new String[c];
for (int i = 0; i < c; ++i) {
l[i] = sc.nextLine();
}
int t = Integer.valueOf(sc.nextLine());
String w_a[] = new String[t];
for (int i = 0; i < t; ++i) {
w_a[i] = sc.nextLine();
}
sc.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; ++i) {
int r = 0;
String w = w_a[i];
Pattern p = Pattern.compile( w.substring(0, w.length()-2) + "(ze|se)");
for (int j = 0; j < c; ++j) {
Matcher m = p.matcher(l[j]);
while(m.find()) {
r++;
}
}
sb.append(r+"\n");
}
System.out.print(sb.toString());
}
}
■ UK and US: Part 2
https://www.hackerrank.com/challenges/uk-and-us-2/problem?isFullScreen=true
import java.util.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c = Integer.valueOf(sc.nextLine());
String l[] = new String[c];
for (int i = 0; i < c; ++i) {
l[i] = sc.nextLine();
}
int t = Integer.valueOf(sc.nextLine());
String w_a[] = new String[t];
for (int i = 0; i < t; ++i) {
w_a[i] = sc.nextLine();
}
sc.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; ++i) {
int r = 0;
String wa = w_a[i]; // w'
int len = wa.length();
int idx = wa.lastIndexOf("our");
Pattern p = Pattern.compile("(^|(?<=[\\s\\n]))" + wa.substring(0, idx) + "(our|or)" + wa.substring(idx + 3, len) + "($|(?=[\\s\\n]))");
for (int j = 0; j < c; ++j) {
Matcher m = p.matcher(l[j]);
while (m.find()) {
r++;
}
}
sb.append(r+"\n");
}
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);
int c = Integer.valueOf(sc.nextLine());
String st = "";
for (int i = 0; i < c; ++i) {
st += sc.nextLine() + " ";
}
String ss[] = st.split(" ");
int t = Integer.valueOf(sc.nextLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; ++i) {
String w = sc.nextLine();
int r = 0;
for (String s : ss) {
if (s.equals(w)) {
r++;
}
if (s.equals(w.replaceAll("our", "or"))) {
r++;
}
}
sb.append(r+"\n");
}
sc.close();
System.out.print(sb.toString());
}
}
■ Find A Sub-Word
https://www.hackerrank.com/challenges/find-substring/problem?isFullScreen=true
import java.util.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.valueOf(sc.nextLine());
String s[] = new String[n];
for (int i = 0; i < n; ++i) {
s[i] = sc.nextLine();
}
int q = Integer.valueOf(sc.nextLine());
String w[] = new String[q];
for (int i = 0; i < q; ++i) {
w[i] = sc.nextLine();
}
sc.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < q; ++i) {
int r = 0;
Pattern p = Pattern.compile("\\w+(" + w[i] + ")\\w+");
for (int j = 0; j < n; ++j) {
Matcher m = p.matcher(s[j]);
while (m.find()) {
r++;
}
}
sb.append(r+"\n");
}
System.out.print(sb.toString());
}
}
■ Find a Word
https://www.hackerrank.com/challenges/find-a-word/problem?isFullScreen=true
import java.util.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.valueOf(sc.nextLine());
String s[] = new String[n];
for (int i = 0; i < n; ++i) {
s[i] = sc.nextLine();
}
int q = Integer.valueOf(sc.nextLine());
String w[] = new String[q];
for (int i = 0; i < q; ++i) {
w[i] = sc.nextLine();
}
sc.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < q; ++i) {
int r = 0;
Pattern p = Pattern.compile("\\b("+ w[i] +")\\b");
for (int j = 0; j < n; ++j) {
Matcher m = p.matcher(s[j]);
while (m.find()) {
r++;
}
}
sb.append(r+"\n");
}
System.out.print(sb.toString());
}
}
( 문제에서 'boundary' 라는 단어에서 힌트를 얻음 )
■ Detect the Domain Name
https://www.hackerrank.com/challenges/detect-the-domain-name/problem?isFullScreen=true
import java.util.*;
import java.util.regex.*;
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("http[s]?://(ww[w2]\\.)?([a-zA-Z0-9\\-]+(\\.[a-zA-Z0-9\\-]+)+)");
TreeSet<String> r = new TreeSet<>();
for (int i = 0; i < n; ++i) {
Matcher m = p.matcher(sc.nextLine());
while (m.find()) {
r.add(m.group(2));
}
}
sc.close();
System.out.print(String.join(";", r));
}
}
( '\w' 는 'a to z', 'A to Z', '0 to 9' 뿐만 아니라 '_' 도 포함 )
■ Detect the Email Addresses
https://www.hackerrank.com/challenges/detect-the-email-addresses/problem?isFullScreen=true
import java.util.*;
import java.util.regex.*;
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\\.]+@\\w+(\\.\\w+)+");
TreeSet<String> r = new TreeSet<>();
for (int i = 0; i < n; ++i) {
Matcher m = p.matcher(sc.nextLine());
while (m.find()) {
r.add(m.group());
}
}
sc.close();
System.out.print(String.join(";", r));
}
}
( 문제에서 'There can be a number of strings separated by dots before and after the "@" symbol' )
■ Detect HTML links
https://www.hackerrank.com/challenges/detect-html-links/problem?isFullScreen=true
import java.util.*;
import java.util.regex.*;
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("<a([^<>]*)>(.*?)</a>"); // 추출
Pattern p2 = Pattern.compile("href=\"([^\"]*)\""); // 추출
Pattern p3 = Pattern.compile("<[^<>]+>"); // 제거
List<String> r = new ArrayList<>();
for (int i = 0; i < n; ++i) {
String s = sc.nextLine();
Matcher m1 = p1.matcher(s);
while (m1.find()) {
Matcher m2 = p2.matcher(m1.group(1));
while (m2.find()) {
String s1 = m2.group(1);
String s2 = m1.group(2);
Matcher m3 = p3.matcher(s2);
while (m3.find()) {
s2 = s2.replaceAll(m3.group(), "");
}
s2 = s2.trim();
r.add(s1 + "," + s2);
}
}
}
sc.close();
StringBuilder sb = new StringBuilder();
int c = r.size();
for (int i = 0; i < c; ++i) {
sb.append(r.get(i) + "\n");
}
System.out.print(sb.toString());
}
}
( 무조건 한꺼번에 나누려 하지 말고 부분적으로 하기 )
■ Building a Smart IDE: Identifying comments
https://www.hackerrank.com/challenges/ide-identifying-comments/problem?isFullScreen=true
import java.util.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder ss = new StringBuilder();
while (sc.hasNextLine()) { // sc.hasNext()
ss.append(sc.nextLine().trim() + "\n");
}
sc.close();
String s = ss.toString(); // "//.*|(\"(?:\\[^\"]|\"|.)*?\")|(?s)/\*.*?\*/"
Pattern p = Pattern.compile("(//.*)|(/\\*((?!\\*/).|\\s)*\\*/)");
Matcher m = p.matcher(s);
StringBuilder sb = new StringBuilder();
while (m.find()) {
sb.append(m.group() + "\n");
}
System.out.print(sb.toString());
}
}
■ Building a Smart IDE: Programming Language Detection
https://www.hackerrank.com/challenges/programming-language-detection/problem?isFullScreen=true
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder ss = new StringBuilder();
while (sc.hasNextLine()) { // sc.hasNext()
ss.append(sc.nextLine() + "\n");
}
sc.close();
String s = ss.toString().replaceAll("(\\/\\/.*)|(\\/\\*((?!\\*\\/).|\\s)*\\*\\/)|(#(?!include|define).*)", "");
String [] nm_arr = {"Java", "C", "Python"};
String [] p_arr = {"\\bSystem\\.(out|err)\\.print(f|ln*)\\(", "\\s+printf\\(", "\\bprint[\\(| ]"};
int n = nm_arr.length;
loop:
for (int i = 0; i < n; ++i) {
Pattern p = Pattern.compile(p_arr[i]);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(nm_arr[i]);
break loop;
}
}
}
}
'코딩 문제 풀기 ( Algorithm problem solving ) > 해커랭크 ( HackerRank )' 카테고리의 다른 글
[HackerRank][Regex](java) Applications 1 (0) | 2023.04.09 |
---|---|
[HackerRank][Regex](java) Assertions (0) | 2023.04.09 |
[HackerRank][Regex](java) Backreferences (0) | 2023.04.09 |
[HackerRank][Regex](java) Grouping and Capturing (0) | 2023.04.09 |
[HackerRank][Regex](java) Repetitions (0) | 2023.04.09 |