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.  
  11. static List<Integer> match(int n, List<String> li)
  12. {
  13. int mat[][]=new int[200005][28];
  14.  
  15. List<Integer> ans=new ArrayList<>();
  16.  
  17. for(int i=0;i<n;i++)
  18. ans.add(0);
  19.  
  20. for(int i=n-1;i>=0;i--)
  21. {
  22. String s=li.get(i);
  23. int len=s.length();
  24.  
  25. int c=0;
  26. for(int j=0;j<len;j++)
  27. {
  28. int y=s.charAt(j)-'a';
  29. c+=mat[j][y];
  30. mat[j][y]=mat[j][y]+1;
  31.  
  32. }
  33. ans.set(i,c);
  34. }
  35. return ans;
  36. }
  37. public static void main (String[] args) throws java.lang.Exception
  38. {
  39. // your code goes here
  40. Scanner sc=new Scanner(System.in);
  41. int n=sc.nextInt();
  42. List<String> li=new ArrayList<>();
  43.  
  44. for(int i=0;i<n;i++)
  45. {
  46. String s=sc.next();
  47. li.add(s);
  48. }
  49.  
  50. List<Integer> ans= match(n,li);
  51. System.out.println(ans);
  52.  
  53. }
  54. }
Success #stdin #stdout 0.12s 81076KB
stdin
3
abc 
ade
abc
stdout
[4, 1, 0]