In the question link , I first tried it using a character array. This solution exceeded the time limit.
char s[100000];
cin >> s;
int cnt[26];
for(int i=0;i<26;++i)
cnt[i]=0;
for(int i=0;i<strlen(s);++i)
cnt[s[i]-'a']++;
int count =0;
for(int i=0;i<26;++i)
if(cnt[i]>0)
count++;
cout << count << endl;
But then I changed the above code to this:
string s;
cin >> s;
int cnt[26];
for(int i=0;i<26;++i)
cnt[i]=0;
for(int i=0;i<s.length();++i)
cnt[s.at(i)-'a']++;
int count =0;
for(int i=0;i<26;++i)
if(cnt[i]>0)
count++;
cout << count << endl;
And the code passed with ease. And also the first one exceeded the time limit of 1 sec, while the second one passed with time of execution of 0.04 sec. Why is there such a large difference in time of execution?