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}  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. 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. For each test case, print Valid if the syntax of the given pattern is correct. Otherwise, print Invalid. Do not print the quotes.

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

Valid Invalid Invalid {codeBox}

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--;
        }
   }
}

Python Find Angle MBC HackerRank Solution Java String Reverse HackerRank Solution Java BigDecimal HackerRank Solution Java String Tokens – HackerRank Solution 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.

No Comments

Sorry, the comment form is closed at this time.