Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created June 15, 2020 09:51
Show Gist options
  • Save SiAust/29e6c43236ccefca8469f7c6148ec787 to your computer and use it in GitHub Desktop.
Save SiAust/29e6c43236ccefca8469f7c6148ec787 to your computer and use it in GitHub Desktop.
Matcher, Pattern, MatchResults, streaming.
/*
* Compiles a regex pattern, calls a Matcher on with the input string as a param,
* calls .results() which returns a stream of MatchResults for each successful match.
* .count() counts the elements in the stream.
* */
String input = new Scanner(System.in).nextLine(); // "accggaabb"
System.out.println((double) Pattern.compile("(?:[cg])").matcher(input).results().count() / input.length() * 100);
// 44.44444444444444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment