Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Solution {
  4. public int solution(String S, int K) {
  5. if(K < 1)
  6. return -1;
  7.  
  8. String[] words = S.split("\\s");
  9.  
  10. int numberOfSms = 0;
  11. int smsLength = 0;
  12. for(int i = 0; i < words.length; i++){
  13.  
  14. int nextWordLength = words[i].length() ;
  15.  
  16. if(nextWordLength > K)
  17. return -1;
  18.  
  19. if(smsLength != 0){
  20. if(smsLength + nextWordLength <= K){
  21. smsLength += nextWordLength +1;
  22. }else{
  23. numberOfSms++;
  24. smsLength = nextWordLength +1;
  25. }
  26. }else{
  27. numberOfSms++;
  28. smsLength = nextWordLength +1;
  29. }
  30. }
  31. return numberOfSms;
  32. }
  33. }
  34.  
  35.  
  36. // one class needs to have a main() method
  37. public class HelloWorld
  38. {
  39. // arguments are passed using the text field below this editor
  40. public static void main(String[] args)
  41. {
  42. Solution myObject = new Solution();
  43. System.out.print(myObject.solution("SMS messages are really gooood", 12));
  44. System.out.print(myObject.solution("oen one one", 3));
  45. System.out.print(myObject.solution("oen one one one one", 3));
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement