Strings-difference in ascii codes

https://online.codingblocks.com/player/25696/content/4778/4823

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
String str = scn.nextLine();
StringBuilder sb = new StringBuilder(str);
int a=0;
int b =1;
int c = 1;
if(str.length()>=2 && str.length()<=1000){
for(int i =1;i<=str.length()-1;i++){
int as = ((int)(sb.charAt(b)))-((int)(sb.charAt(a)));
sb = sb.insert(c, as);
if(as>=0){
a = a+2;
b = b+2;
c = c+2;
}
else{
a =a+3;
b =b+3;
c = c+3;
}
}
System.out.println(sb);
}
}
}

one test case is failing
can nyone correct my code

Hi @amanchaurasia1833,
In your code you have increase the value of a and b by 2 if ‘as’ is greater than zero and by 3 if ‘as’ is less than zero. this is done to go to the correct index for next iteration . But if the difference as is two digit number like 19 then increasing a and b by 2 will not work as then your a will point at 9 of number 19 instead of the character . Try taking ama as a testcase and dry run your algorithm on this … you will see the mistake.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.