Group(), Groups() & Groupdict() in Python | HackerRank Solution

Hello coders, today we are going to solve Group(), Groups() & Groupdict() HackerRank Solution in Python.

Group(), Groups() & Groupdict()


group() expression returns one or more subgroups of the match.


>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','[email protected]')
>>>       # The entire match 
'[email protected]'
>>>       # The first parenthesized subgroup.
>>>       # The second parenthesized subgroup.
>>>       # The third parenthesized subgroup.
>>>,2,3)   # Multiple arguments give us a tuple.
('username', 'hackerrank', 'com')

groups() expression returns a tuple containing all the subgroups of the match.


>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','[email protected]')
>>> m.groups()
('username', 'hackerrank', 'com')

groupdict() expression returns a dictionary containing all the named subgroups of the match, keyed by the subgroup name.


>>> m = re.match(r'(?P<user>\w+)@(?P<website>\w+)\.(?P<extension>\w+)','[email protected]')
>>> m.groupdict()
{'website': 'hackerrank', 'user': 'myname', 'extension': 'com'}


You are given a string S.
Your task is to find the first occurrence of an alphanumeric character in S (read from left to right) that has consecutive repetitions.

Input Format

A single line of input containing the string S.


  • 0 < len(S) < 100

Output Format

Print the first occurrence of the repeating character. If there are no repeating characters, print -1.

Sample Input


Sample Output



.. is the first repeating character, but it is not alphanumeric.
1 is the first (from left to right) alphanumeric repeating character of the string in the substring 111.

Solution – Group(), Groups() & Groupdict() in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT

import re

m ='([a-zA-Z0-9])\1', input().strip())
print( if m else -1)

Disclaimer: The above Problem (Group(), Groups() & Groupdict()) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.