I am working on the following problem https://www.hackerrank.com/challenges/reduced-string .
I want to solve the above problem recursively . My code is as follows .
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(neInputStreamReader(System.in));
String line = br.readLine();
System.out.print(reduce(line));
}
public static String reduce (String str) {
if (str.equals("")) return "Empty String";
if (str.length()<2) return str;
if (str.charAt(0) == str.charAt(1)) return reduce(str.substring(2));
return str.charAt(0) + reduce(str.substring(1));
}
}
The above code fails for the following test case
baab
Could any one point out what is the problem in my code ?