Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public static ArrayList<Character> getLongestCommonSubstring(char[] array1, char[] array2) {
  2. ArrayList<Character> charArray = new ArrayList<>();
  3. ArrayList<Character> tempArray = new ArrayList<>();
  4. boolean canAddNextCharacter = false;
  5. int index = 0;
  6. for (int i = 0; i < array1.length; i ++) {
  7. for(int j = index; j < array2.length; j++) {
  8. if(array1[i] != array2[j]) {
  9. continue;
  10. }
  11.  
  12. if(array1[i] == array2[j]) {
  13. charArray.add(array1[i]);
  14. canAddNextCharacter = ((j + 1) < array2.length - 1) && ((i+1) < array1.length -1) && (array1[i+1] == array2[j+1]);
  15. break;
  16. }
  17. canAddNextCharacter = false;
  18. }
  19.  
  20. if(charArray.size() > tempArray.size()) {
  21. tempArray = charArray;
  22. }
  23.  
  24. if(!canAddNextCharacter) {
  25. charArray = new ArrayList<>();
  26. index = 0;
  27. }
  28. }
  29. return tempArray;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement