Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Comparator;
- public class Main {
- public static void main(String[] args) {
- String str1 = "abcdefdqwdqwdqwdvadawfg";
- String str2 = "xzabcdkldawdqdqvfvdlokojqdj";
- String shorterStr = "";
- String longerStr = "";
- if(str1.length() > str2.length()){
- longerStr = str1;
- shorterStr = str2;
- }
- else {
- shorterStr = str1;
- longerStr = str2;
- }
- System.out.println("Shorter: " + shorterStr);
- System.out.println("Longer: " + longerStr);
- char[] shorterChArr = shorterStr.toCharArray();
- char[] longerChArr = longerStr.toCharArray();
- ArrayList<Integer> longestSeq = new ArrayList<Integer>();
- int counter = 1;
- for (int i = 0; i < shorterChArr.length - 1; i++) {
- for (int j = 0; j < longerChArr.length - 1; j++) {
- if(shorterChArr[i] == longerChArr[j]){
- boolean hasNext = shorterChArr[i + 1] == longerChArr[j + 1];
- if(hasNext) counter++;
- else {
- longestSeq.add(counter);
- }
- }
- }
- }
- System.out.println(longestSeq);
- System.out.println(longestSeq.stream().max(Comparator.comparing(Integer::valueOf)).get());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement