Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Solution {
- public int solution(String S, int K) {
- if(K < 1)
- return -1;
- String[] words = S.split("\\s");
- int numberOfSms = 0;
- int smsLength = 0;
- for(int i = 0; i < words.length; i++){
- int nextWordLength = words[i].length() ;
- if(nextWordLength > K)
- return -1;
- if(smsLength != 0){
- if(smsLength + nextWordLength <= K){
- smsLength += nextWordLength +1;
- }else{
- numberOfSms++;
- smsLength = nextWordLength +1;
- }
- }else{
- numberOfSms++;
- smsLength = nextWordLength +1;
- }
- }
- return numberOfSms;
- }
- }
- // one class needs to have a main() method
- public class HelloWorld
- {
- // arguments are passed using the text field below this editor
- public static void main(String[] args)
- {
- Solution myObject = new Solution();
- System.out.print(myObject.solution("SMS messages are really gooood", 12));
- System.out.print(myObject.solution("oen one one", 3));
- System.out.print(myObject.solution("oen one one one one", 3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement