fork(2) download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. static int count;
  5. static String s1 = "CDGI"; // you can try with "EEE"
  6. static String s2 = "ABCDEFGHI"; // you can try with "EEE"
  7. public static void main(String[] args) {
  8.  
  9. LinkedList<Integer> ll = new LinkedList<>();
  10. for(int i =0; i<s1.length();i++){
  11. int t = i;
  12. count = 0;
  13. for (int j=0; j<s2.length();j++){
  14. if(s1.charAt(t)==s2.charAt(j)){
  15. count++;
  16. doIt(t+1,j+1);
  17. break ;
  18. }
  19. }
  20. ll.add(count);
  21. }
  22. System.out.println(ll); // printing the whole LinkedList
  23. System.out.println(Collections.max(ll)); // taking the maximum value
  24. }
  25. public static void doIt(int a, int b){
  26.  
  27. for ( ; a<s1.length(); a++){
  28. int t = b ;
  29. for (; t<s2.length(); t++){
  30. if (s1.charAt(a) == s2.charAt(t)){
  31. count++;
  32. b++;
  33. break ;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. }
  40.  
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
[4, 3, 2, 1]
4