Guest User

Untitled

a guest
Mar 18th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. /*
  2. **
  3. *
  4. * Google Telephonic Round 1
  5. *
  6. * Given two strings - S1 and S2.
  7. * Arrange the characters of S1 in same alphabetical order as the characters of S2.
  8. * If a character of S1 is not present in S2 - such characters should come at the end of
  9. * the result string, but make sure to retain the order of such characters
  10. * Case sensitivity is irrelevant
  11. * e.g. S1 = "Google", S2 = "dog"
  12. * Output = "ooggle"
  13. *
  14. * e.g. S1 = "abcdedadf", S2 = "cae"
  15. * Output = "caaebdddf"
  16. *
  17. */
  18.  
  19. tring function(String s1, String s2) {
  20. if (s1 == null || s2 == null)
  21. return ''
  22. if (s1.isEmpty() || s2.isEmpty() || s1.length() == 1)
  23. return s1.toLowerCase()
  24.  
  25. LinkedList<String> listOfStrings = Arrays.asList(s1.split(''))
  26. ArrayList<String> templateString = s2.split('')
  27. int outsideIterator = 0
  28.  
  29. for (String ts in templateString) {
  30. int i = outsideIterator
  31.  
  32. for (i; i < listOfStrings.size(); i++) {
  33. if (listOfStrings[i].equalsIgnoreCase(ts)) {
  34. if (i != outsideIterator) {
  35. listOfStrings.add(outsideIterator, listOfStrings.remove(i))
  36. }
  37. outsideIterator++
  38. }
  39. }
  40. }
  41. listOfStrings.join().toLowerCase()
  42. }
Add Comment
Please, Sign In to add comment