Skip to content

Commit 4556542

Browse files
refactor 418
1 parent eef5bff commit 4556542

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/main/java/com/fishercoder/solutions/_418.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,31 @@
6262
*/
6363
public class _418 {
6464

65-
/**credit: https://discuss.leetcode.com/topic/62455/21ms-18-lines-java-solution
66-
*
67-
1. String s = String.join(" ", sentence) + " " ;. This line gives us a formatted sentence to be put to our screen.
68-
2. start is the counter for how many valid characters from s have been put to our screen.
69-
3. if (s.charAt(start % l) == ' ') is the situation that we don't need an extra space for current row. The current row could be successfully fitted. So that we need to increase our counter by using start++.
70-
4. The else is the situation, which the next word can't fit to current row. So that we need to remove extra characters from next word.
71-
5. start / s.length() is (# of valid characters) / our formatted sentence.
72-
*/
73-
public int wordsTyping(String[] sentence, int rows, int cols) {
74-
String s = String.join(" ", sentence) + " ";
75-
int start = 0;
76-
int l = s.length();
77-
for (int i = 0; i < rows; i++) {
78-
start += cols;
79-
if (s.charAt(start % l) == ' ') {
80-
start++;
81-
} else {
82-
while (start > 0 && s.charAt((start - 1) % l) != ' ') {
83-
start--;
65+
public static class Solution1 {
66+
/**
67+
* credit: https://discuss.leetcode.com/topic/62455/21ms-18-lines-java-solution
68+
* <p>
69+
* 1. String s = String.join(" ", sentence) + " " ;. This line gives us a formatted sentence to be put to our screen.
70+
* 2. start is the counter for how many valid characters from s have been put to our screen.
71+
* 3. if (s.charAt(start % l) == ' ') is the situation that we don't need an extra space for current row. The current row could be successfully fitted. So that we need to increase our counter by using start++.
72+
* 4. The else is the situation, which the next word can't fit to current row. So that we need to remove extra characters from next word.
73+
* 5. start / s.length() is (# of valid characters) / our formatted sentence.
74+
*/
75+
public int wordsTyping(String[] sentence, int rows, int cols) {
76+
String s = String.join(" ", sentence) + " ";
77+
int start = 0;
78+
int l = s.length();
79+
for (int i = 0; i < rows; i++) {
80+
start += cols;
81+
if (s.charAt(start % l) == ' ') {
82+
start++;
83+
} else {
84+
while (start > 0 && s.charAt((start - 1) % l) != ' ') {
85+
start--;
86+
}
8487
}
8588
}
89+
return start / s.length();
8690
}
87-
return start / s.length();
8891
}
89-
9092
}

src/test/java/com/fishercoder/_418Test.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/11/17.
11-
*/
129
public class _418Test {
13-
private static _418 test;
10+
private static _418.Solution1 test;
1411
private static String[] sentence;
1512

1613
@BeforeClass
1714
public static void setup() {
18-
test = new _418();
15+
test = new _418.Solution1();
1916
}
2017

2118
@Test

0 commit comments

Comments
 (0)