Problem: This program doesn't like consecutive characters in a string. If it find consecutive characters them it bombs them and generate a substring. The largest consecutive character substring is bombed first.
For example, the input "abcccddbcaab" will be bombed in each step to get he following substrings
Input: abcccddbcaab
Step 1: abddbcaab
Step 2: abbcaab
Step 3: acaab
Step 4: acb
//To remove the largest consecutive same character substring repeatedly until there's no such substring
public class Bomber {
public static void main(String args[])
{
String temp2;
temp2="abcccddbcaab";
while(true)
{
System.out.println(temp2);
temp2=bomb(temp2);
if(temp2==null)
break;
}
}
//Returns the string after removing the largest consecutive same character substring
public static String bomb(String str)
{
int startMax=-1,endMax=-1;
int globalMax=0,localMax=0;
int start=0,end=0;
for(int i=0;i<=str.length()-2;)
{
if(str.charAt(i)==str.charAt(i+1))
{
start=i;localMax=0;
while(str.charAt(i)==str.charAt(i+1))
{
localMax++;
i++;
if(i==str.length()-1)
break;
}
end=start+localMax;
if(localMax>globalMax)
{
globalMax=localMax;
startMax=start;
endMax=end;
}
}
else
{
i++;
}
}
if(globalMax>0)
return str.substring(0,startMax) + str.substring(endMax+1,str.length());
else //No bombing happened in this method call. So we can stop bombing the string
return null;
}
}
For example, the input "abcccddbcaab" will be bombed in each step to get he following substrings
Input: abcccddbcaab
Step 1: abddbcaab
Step 2: abbcaab
Step 3: acaab
Step 4: acb
//To remove the largest consecutive same character substring repeatedly until there's no such substring
public class Bomber {
public static void main(String args[])
{
String temp2;
temp2="abcccddbcaab";
while(true)
{
System.out.println(temp2);
temp2=bomb(temp2);
if(temp2==null)
break;
}
}
//Returns the string after removing the largest consecutive same character substring
public static String bomb(String str)
{
int startMax=-1,endMax=-1;
int globalMax=0,localMax=0;
int start=0,end=0;
for(int i=0;i<=str.length()-2;)
{
if(str.charAt(i)==str.charAt(i+1))
{
start=i;localMax=0;
while(str.charAt(i)==str.charAt(i+1))
{
localMax++;
i++;
if(i==str.length()-1)
break;
}
end=start+localMax;
if(localMax>globalMax)
{
globalMax=localMax;
startMax=start;
endMax=end;
}
}
else
{
i++;
}
}
if(globalMax>0)
return str.substring(0,startMax) + str.substring(endMax+1,str.length());
else //No bombing happened in this method call. So we can stop bombing the string
return null;
}
}