Friday, November 29, 2019

Group(), Groups() & Groupdict() - Hacker Rank Solution

Group(), Groups() & Groupdict() - Hacker Rank Solution



[A-Za-z0-9])\1+ can be used to solve this problem.
The expression ([A-Za-z0-9]) will match an alphanumeric character and store it in group .
The expression \1 matches the exact same text that was matched by the first capturing group.

import re
m = re.search(r'([A-Za-z0-9])\1+',raw_input())
if m:
    print m.group(1)
else:
    print -1

3 comments:

  1. import re
    m = re.search(r'([a-zA-Z0-9])\1+', input().strip())
    print(m.group(1) if m else -1)

    ReplyDelete
  2. #To work in python 3
    import re
    m = re.search(r'([A-Za-z0-9])\1+',input())
    if m:
    print(m.group(1))
    else:
    print(-1)

    ReplyDelete

Powered by Blogger.