Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StringPalindrome {
- public static void main(String[] args) {
- recursiveSolve("ddabccba");
- }
- public static void recursiveSolve(String input) {
- int result = recursiveSolve(input, 0, input.length() - 1);
- System.out.println(result);
- }
- public static int recursiveSolve(String input, int start, int end) {
- if (start == end) return 0;
- if (start > end) return Integer.MAX_VALUE;
- if (start == end - 1) {
- if (input.charAt(start) == input.charAt(end)) {
- return 0;
- } else {
- return 1;
- }
- }
- if (input.charAt(start) == input.charAt(end)) {
- return recursiveSolve(input, start + 1, end - 1);
- }
- return Math.min(recursiveSolve(input, start + 1, end), recursiveSolve(input, start, end - 1)) + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment