Given 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.
- Get the String.
- 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.
- Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isIdentical(string str)
{
const regex pattern( "\\b([a-zA-Z0-9])\\1\\1+\\b" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "aaa" ;
cout << isIdentical(str1) << endl;
string str2 = "11111" ;
cout << isIdentical(str2) << endl;
string str3 = "aaab" ;
cout << isIdentical(str3) << endl;
string str4 = "abc" ;
cout << isIdentical(str4) << endl;
string str5 = "aa" ;
cout << isIdentical(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isIdentical(String str)
{
String regex
= "\\b([a-zA-Z0-9])\\1\\1+\\b" ;
Pattern p
= Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "aaa" ;
System.out.println(
isIdentical(str1));
String str2 = "11111" ;
System.out.println(
isIdentical(str2));
String str3 = "aaab" ;
System.out.println(
isIdentical(str3));
String str4 = "abc" ;
System.out.println(
isIdentical(str4));
String str5 = "aa" ;
System.out.println(
isIdentical(str5));
}
}
|
Python3
import re
def isValidGUID( str ):
regex = "\\b([a-zA-Z0-9])\\1\\1+\\b"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "aaa"
print (isValidGUID(str1))
str2 = "11111"
print (isValidGUID(str2))
str3 = "aaab"
print (isValidGUID(str3))
str4 = "abc"
print (isValidGUID(str4))
str5 = "aa"
print (isValidGUID(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static bool isIdentical( string str)
{
string regex
= "\\b([a-zA-Z0-9])\\1\\1+\\b" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "aaa" ;
Console.WriteLine(
isIdentical(str1));
string str2 = "11111" ;
Console.WriteLine(
isIdentical(str2));
string str3 = "aaab" ;
Console.WriteLine(
isIdentical(str3));
string str4 = "abc" ;
Console.WriteLine(
isIdentical(str4));
string str5 = "aa" ;
Console.WriteLine(
isIdentical(str5));
}
}
|
Javascript
function isIdentical(str)
{
let pattern = new RegExp( "\\b([a-zA-Z0-9])\\1\\1+\\b" );
if (str== null )
{
return false ;
}
if (pattern.test(str)== true )
{
return true ;
}
else
{
return false ;
}
}
let str1 = "aaa" ;
console.log(isIdentical(str1)+ "<br>" );
let str2 = "11111" ;
console.log(isIdentical(str2)+ "<br>" );
let str3 = "aaab" ;
console.log(isIdentical(str3)+ "<br>" );
let str4 = "abc" ;
console.log(isIdentical(str4)+ "<br>" );
let str5 = "aa" ;
console.log(isIdentical(str5)+ "<br>" );
|
Output: true
true
false
false
false