TCS CodeVita Problems and their Solution
Problem 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, i.e. this marriage is different kind of marriage. An array of Bridetobe will choose from an array of Groomtobe. The arrangement at this Swayamvar is as follows: Bridestobe 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 Bridestobe 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 Groomtobe. The Initial state of Groomstobe 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 and nobody can get married. Given a mix of Selection or Passing over, different pairs will get formed. The selection criteria are as follows: 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?Constraints 1<= N <= 10^4 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 bridestobe. Third line contains a string in lowercase, of length N containing initial state of groomstobe. Each string contains only lowercase 'r' and 'm' stating person at that index drinks "rum"(for 'r') or mojito(for 'm'). Output Output a single integer denoting the number of pairs left unmatched. Example 1 Input 4 Output 0 Explanation The bride at first place will only marry groom who drinks rum. So the groom at first place will join the end of the queue, as his drinking pattern "m " does not match with the drinking pattern of first bride " r ". Updated groom's queue is " r m r m ". Now the bride at first place will marry the groom at first place as drinking pattern of first bride " r " matches with first groom of updated groom list. Updated bride's queue is " r m m " and groom's queue is " m r m ", as once the pair get married, they both come out of the queue. Now the first bride of updated list has drinking pattern " r ", which does not match with the drinking pattern of first groom drinking pattern, hence updated groom queue is " r m m " , now the bride at first place will marry the groom at first place as drinking pattern of first bride " r " matches with first groom of updated groom list. Updated bride's queue is " m m " and groom's queue is " m m ", as once the pair get married, they both come out of the queue. The process continues and at last there are no pairs left. So, answer is 0 It shows that all the bride found suitable groom according to their own drinking pattern (either r  rum of bride matches with r rum of groom or m  mojito of bride matches with m  mojito of groom. Example 2 Input 4 Output 2 Explanation The bride at first place will only marry groom who drinks rum. So, the groom at first place will join the end of the queue, as his drinking pattern "m" does not match with the drinking pattern of first bride "r". Updated groom's queue is " m m r ". The bride at first place will only marry groom who drinks rum, So the groom at first place will join the end of the queue, as his drinking pattern "m" does not match with the drinking pattern of first bride "r ". Following the above process, 2 pairs will be left unmatched. Remember that bride will not move until she gets a groom of her choice. 2 shows that 2 brides do not find any suitable groom for them according to their own drinking pattern (either r  rum of bride matches with r rum of groom or m  mojito of bride matches with m  mojito of groom). Python Code of above Swayamvar problem:Understanding above python code for Swayamvar problem:EXAMPLE 1: Let us consider the explanation of example 1 and explain same using above code: Output: 0 shows that all the bride found suitable groom according to their own drinking pattern (either r  rum of bride matches with r rum of groom or m  mojito of bride matches with m  mojito of groom. EXAMPLE 2: Let us consider the explanation of example 2 and explain same using above code: 品色堂永远最新章节目录,品色堂永远无弹窗
品色堂永远最新章节目录,品色堂永远无弹窗
,夜关门之欲望之花最新章节,夜关门之欲望之花无弹窗
夜关门之欲望之花最新章节,夜关门之欲望之花无弹窗
Output: 2 shows that 2 brides do not find any suitable groom for them according to their own drinking pattern (either r  rum of bride matches with r rum of groom or m  mojito of bride matches with m  mojito of groom). IF you see for i = 2 bride list ( bride[ r, m, r, m,] ) value is r and remaining groom list was ( groom[m, m] ), as per the condition of question the least eligible bride will only get chance of finding groom, if most eligible has found, since bride at i = 2 place whose drinking pattern is r does not has any match in groom[m, m], therefore she will not get married because of which bride at i = 3 even though har drinking pattern is matching with groom[ m, m ] does not got the chance to marry, as the most eligible( bride at i = 2 ) is still un married.
Next TopicAngry Professor Problem Solution
