2
public String largestMerge(String word1, String word2) {3
StringBuilder sb = new StringBuilder();6
char[] w1 = word1.toCharArray();7
char[] w2 = word2.toCharArray();12
// we loop until we exhaust any one of the 2 words completely13
while (i < n1 && j < n2) {15
// if both the characters are equal we choose the one where the next largest occurs earlier17
if (check(w1, i, w2, j)) {24
// else we greedily choose the largest of the two characters25
else if (w1[i] > w2[j]) {32
// at the end of the loop we append any remaining word33
sb.append(word1.substring(i));34
sb.append(word2.substring(j));39
private boolean check(char[] w1, int i, char[] w2, int j) {40
// will return true if we need to extract from word1 and false if we need to extract from word242
while (i < w1.length && j < w2.length) {46
} else if (w1[i] > w2[j]) {53
// if we are unable to find any exhaustable character till the end of the loop we use the one54
// having larger length