title: Reducing the time complexity of the program
body: <p>I am doing this practice problem where given a string <code>s</code> of lowercase letters, and a position <code>p</code>, I have to print all the occurrences of the letter at position <code>p</code> which occur before the position <code>p</code>. <br><br>Eg: If the string is "abaac" and p=3(1 based index) then output is 1 because 'a' occurs one time before the position p.<br><br> There are q queries each giving me a position p. Is there any way to make this code more efficient? <br>I'm using hashing which is pretty efficient I think? Is there any algorithm that I am not aware of? </p>
  
  <pre><code>#include&lt;bits/stdc++.h&gt;
  using namespace std;
  int main()
  {
      int n;
      cin&gt;&gt;n;
      string s;
      cin&gt;&gt;s;
      int q;
      cin&gt;&gt;q;
      while(q--)
      {
          int p;
          cin&gt;&gt;p;
          int freq[26] = {0};
          for(int i = 0; i&lt;(p-1); i++)
              freq[s[i]-'a']++;
          cout&lt;&lt;freq[s[p-1]-'a']&lt;&lt;"\n";
      }
  
  }
  
  </code></pre>