Skip to main content
deleted 127 characters in body
Source Link
lealand
  • 1.2k
  • 13
  • 22

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.stream(SPLIT_REGEX.split(string))
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

(Note that the last assert isn't necessarily valid, since any other two character substring would suffice.)

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.stream(SPLIT_REGEX.split(string))
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

(Note that the last assert isn't necessarily valid, since any other two character substring would suffice.)

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.stream(SPLIT_REGEX.split(string))
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

(Note that the last assert isn't necessarily valid, since any other two character substring would suffice.)

added 111 characters in body
Source Link
lealand
  • 1.2k
  • 13
  • 22

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.stream(SPLIT_REGEX.split(string))
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

(Note that the last assert isn't necessarily valid, since any other two character substring would suffice.)

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.stream(SPLIT_REGEX.split(string))
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.stream(SPLIT_REGEX.split(string))
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

(Note that the last assert isn't necessarily valid, since any other two character substring would suffice.)

Still discovering more Java 8 features...
Source Link
lealand
  • 1.2k
  • 13
  • 22

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.asListstream(SPLIT_REGEX.split(string))
        .stream()
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.asList(SPLIT_REGEX.split(string))
        .stream()
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

I'm fleshing my comment out into an answer so that I can expand upon an alternative solution to this problem which I enjoy.

Since we're dealing with a String, I suspect that we can use a regular expression to String#split it into chucks for further processing. Specifically, we want a regular expression which will partition every time a character is not like the preceding one. We can do this with lookarounds:

private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

This pattern matches anywhere that:

  • (?<=(.)) given any preceding character we store in group 1 (positive lookbehind),

  • (?!\\1) the following character does not match group 1 (negative lookahead).

So splitting the string aaabbcdd with this regex will produce the array ["aaa", "bb", "c", "dd"].

From there, we can stream and find the max element by string length:

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;

public class StringRunCount {

  private static final Pattern SPLIT_REGEX = Pattern.compile("(?<=(.))(?!\\1)");

  public static String maxRun(final String string) {
    if (null == string) {
      throw new IllegalArgumentException("Argument cannot be null");
    }

    return Arrays.stream(SPLIT_REGEX.split(string))
        .max(Comparator.comparing(String::length))
        .get();
  }

  public static void main(final String[] args) {
    assert "a".equals(maxRun("a"));
    assert "aa".equals(maxRun("aab"));
    assert "bbbbb".equals(maxRun("abbbbbcc"));
    assert "aa".equals(maxRun("aabbccdd"));
  }

}

I learned about Comparator.comparing(String::length) from @Landei's answer, which I'm gracious for because I was expecting to be able to just pass in String::length by itself, originally. I also just call get on the Optional<String> returned by max because we've ruled out the only case the result could be null with our parameter check.

Source Link
lealand
  • 1.2k
  • 13
  • 22
Loading