I am facing an index out of bound for line number 34 in my code. I have also commented on this line. Kindly let me know where I am wrong.
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
while(test–>0)
{
String input = br.readLine();
String left=“”, right=“”;
int len = input.length();
int mid = len/2;
if(len%2==0){
left = input.substring(0,mid);
right = input.substring(mid,len);
}
else{
left = input.substring(0,mid);
right = input.substring(mid+1,len);
}
int flag=0;
for(int i=0;i<left.length();i++)
{
loop2:
for(int j=0;j<right.length();j++)
{
if(left.charAt(i)==right.charAt(j)) //for this line
{
if(j==((right.length())-1))
right = right.substring(0,j) + “0”;
else
right = right.substring(0,j) + “0” + right.substring(j+1);
i++;
continue loop2;
}
if((j == right.length()-1) && (left.charAt(i)!=right.charAt(j)))
{
flag=1;
break;
}
}
}
if(flag==1)
System.out.println(“NO”);
else
System.out.println(“YES”);
}
}
}
Please format your code, or share it as a link to ideone.
You are changing the lenght of string within the loop which is causing the issue here 
But, im not changing the length. I am just replacing a character with another character in string.
It would still cause an issue since there is a change in String.
please format your code from next time onwards (formatting)
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
while(test–>0)
{
String input = br.readLine();
String left="", right="";
int len = input.length();
int mid = len/2;
if(len%2==0){
left = input.substring(0,mid);
right = input.substring(mid,len);
}
else{
left = input.substring(0,mid);
right = input.substring(mid+1,len);
}
int flag=0;
for(int i=0;i<left.length();i++)
{
loop2:
for(int j=0;j<right.length();j++)
{
if(left.charAt(i)==right.charAt(j)) //for this line
{
if(j==((right.length())-1))
right = right.substring(0,j) + “0”;
else
right = right.substring(0,j) + “0” + right.substring(j+1);
i++;
continue loop2;
}
if((j == right.length()-1) && (left.charAt(i)!=right.charAt(j)))
{
flag=1;
break;
}
}
}
if(flag==1)
System.out.println(“NO”);
else
System.out.println(“YES”);
}
}
}
The Index out of range error is raised when you attempt to use the value of a list/string/dict/tuple index which is not defined. For example,
a = [1,2,3,4,5]
print(a[5])
This will give error since the list has no fifth index (zero based indexing) so only index [0] to [4] are valid
So, what is the best way to handle it. Do you have any suggestions. Or I have to change my entire approach?
If you can store the strings in some extra temporary variable
OR
Store the length initially outside the loop
int r_len = <that_string>.length();
and use r_len in loops
yup. its working now.
But still getting wrong answer for abbaab input. please check it once
https://www.codechef.com/viewsolution/36060846
I really think you have over-complicated the problem. Even if you get the right answer for "abbaab"
, it will timeout for larger strings as your complexity is \mathcal O(n^2).
This is how I’ve solved it.
1 Like