Matching Specific String

  • + 0 comments

    java solution

    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 scanner = new Scanner(System.in);
            Pattern pattern = Pattern.compile("hackerrank");
            int count = 0;
            while (scanner.hasNext()) {
                Matcher matcher = pattern.matcher(scanner.next());
                if (matcher.find()) count++;
            }
            System.out.println("Number of matches : " + count);
        }
    }