I am using HashMap() for the problem but am facing issues with regards to order and occurrence of characters in output.
I tried to reverse the String builder both while iteration and after StringBuilder was created, still face another issues.
int l1 = inputStr1.length();
int l2 = inputStr2.length();
StringBuilder mkr = new StringBuilder();
HashMap<Character, Integer> res = new HashMap<>();
for (int i = 0; i < l1; i++) {
res.put(inputStr1.charAt(i),i);
}
for (int j = 0; j < l2; j++) {
if (res.containsKey(inputStr2.charAt(j))){
mkr.append(inputStr2.charAt(j));
}
}
mkr = mkr.reverse(); // Code only used in Test Scenario - 2
String result = mkr.toString();
if(result == null){return null;}
return result;
Test Scenario 1 - Input String 1 : Hello Input String 2 : world Expected output is: lo Actual output generated by my code: ol
Test Scenario 2 - [After reversing the StringBuilder] Input String 1: hi you are good Input String 2 : hi man Expected output is: hi a Actual output generated by my code: a ih