fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. String s1 = "zyxwv";
  14. String s2 = "edcba";
  15. System.out.println(solve(0,0,s1.toCharArray(),s2.toCharArray()));
  16. }
  17.  
  18. public static int solve(int i, int j, char[] str1, char[] str2 ){
  19.  
  20. if(i== str1.length && j == str2.length) return 0;
  21.  
  22. int ans = Integer.MAX_VALUE;
  23.  
  24. if(i<str1.length ){
  25. char ch = str1[i];
  26. int intCfts = checkConflicts(str1, i, ch);
  27. int newCfts = checkConflicts(str2, j, ch);
  28. ans = Math.min(ans, intCfts+newCfts+solve(i+1,j, str1, str2));
  29. }
  30.  
  31. if(j<str2.length ){
  32. char ch = str2[j];
  33. int intCfts = checkConflicts(str2, j, ch);
  34. int newCfts = checkConflicts(str1, i, ch);
  35. ans = Math.min(ans, intCfts+newCfts+solve(i,j+1,str1, str2));
  36.  
  37. }
  38.  
  39. return ans;
  40.  
  41. }
  42. public static int checkConflicts(char[] str, int index, char ch){
  43. int cnt = 0;
  44. for(int i = index; i< str.length; i++){
  45. if(str[i]-'a' < ch-'a') cnt++;
  46. }
  47. return cnt;
  48. }
  49. }
Success #stdin #stdout 0.07s 54564KB
stdin
Standard input is empty
stdout
20