1-4 lines in Java

  • 15

    Not sure I did it as good as it can be, as I'm still a beginner at Java streaming. If you can improve this, I'll be happy to see how.

    public List<List<String>> groupStrings(String[] strings) {
        return new ArrayList(Stream.of(strings).collect(Collectors.groupingBy(
            s -> s.chars().mapToObj(c -> (c - s.charAt(0) + 26) % 26)

  • 0

    Downvote? Seriously? For what?

    If you can do better than this, then like I said, please show me.

  • 1

    Really crazy golfing code... I totally cannot understand it.

    BTW, I give an upvote to cancel that downvote :-)

  • -1

    I think 120ms is too slow, compared to other solutions.

  • 1

    "Slower", yes, but "too" slow? Too slow for what? Not for the OJ, which easily accepts it. There are plenty of real world applications that don't need fast code, either. Otherwise nobody would use Python and Ruby, maybe not even Java. We might all write C :-)

    Other factors matter as well, like simplicity and correctness. If you look at the top-voted solution for example, I'd say that mine is much simpler, easier to understand and there's less room for errors.

  • 1

    Very nice solution!

    Modified it slightly, thanks for posting!

    import static java.util.stream.Collectors.toList;
    public class Solution {
        public List<List<String>> groupStrings(String[] strings) {
            return Arrays.stream(strings)
        private List<Integer> normalize(String s) {
            return s.chars()
                    c -> (c - s.charAt(0) + 26) % 26

  • 0

    @KidOptimo Dang, I didn't have to sort? I guess my mind was stuck in Python mode.

    Edit 2: Oh wait... the problem used to require each group to be sorted and this requirement has been lifted after I posted. I removed my sorting now.

    Nice version. Didn't know this:: ... and that you can import methods like that. Thanks.

  • 1

    I am afraid this is not a good coding style for interviews.

Log in to reply

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