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.
Answers
Answer:
package Arrays;
import java.util.*;
public class Swayamvar {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
String BrideChoice = scn.next();
String GhroomChoice = scn.next();
int brLen = n;
for(int i = 0; i < n; i++) {
boolean condition = false;
int loop = brLen;
for(int j = 0; j < loop; j++) {
//removing character from string
if(BrideChoice.charAt(0) == GhroomChoice.charAt(j)) {
BrideChoice = charRemoveAt(BrideChoice, 0);
GhroomChoice = charRemoveAt(GhroomChoice, j);
brLen -= 1;
condition = true;
break;
}
}
if(condition == false) {
break;
}
}
System.out.println(brLen);
}
public static String charRemoveAt(String str, int p) {
// System.out.println(str.substring(0, p) + str.substring(p + 1));
return str.substring(0, p) + str.substring(p + 1);
}
}
Explanation: