HackerRank Forward References Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Forward References Solution. This problem is a part of the Regex HackerRank Series.

HackerRank Forward References Solution
HackerRank Forward References Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Forward References Solution

NOTE – Forward reference is supported by JGsoft, .NET, Java, Perl, PCRE, PHP, Delphi and Ruby regex flavors.

Forward reference creates a back reference to a regex that would appear later.
Forward references are only useful if they’re inside a repeated group.
Then there may arise a case in which the regex engine evaluates the backreference after the group has been matched already.


You have a test string S.
Your task is to write a regex which will match S, with following condition(s):

  • S consists of tic or tac.
  • tic should not be immediate neighbour of itself.
  • The first tic must occur only when tac has appeared at least twice before.

Valid S


Invalid S



This is a regex only challenge. You are not required to write any code.
You only have to fill the regex pattern in the blank (_________).

HackerRank Forward References Solutions in Cpp

HackerRank Forward References Solutions in Java

public class Solution {    
    public static void main(String[] args) {
        Regex_Test tester = new Regex_Test();
        tester.checker("^(tac|(tac){2,}(tic(tac)+)*(tic)?)$"); // Use \\ instead of using \ 

HackerRank Forward References Solutions in Python

HackerRank Forward References Solutions in JavaScript

HackerRank Forward References Solutions in PHP

$Regex_Pattern = '/^(\2tic(?!tic)|(tac))+$/'; //Do not delete '/'. Replace __________ with your regex.  

Disclaimer: This problem (Forward References) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Positive Lookbehind Solution

Leave a Reply

Your email address will not be published. Required fields are marked *