Simple Java 8 functional (declarative) solution

  • 4

    I noticed that Java 8 streams are much slower than loops. Streams make the code declarative but are a huge hit to the performance, unfortunately.

    public List<String> generatePossibleNextMoves(String s) {
        List<String> res = new ArrayList<>();
        IntStream.range(1,s.length()).forEach(i -> 
            {if (s.charAt(i-1) == '+' && s.charAt(i) == '+') 
            res.add(s.substring(0,i-1) + "--" + s.substring(i+1,s.length()));});
        return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.