Negative Lookahead – HackerRank Solution

In this post, we will solve Negative Lookahead HackerRank Solution. This problem (Negative Lookahead) is a part of HackerRank Regex series.


The negative lookahead (?!) asserts regex_1 not to be immediately followed by regex_2. Lookahead is excluded from the match (do not consume matches of regex_2), but only assert whether a match is possible or not.

Solution – Negative Lookahead – HackerRank Solution


Regex_Pattern = r'(.)(?!\1)'

import re

Test_String = input()

match = re.findall(Regex_Pattern, Test_String)

print("Number of matches :", len(match))

Note: This problem (Negative Lookahead) is generated by HackerRank 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. Required fields are marked *