Okay,so i wanted to find the largest sub-string in a given string in java. So, i went ahead and wrote a recursive function for the same to identify and return the size of each sub string. My initial plan was to have all sizes of the sub-string added to Array list. I hen proposed to sort the array list in descending order and taking the top most value which would give me the size of the Largest sub-string. The problem is my ArrayList wen bonkers. Its not adding the values my function is returning but storing some other values.
My function is returning: 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 2 0 1
But what is stored is : 0 18 17 -1 -1 -1 -1 -1 11 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
As you can this is totally different . I did figure out a solution but i want to know why ArrayList is not working as it should. Any insight will be greatly appreciated. Again i just want to know why the ArrayList is not working as it should.
package sush;
import java.util.ArrayList;
import java.util.Collections;
public class Paliandrome {
static int chk ( String s ,int x, int y,int count)
{
String begins=s.charAt(x)+"";
String end=s.charAt(y)+"";
if(x==y && begins.equals(end))
{
System.out.println(count);
return count+1;
}
if(x>y)
return count;
if(begins.equals(end))
return chk(s,x+1,y-1,count+2);
else
return count=0;
}
public static void main(String[] args)
{
String sh = "aabaabaasdwaasddsaa";
int x = 0 , y = 0;
int count=0,high=0;
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.clear();
for(int i=0 ; i<sh.length() ; i++ )
{
x=i;
y=sh.length()-1;
count=0;
int temp =chk(sh,x,y,count);
System.out.println(temp);
arr.add(temp);
}
System.out.println(high+"\n"+"ArrayList contents :");
for(int i= 0;i<arr.size();i++)
{
System.out.println(arr.indexOf(i));
}
Collections.sort(arr);
Collections.reverse(arr);
System.out.println(arr.indexOf(0));
}
}