Super Reduced String

  • + 0 comments

    Java 15

        public static String superReducedString(String s) {
            List<Character> chList = s.chars()
                                   .mapToObj(c -> (char) c) 
                                   .collect(Collectors.toList());
            int index = 0;
            while (index < chList.size() - 1) {
                if (chList.get(index).equals(chList.get(index + 1))) {
                    chList.remove(index); 
                    chList.remove(index);
                    if (index > 0) index--;
                } else index++;
            }
            
            String result = chList.stream()
                .map(e -> e.toString()).collect(Collectors.joining());
            return result.length() > 0 ? result : "Empty String";
        }