我正在處理以下問題https://www.hackerrank.com/challenges/reduced-string。Hackerrank字符串縮減
我想遞歸解決上述問題。我的代碼如下。
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));
}
}
上面的代碼失敗以下測試用例
BAAB
可以在任何一個指出什麼是我的代碼的問題?
可能重複[什麼是調試器,它如何幫助我診斷問題?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-幫助我診斷問題) – Raedwald
這不是一個調試網站。 – sashas
**不起作用**如果您希望我們爲您提供幫助,則說明不夠完整。 – GhostCat