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. Scanner sc=new Scanner(System.in);
  14. String s=sc.next();
  15.  
  16. int n=s.length();
  17.  
  18. int prefix[][]=new int[n+1][27];
  19.  
  20. for(int i=0;i<n;i++)
  21. {
  22. int val= s.charAt(i)-'a' ;
  23. for(int j=0;j<26;j++)
  24. {
  25. if(j==val)
  26. {
  27.  
  28. prefix[i+1][j]=prefix[i][j]+1;
  29.  
  30. }
  31. else
  32. prefix[i+1][j]=prefix[i][j];
  33.  
  34. }
  35.  
  36. }
  37.  
  38.  
  39. int q=sc.nextInt();
  40. int ans=0;
  41. for(int i=0;i<q;i++)
  42. {
  43. int l=sc.nextInt();
  44. int r=sc.nextInt();
  45.  
  46.  
  47. int val=Math.max(l-1,0);
  48. for(int j=0;j<=25;j++)
  49. {
  50. int freq= prefix[r][j]-prefix[val][j];
  51. ans+=(freq*(freq+1))/2;
  52. }
  53. }
  54.  
  55. System.out.println(ans);
  56. }
  57. }
Success #stdin #stdout 0.13s 56552KB
stdin
aabc
2
1 2
3 4
stdout
5