Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- **
- *
- * Google Telephonic Round 1
- *
- * Given two strings - S1 and S2.
- * Arrange the characters of S1 in same alphabetical order as the characters of S2.
- * If a character of S1 is not present in S2 - such characters should come at the end of
- * the result string, but make sure to retain the order of such characters
- * Case sensitivity is irrelevant
- * e.g. S1 = "Google", S2 = "dog"
- * Output = "ooggle"
- *
- * e.g. S1 = "abcdedadf", S2 = "cae"
- * Output = "caaebdddf"
- *
- */
- tring function(String s1, String s2) {
- if (s1 == null || s2 == null)
- return ''
- if (s1.isEmpty() || s2.isEmpty() || s1.length() == 1)
- return s1.toLowerCase()
- LinkedList<String> listOfStrings = Arrays.asList(s1.split(''))
- ArrayList<String> templateString = s2.split('')
- int outsideIterator = 0
- for (String ts in templateString) {
- int i = outsideIterator
- for (i; i < listOfStrings.size(); i++) {
- if (listOfStrings[i].equalsIgnoreCase(ts)) {
- if (i != outsideIterator) {
- listOfStrings.add(outsideIterator, listOfStrings.remove(i))
- }
- outsideIterator++
- }
- }
- }
- listOfStrings.join().toLowerCase()
- }
Add Comment
Please, Sign In to add comment