Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. package pl.zawierucha.main;
  2.  
  3. public class SolutionLIS {
  4.  
  5. public static int solution(String S) {
  6. return LIS(S, 0, Integer.MIN_VALUE);
  7. }
  8.  
  9. public static int LIS(String S, int i, int prev) {
  10. // Base case: nothing is remaining; starting index = len of string
  11. if (i == S.length()) {
  12. return 0;
  13. }
  14.  
  15. // case 1: exclude the current element and process the
  16. // remaining elements; we start with the next letter
  17. int excl = LIS(S, i + 1, prev);
  18.  
  19. // case 2: include the current element if it is greater
  20. // than previous element in LIS
  21. int incl = 0;
  22. if (S.charAt(i) >= prev) {
  23. incl = 1 + LIS(S, i+1, S.charAt(i));
  24. }
  25.  
  26. return Integer.max(incl, excl);
  27. }
  28.  
  29. public static void main(String[] args)
  30. {
  31. String A = "banana";
  32. System.out.print("Length of LIS is " + solution(A));
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement