Check three or more consecutive identical characters or numbers

Given a string str, the task is to check whether the given string contains 3 or more consecutive identical characters/numbers or not by using Regular Expression.

Examples:

Input: str = “aaa”;
Output: true
Explanation:
The given string contains a, a, a which are consecutive identical characters.

Input: str = “abc”;
Output: false
Explanation:
The given string contains a, b, c which are not consecutive identical characters.

Input: str = “11”;
Output: false
Explanation:
The given string contains 1, 1 which are not 3 or more consecutive identical numbers.



Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.

  1. Get the String.
  2. Create a regular expression to check 3 or more consecutive identical characters or numbers as mentioned below:

    regex = “\\b([a-zA-Z0-9])\\1\\1+\\b”;

    Where:

    • \\b represents the word boundary.
    • ( represents the starting of the group 1.
    • [a-zA-Z0-9] represents a letter or a digit.
    • ) represents the ending of the group 1.
    • \\1 represents the same character as group 1.
    • \\1+ represents the same character as group 1 one or more times.
    • \\b represents the word boundary.
  3. Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
  4. Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check three or
// more consecutiveidentical
// characters or numbers
// using regular expression
  
import java.util.regex.*;
class GFG {
  
    // Function to check three or
    // more consecutiveidentical
    // characters or numbers
    // using regular expression
    public static boolean isIdentical(String str)
    {
        // Regex to check three or
        // more consecutive identical
        // characters or numbers
        String regex
            = "\\b([a-zA-Z0-9])\\1\\1+\\b";
  
        // Compile the ReGex
        Pattern p
            = Pattern.compile(regex);
  
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
  
        // Find match between given string
        // and regular expression
        // using Pattern.matcher()
        Matcher m = p.matcher(str);
  
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // Test Case 1:
        String str1 = "aaa";
        System.out.println(
            isIdentical(str1));
  
        // Test Case 2:
        String str2 = "11111";
        System.out.println(
            isIdentical(str2));
  
        // Test Case 3:
        String str3 = "aaab";
        System.out.println(
            isIdentical(str3));
  
        // Test Case 4:
        String str4 = "abc";
        System.out.println(
            isIdentical(str4));
  
        // Test Case 5:
        String str5 = "aa";
        System.out.println(
            isIdentical(str5));
    }
}

chevron_right


Output:

true
true
false
false
false

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.