Header Ad

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.

HackerRank Group(), Groups(), & Groupdict() solution in python


Problem solution in Python 2 programming.

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)



Problem solution in Python 3 programming.

# 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)


Post a Comment

0 Comments