Recursively remove all adjacent duplicates geeks solution java
problem :
remove all adjacent duplicates in a string
Input
2
grrrrrrrrrrrrrgr
acbbcddeeffggfc
output:
r
afc
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void check(String str)
{
if(str.length()<=1)
{
System.out.println(str);
return;
}
// System.out.println(str);
String n=new String();
int count=0;
for(int i=0;i<str.length();i++)
{
while(i<str.length()-1 && str.charAt(i)==str.charAt(i+1))
{
if(i<str.length()-2 &&str.charAt(i)!=str.charAt(i+2))
i+=2;
else
i++;
count++;
}
if(i!=str.length()-1)
n=n+str.charAt(i);
else
{if(i==str.length()-1 && str.charAt(i)!=str.charAt(i-1))
n=n+str.charAt(i);
}
}
if(count>0)
check(n);
else
System.out.println(n);
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
String str=ab.next();
check(str);
// System.out.println();
}
}
}
remove all adjacent duplicates in a string
Input
2
grrrrrrrrrrrrrgr
acbbcddeeffggfc
output:
r
afc
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void check(String str)
{
if(str.length()<=1)
{
System.out.println(str);
return;
}
// System.out.println(str);
String n=new String();
int count=0;
for(int i=0;i<str.length();i++)
{
while(i<str.length()-1 && str.charAt(i)==str.charAt(i+1))
{
if(i<str.length()-2 &&str.charAt(i)!=str.charAt(i+2))
i+=2;
else
i++;
count++;
}
if(i!=str.length()-1)
n=n+str.charAt(i);
else
{if(i==str.length()-1 && str.charAt(i)!=str.charAt(i-1))
n=n+str.charAt(i);
}
}
if(count>0)
check(n);
else
System.out.println(n);
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
String str=ab.next();
check(str);
// System.out.println();
}
}
}
0 Comments:
Post a Comment