fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static int findMaxDifferenceIndex(String X, String Y) {
  5. // Create Group1 by taking every 2nd character from X
  6. StringBuilder group1 = new StringBuilder();
  7. for (int i = 0; i < X.length(); i += 2) {
  8. group1.append(X.charAt(i));
  9. }
  10.  
  11. int N = group1.length();
  12. int maxDiff = -1;
  13. int maxDiffIndex = -1;
  14.  
  15. // Check all possible substrings of Y with length N
  16. for (int i = 0; i <= Y.length() - N; i++) {
  17. // Get substring of length N starting at index i
  18. String substring = Y.substring(i, i + N);
  19.  
  20. // Calculate total absolute ASCII difference
  21. int currentDiff = 0;
  22. for (int j = 0; j < N; j++) {
  23. currentDiff += Math.abs(group1.charAt(j) - substring.charAt(j));
  24. }
  25.  
  26. // Update max difference and index if current difference is larger
  27. if (currentDiff > maxDiff) {
  28. maxDiff = currentDiff;
  29. maxDiffIndex = i;
  30. }
  31. }
  32.  
  33. return maxDiffIndex;
  34. }
  35.  
  36. public static void main(String[] args) {
  37. // Read input from standard input
  38. Scanner scanner = new Scanner(System.in);
  39.  
  40. // Read number of test cases if required (SPOJ may have multiple test cases)
  41. // If single test case, remove this loop
  42. while (scanner.hasNext()) {
  43. String X = scanner.nextLine();
  44. String Y = scanner.nextLine();
  45.  
  46. // Call the method and print result
  47. int result = findMaxDifferenceIndex(X, Y);
  48. System.out.println(result);
  49. }
  50.  
  51. scanner.close();
  52. }
  53. }
Success #stdin #stdout 0.13s 56636KB
stdin
aabbcc
abcd
stdout
1