Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- class Solution {
- public static void main(String[] args) {
- int result = maxWordCount("Jag gillar korv med bröd. Det är gott! Eller?");
- System.out.println(result);
- }
- static int maxWordCount(String text) {
- var sentences = new ArrayList<String>();
- var carry = new StringBuilder();
- var delimeters = List.of('!', '?', '.');
- // split string into sentences
- for (char c : text.toCharArray()) {
- if (delimeters.contains(c)) {
- sentences.add(carry.toString());
- carry = new StringBuilder();
- }
- else {
- carry.append(c);
- }
- }
- int max = 0;
- // find the maximum word count in the sentence
- for (var sentence : sentences) {
- var words = sentence.split(" ");
- int curr = 0;
- for (var word : words) {
- if (" ".equals(word) || "".equals(word)) {
- continue;
- }
- curr += 1;
- }
- max = Math.max(max, curr);
- }
- return max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement