×

# ENIGMA06 - Editorial

Shyam and Raju

Author and Editorialist : Dhruv Mahajan

Simple

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:

This question is marked "community wiki".

464
accept rate: 10%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,643
×2,168
×1,173
×42

question asked: 16 Oct '15, 19:14

question was seen: 749 times

last updated: 16 Oct '15, 23:26