Java Pattern Syntax Checker - Hacker Rank Solution

Java Pattern Syntax Checker - Hacker Rank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java Pattern Syntax Checker Problem with a very easy explanation. This is the 20th problem of Java on HackerRank. In this article, you will get more than one approach to solving this problem. So let's start-

{tocify} $title={Table of Contents}

Java Pattern Syntax Checker - Hacker Rank Solution

HackerRank Java Pattern Syntax Checker - Problem Statement

Using Regex, we can easily match or search for patterns in a text. Before searching for a pattern, we have to specify one using some well-defined syntax.

In this problem, you are given a pattern. You have to check whether the syntax of the given pattern is valid.

Note: In this problem, a regex is only valid if you can compile it using the Pattern.compile method.

Input Format

The first line of input contains an integer N, denoting the number of test cases. The next N lines contain a string of any printable characters representing the pattern of a regex.

Output Format

For each test case, print Valid if the syntax of the given pattern is correct. Otherwise, print Invalid. Do not print the quotes.

Sample Input

3 ([A-Z])(.+) [AZ[a-z](a-z) batcatpat(nat {codeBox}

Sample Output

Valid Invalid Invalid {codeBox}

Java Pattern Syntax Checker - Hacker Rank Solution

Approach I:

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int testCases = scan.nextInt();
        scan.nextLine(); 
        while (testCases-- > 0) {
           String pattern = scan.nextLine();
           try {
               Pattern.compile(pattern);
               System.out.println("Valid");
           } catch (PatternSyntaxException exception) {
               System.out.println("Invalid");
           }
        }
        scan.close();
    }
}


Approach II:

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

public class Solution
{
   public static void main(String[] args){
      Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
    
        while(testCases>0){
            String pattern = in.nextLine();

            try {
                Pattern.compile(pattern);
                System.out.println("Valid");
            } catch(PatternSyntaxException e) {
                System.out.println("Invalid");
            }

            testCases--;
        }
   }
}

Also Read:

Disclaimer: The above Problem ( Java Pattern Syntax Checker ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these two solutions will pass all the test cases. Now visit Java Pattern Syntax Checker HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post