Write a method removeDuplicatesString input reduces all substrings of the same repeated character to just one occurrence of the character For instance if input = hello all! the method should return h
public static String removeDuplicates(String input) { if (input.length() <= 1) { return input; } if (input.charAt(0) == input.charAt(1)) { int i = 1; while (i < input.length() && input.charAt(i) == input.charAt(0)) { i++; } return input.charAt(0) + removeDuplicates(input.substring(i)); } else { return input.charAt(0) + removeDuplicates(input.substring(1)); } }
原文地址: https://www.cveoy.top/t/topic/bO7P 著作权归作者所有。请勿转载和采集!