HackerRank Group() Groups() & Groupdict() solution in python YASH PAL, 31 July 202417 January 2026 HackerRank Group() Groups() & Groupdict() solution in Python – In this Group(), Groups(), & Groupdict() problem, 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.group()A group() expression returns one or more subgroups of the match.Code>>> import re >>> m = re.match(r'(\w+)@(\w+)\.(\w+)','username@hackerrank.com') >>> m.group(0) # The entire match 'username@hackerrank.com' >>> m.group(1) # The first parenthesized subgroup. 'username' >>> m.group(2) # The second parenthesized subgroup. 'hackerrank' >>> m.group(3) # The third parenthesized subgroup. 'com' >>> m.group(1,2,3) # Multiple arguments give us a tuple. ('username', 'hackerrank', 'com') groups()A groups() expression returns a tuple containing all the subgroups of the match.Code>>> import re >>> m = re.match(r'(\w+)@(\w+)\.(\w+)','username@hackerrank.com') >>> m.groups() ('username', 'hackerrank', 'com') groupdict()A groupdict() expression returns a dictionary containing all the named subgroups of the match, keyed by the subgroup name.Code>>> m = re.match(r'(?P<user>\w+)@(?P<website>\w+)\.(?P<extension>\w+)','myname@hackerrank.com') >>> m.groupdict() {'website': 'hackerrank', 'user': 'myname', 'extension': 'com'}HackerRank Group() Groups() & Groupdict() solution in Python 2.from __future__ import print_function import re r=re.search(r'([0-9a-zA-Z])1',raw_input()) print(r.group(1) if r else -1)Group() Groups() & Groupdict() solution in Python 3.# Enter your code here. Read input from STDIN. Print output to STDOUT import re m = re.findall(r"([A-Za-z0-9])1+",input()) if m: print(m[0]) else: print(-1)Problem solution in pypy programming.# Enter your code here. Read input from STDIN. Print output to STDOUT import re s=raw_input() m=re.search(r'([a-z0-9])1+',s) if m is None: print -1 else: print m.group(0)[1]Problem solution in pypy3 programming.# Enter your code here. Read input from STDIN. Print output to STDOUT import re m = re.search(r'([a-zA-Z0-9])1', input().strip()) print(m.group(1) if m else -1) coding problems solutions Hackerrank Problems Solutions Python Solutions HackerRankPython