I passed all test cases except one(TLE).

int main() {

int t;scanf("%d",&t);
   char s[100000];
    int len=strlen(s);
  //  cout<<len<<endl;
 //   for(int i=0;i<100000;i++)if(s[i]-'a'>=0 && s[i]-'a'<=25)len++;else break;
    int cost[len];
    for(int i=0;i<len;i++)cost[i]=1000000000;
    vector <int> g[26];
    for(int i=0;i<len;i++)g[s[i]-'a'].pb(i);
    for(int i=0;i<len-1;i++){
        for(auto it=0;it<g[s[i]-'a'].size();it++)if(cost[g[s[i]-'a'][it]]>cost[i])cost[g[s[i]-'a'][it]]=cost[i];
        int temp=abs(s[i+1]-s[i]);
return 0;


I dont think we can see your code because LoC solutions not being public. Can you somehow share your code?

I have edited question.

It seems obviously TLE to me :confused:

Take a look at the nested for-loops you used. I think that makes your code O(N^2). Doesn’t it? In case I am wrong, can you explain further what you have tried to do?

I know it has to be TLE. I am not asking about how can I optimize my code. I want to know how to solve it efficiently.

A more elegant way of what you tried to do is like this-

       int arr[26],i,j;

	   int dp[n];
	   //for(i=0;i<n;i++)cout<<dp[i]<<" ";cout<<endl;

arr is serving the purpose of, minimum cost of reaching at letter i if we use jumps in between.


Ohk, I thought you wanted to know why your code is wrong.

I didnt include comments because i thought we both are on same both in terms of thinking. Do you need comments as well?

No comments needed. Vow!!! I just had to use dp. Thanks.

You did use dp, its just that you could have done better. Wish you best :slight_smile:

1 Like

Nice solution @vijju123 :slight_smile:
I guess you have used shortest path algo in some way (denoted by arr[]).

Thanks dear :slight_smile: . Yes, I used arr for that. Its just visualization. Take it as, we are at some character ‘ch’ present at index i, we can reach it in two ways, either by jumping from previous char, or jumping to this char directly from same-character checkpost. Jumping from previous same-char checkpost is already stored in arr[s[i]-‘a’], and cost to reach here from previous element is in dp[i].

For this case, imagine that we are filling a general index i , then proceed for start and end of table.

The best thing is synergy, dp[i] helps in determining arr, while arr helps in finding dp[i].

@vijju123 I have a doubt what if there is no check point of that character but it gives the smallest path i.e. acdefb.
So here a to b is shortest path Right?. But the algorithm is looking for check points which does not exist. So it will go for a-f i.e 5 then f-b that is 4. Because arr[b-‘a’] is 1000000. Can you please explain how the algo will handle this.

In the question we are starting from a, and have to reach b. We cannot reach b before we reach f, since jumping isnt possible and as given in Q, we have to go linearly from a to c, then d,e,f and finally b. Answer is same in both cases.

I interpreted the question in wrong way. Need to develop patience to read the question carefully. Lesson learned:)

1 Like

A very similar problem to this one is DIGJUMP. Maybe its editorial will help you much more :slight_smile: