Pattern Syntax Checker

Sort by

recency

|

396 Discussions

|

  • + 0 comments
    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 exc) {
                    System.out.println("Invalid");
                }
                
                testCases--;
    		}
            
            in.close();
    	}
    }
    
  • + 0 comments

    Here is Pattern Syntax Checker solution in Java - https://programmingoneonone.com/hackerrank-pattern-syntax-checker-solution-in-java.html

  • + 0 comments
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int cnt = scan.nextInt();
        scan.nextLine();
        for (int i = 0; i < cnt; i++){
            try{
                Pattern.compile(scan.nextLine());
            } catch (Exception e){
                System.out.println("Invalid");
                continue;
            }
            System.out.println("Valid");
        }
        scan.close();
    }
    
  • + 0 comments

    Java 8

    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();
              	//Write your code
                try{
                    Pattern p = Pattern.compile(pattern);
                    System.out.println("Valid");
                }catch(Exception e){
                    System.out.println("Invalid");
                }
                testCases--;
                
    		}
    	}
    }
    
  • + 0 comments
    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();
              	//Write your code
                try{
                Pattern pattern2 = Pattern.compile(pattern);
                System.out.println("Valid");
                }catch (PatternSyntaxException e) {  
                    System.out.println("Invalid");
                }
                testCases--; 
    		}
    	}