ENIGMA06 - Editorial

PROBLEM LINK:

Shyam and Raju

Author and Editorialist : Dhruv Mahajan

DIFFICULTY:

Simple

PREREQUISITES:

DP

EXPLANATION:

Store the occurance of A,B,C in a dp array.

Let,
Ai = Number of ‘A’s in S between the indexes 1 and i (inclusive).
Bi = Number of ‘B’s in S between the indexes 1 and i (inclusive).
Ci = Number of ‘C’s in S between the indexes 1 and i (inclusive).

Let’s consider the substring Sj…i :
Number of ‘A’-s in that substring = Ai - Aj-1
Number of ‘B’-s in that substring = Bi - Bj-1
Number of ‘C’-s in that substring = Ci - Cj-1

So for that substring to be good: Ai - Aj-1 = Bi - Bj-1 = Ci - Cj-1

Alternatively the following two conditions are enough for that substring to be good:

Ai - Bi = Aj-1 - Bj-1
Ai - Ci = Aj-1 - Cj-1

Find all the subtrings in two loops match the occurance of A,B,C from the dp array’s.

SOLUTION:

Link to Solution