Array ( [0] => [1] => questions [2] => Codevita-Previous-Questions [3] => Swayamvar ) Codevita Previous Questions | Swayamvar | THE INQUISITIVE





Swayamvar

LEVEL:Beginner

Description

A ceremony where a Bride chooses her Groom from an array of eligible bachelors is called Swayamvar. But this is a Swayamvar with difference. An array of Bride-to-be will choose from an array of Groom-to-be.

The arrangement at this Swayamvar is as follows

Brides-to-be are organized such that the most eligible bachelorette will get first chance to choose her Groom. Only then, the next most eligible bachelorette will get a chance to choose her Groom

If the initial most eligible bachelorette does not get a Groom of her choice, none of the Brides-to-be have any chance at all to get married. So unless a senior bachelorette is out of the queue, the junior bachelorette does not get a chance to select her Groom-to-be

Inital state of Grooms-to-be is such that most eligible bachelor is at the head of the queue. The next most eligible bachelor is next in the queue. So on and so forth.

Now everything hinges on the choice of the bachelorette. The most eligible bachelorette will now meet the most eligible bachelor.

If bachelorette selects the bachelor, both, the bachelorette and the bachelor are now Bride and Groom respectively and will no longer be a part of the Swayamvar activity. Now, the next most eligible bachelorette will get a chance to choose her Groom. Her first option is the next most eligible bachelor (relative to initial state)

If the most eligible bachelorette passes the most eligible bachelor, the most eligible bachelor now moves to the end of the queue. The next most eligible bachelor is now considered by the most eligible bachelorette. Selection or Passing over will have the same consequences as explained earlier.

If most eligible bachelorette reaches the end of bachelor queue, then the Swayamvar is over. Nobody can get married.

Given a mix of Selection or Passing over, different pairs will get formed.

The selection criteria is as follows

1. Each person either drinks rum or mojito. Bride will choose groom only if he drinks the same drink as her.

Note : There are equal number of brides and grooms for the swayamvar.

Tyrion as the hand of the king wants to know how many pairs will be left unmatched. Can you help Tyrion?

Input Format

First line contains one integer N, which denotes the number of brides and grooms taking part in the swayamvar. Second line contains a string in lowercase, of length N containing initial state of brides-to-be. Third line contains a string in lowercase, of length N containing initial state of grooms-to-be. Each string contains only lowercase 'r' and 'm' stating person at that index drinks "rum"(for 'r') or mojito(for 'm').

Output Format

Output a single integer denoting the number of pairs left unmatched.


Example 1:

Input
4
rrmm
mmrr
Output
0
Example 2:

Input
5
rrrmm
mrmrm
Output
3
Example 3:

Input
2
mm
rr
Output
2

oops

Login to see Discussion




Approach

Step-i) create a method no_of_pairs which takes bride , groom and n as argument
Step-ii)initialize flag to False and answer to 0
Step-iii)run a infinite loop follow below steps
Step-iv)intialize count to 0
Step-v)if length of bride list is 0 break the loop
Step-vi)if first element of bride and groom are not equal increase count by one value pop first element of groom and append to last

Step-vii)if count equal to n assign True to flag
Step-viii) if flag is True break the loop
Step-ix)repeat step vi to viii till first element of bride and groom are not equal
Step-x) if first element of both the list are equal remove the first element from both list
Step-xi)if flag equal to True break the loop
Step-xii)now increment the answer by one value

Step-xiii) if answer equal to n return 0 else return n-answer


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution