# Please point out what I'm doing wrong

• My basic approach is this

1. Note `currentIndex` for `ring`. Initially, the `currentIndex` is 0 for both directions i.e. clockwise and anticlockwise
2. For each letter in `key`, count the number of clockwise steps and the anticlockwise steps needed. Take the `min` and add this min to the result
3. Also maintain a `pos` array. The first element of `pos` denotes the index reached after moving clockwise. The second element denotes the index reached after moving anticlockwise. So `pos` is

`[index reached after moving clockwise][index reached after moving anticlockwise]`

1. As per the `min` calculated in step 2, decide the `currentIndex` for the next iteration

Here is the code. It passes 54 test cases but fails after that. My answer is 1 more than what is expected. What am i doing wrong?

``````public class Solution {
public int findRotateSteps(String ring, String key) {

int res=0;
int [] pos = new int[2];
String [] ringArr = ring.split("");
for(String k : key.split(""))
{
int currentIndex = pos[0];
int clockSteps = getSteps(k,ringArr,pos,0,currentIndex);
int anticlockSteps = getSteps(k,ringArr,pos,1,currentIndex);
if(clockSteps<=anticlockSteps)
pos[1]=pos[0];
else
pos[0]=pos[1];

res+=Math.min(clockSteps,anticlockSteps)+1;
}

return res;
}

int getSteps(String k,String [] ringArr,int [] pos, int direction, int currentIndex)
{
int steps=0;
int i=currentIndex;
if(direction==0)
{
do{
if(ringArr[i].equals(k))
break;
else{

steps++;
}
i++;
if(i>ringArr.length-1)
i=0;
}while(i!=currentIndex);

}
else
{
do{
if(ringArr[i].equals(k))
break;
else{

steps++;
}
i--;
if(i<0)
i=ringArr.length-1;
}while(i!=currentIndex);
}
pos[direction]=i;
return steps;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.