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 a = "abcdefghijklmnopqrstuvwxyz";
  14. String b = "cba";
  15. System.out.println(solution(a,b));
  16. }
  17.  
  18. public static int solution(String s1,String s2)
  19. {
  20. int sum = 0,pos = 0;
  21. HashMap<Character, Integer> map = new HashMap<>();
  22. char[] s3 = s1.toCharArray();
  23. char[] s4 = s2.toCharArray();
  24. for(int i=0;i<s1.length();i++){
  25.  
  26. map.put(s3[i],i);
  27.  
  28. }
  29. for(int i=0;i<s2.length();i++){
  30.  
  31. int k = map.get(s4[i]);
  32. sum += Math.abs(k-pos);
  33. pos = k;
  34. System.out.println(sum);
  35. }
  36. return sum;
  37.  
  38. }
  39. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
2
3
4
4