×

# CDZ14A - Editorial

Author: sikander_nsit
Tester: sikander_nsit
Editorialist: sikander_nsit

SIMPLE-EASY

# PROBLEM:

Given a string consisting of three possible characters 'B', 'R' and 'G' find the length of the shortest substring such that it contains all three characters atleast once.

# EXPLANATION:

This was an implementation based problem. There are numerous ways to do it. One way is to traverse the string and for each position calculate the answer if the substring ended at the current position. For this we can keep three variables to keep track of the most recent position of each pill. At each position update the most recent position of the current pill as the current index and calculate the answer by subtracting from this the minimum of the three values.
PSEUDOCODE

    arr['R']=arr['G']=arr['B']=-10000000;
for(j=0;j<n;++j)
{
arr[str[j]]=j;
num=j-min(arr['R'],min(arr['G'],arr['B']))+1;
ans=min(ans,num);
}


# AUTHOR'S SOLUTION:

Author's solution can be found here.

This question is marked "community wiki".

1.5k82130
accept rate: 0%

19.8k350498541

 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,852
×1,191
×850
×10
×2

question asked: 11 Feb '14, 03:33

question was seen: 868 times

last updated: 18 Apr '17, 22:08