Array out of bound exp

import java.io.;
import java.util.
;

public class Solution {

public static void dup(String str,int i,String ans)
{
    if(i==str.length())
        System.out.println(ans);
    
    if(str.charAt(i)==str.charAt(i+1))
    {
        char st=str.charAt(i);
        dup( str,i+2, ans+st);
    }
    else{
         char st=str.charAt(i);
         dup(str,i+1,ans+st);
    }
        
    
}

public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    String a=s.nextLine();
    
   dup(a,0," ");
    
    
    
}      

}
how can we remove out of bound exception while comparing with next element ?

Hi @Kapsime_S,
I can’t really understand what you are trying to do here … This is in regards with coin change problem right??
Because there was that selected so i don’t understand in regards of which question are you asking doubt.

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.