BC104 - Editorial

#PROBLEM LINK:

Practice
Contest

Author: Ayush Nagal

#DIFFICULTY:
EASY

#PREREQUISITES:
String

#PROBLEM:
Given a string s, containing only A and B characters. We have to change it into a string such that there are no matching adjacent characters. To do this, we are allowed to delete zero or more characters in the string.

#EXPLANATION:
We need to run two nested loops as shown below:

for(int i=0;i<l;i++)
{
  f=0;
  for(int j=i+1;j<l;j++)
  {
     if(s[i]==s[j])
     f++;
     else
     break;
  }
  c+=f;
  if(f!=0)
  i+=f;
 }

Here, i is used to fix one character while j is used to check for character matching with i. f is used to store the frequency of the number of same characters. c is used to store the total number of characters to be deleted.

#AUTHOR’S SOLUTION:
Author’s solution can be found here.

4 Likes

Can we solve this using recursion?