Advertisement
Spocoman

01. Largest Common End

Oct 21st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         String[] arrOne = Arrays
  9.                 .stream(scanner.nextLine().split(" "))
  10.                 .toArray(String[]::new);
  11.  
  12.         String[] arrTwo = Arrays
  13.                 .stream(scanner.nextLine().split(" "))
  14.                 .toArray(String[]::new);
  15.  
  16.         int frontEqualsElements = 0, backEqualsElements = 0,
  17.         minArrLength = Math.min(arrOne.length, arrTwo.length);
  18.  
  19.         for (int i = 0; i < minArrLength; i++) {
  20.             if (arrOne[i].equals(arrTwo[i])) {
  21.                 frontEqualsElements++;
  22.             }
  23.         }
  24.  
  25.         for (int i = 1; i <= minArrLength; i++) {
  26.             if (arrOne[arrOne.length - i].equals(arrTwo[arrTwo.length - i])) {
  27.                 backEqualsElements++;
  28.             }
  29.         }
  30.  
  31.         System.out.println(Math.max(frontEqualsElements, backEqualsElements));
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement