Created
June 15, 2020 09:51
-
-
Save SiAust/29e6c43236ccefca8469f7c6148ec787 to your computer and use it in GitHub Desktop.
Matcher, Pattern, MatchResults, streaming.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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