morry2341

G7

Dec 11th, 2022
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. public class G7 {
  2.     public static void main(String[] args) {
  3.         System.out.println(lcs("testing123testing", "thisisatest"));
  4.         System.out.println(lcs("test", "thisisatest"));
  5.         System.out.println(lcs("testing", "sting"));
  6.         System.out.println(lcs("testing", "thisisasting"));
  7.     }
  8.  
  9.     static String lcs(String a, String b) {
  10.         if (a.length() > b.length())
  11.             return lcs(b, a);
  12.  
  13.         String res = "";
  14.         for (int ai = 0; ai < a.length(); ai++) {
  15.             for (int len = a.length() - ai; len > 0; len--) {
  16.  
  17.                 for (int bi = 0; bi <= b.length() - len; bi++) {
  18.  
  19.                     if (a.regionMatches(ai, b, bi, len) && len > res.length()) {
  20.                         res = a.substring(ai, ai + len);
  21.                     }
  22.                 }
  23.             }
  24.         }
  25.         return res;
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment