When you’re precalculating *countchar* it will work with complexity *O(length * length)* in worst case. You can change a way how to calculate it.

```
if(a[0] != '#')
countchar[0][a[0] - 'a'] = 1;
for(i = 1; i < length; i++)
{
for(j = 0; j < 26; j++)
countchar[i][j] = countchar[i - 1][j];
if(a[i] != '#')
countchar[i][a[i] - 'a']++;
}
```

1 Like

thanks … got an ac