Skip to content

Commit 83bbea4

Browse files
committed
Added task 30.
1 parent 2a37e1d commit 83bbea4

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

src/main/java/s0019.remove.nth.node.from.end.of.list/ListNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ public class ListNode {
1414
this.val = val;
1515
this.next = next;
1616
}
17+
18+
public String toString() {
19+
String result = "";
20+
ListNode nextCopy = next;
21+
while (nextCopy.next != null) {
22+
result += ", " + nextCopy.val;
23+
nextCopy = nextCopy.next;
24+
}
25+
return result;
26+
}
1727
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package s0030.substring.with.concatenation.of.all.words;
2+
3+
import java.util.*;
4+
5+
public class Solution {
6+
public List<Integer> findSubstring(String s, String[] words) {
7+
List<Integer> output = new ArrayList<>();
8+
if (s == null || words == null || s.length() == 0 || words.length == 0) {
9+
return output;
10+
}
11+
int wordLen = words[0].length(), wordsCount = 0;
12+
Map<String, Integer> wordToCount = new HashMap<>();
13+
for (String word : words) {
14+
wordToCount.put(word, wordToCount.getOrDefault(word, 0) + 1);
15+
wordsCount++;
16+
}
17+
int substringLen = wordLen * wordsCount;
18+
for (int start = 0; start < wordLen; start++) {
19+
Queue<String> queue = new LinkedList<>();
20+
Map<String, Integer> substrWordToCount = new HashMap<>();
21+
for (int lo = start, hi = start; hi <= s.length() - wordLen; hi += wordLen) {
22+
String word = s.substring(hi, hi + wordLen);
23+
if (!wordToCount.containsKey(word)) {
24+
queue = new LinkedList<>();
25+
lo = hi + wordLen;
26+
substrWordToCount = new HashMap<>();
27+
} else {
28+
int substrWordCount = substrWordToCount.getOrDefault(word, 0);
29+
if (substrWordCount >= wordToCount.get(word)) {
30+
while (!queue.peek().equals(word)) {
31+
String wordToRemove = queue.poll();
32+
int count = substrWordToCount.get(wordToRemove);
33+
if (count == 1) substrWordToCount.remove(wordToRemove);
34+
else substrWordToCount.put(wordToRemove, count - 1);
35+
lo += wordLen;
36+
}
37+
lo += wordLen;
38+
queue.poll();
39+
} else {
40+
substrWordToCount.put(word, substrWordCount + 1);
41+
}
42+
queue.offer(word);
43+
if (queue.size() == wordsCount) {
44+
output.add(lo);
45+
}
46+
}
47+
}
48+
}
49+
return output;
50+
}
51+
}

src/test/java/s0019.remove.nth.node.from.end.of.list/SolutionTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ public class SolutionTest {
99
@Test
1010
public void fourSum() {
1111
ListNode head = new ListNode();
12-
head.val = 1;
1312
ListNode node1 = new ListNode();
14-
node1.val = 2;
13+
node1.val = 1;
1514
head.next = node1;
1615
ListNode node2 = new ListNode();
17-
node2.val = 3;
16+
node2.val = 2;
1817
node1.next = node2;
1918
ListNode node3 = new ListNode();
20-
node3.val = 4;
19+
node3.val = 3;
2120
node2.next = node3;
2221
ListNode node4 = new ListNode();
23-
node4.val = 5;
22+
node4.val = 4;
2423
node3.next = node4;
25-
assertThat(new Solution().removeNthFromEnd(head, 2).toString(), equalTo("[1, 2, 3, 5]"));
24+
ListNode node5 = new ListNode();
25+
node5.val = 5;
26+
node4.next = node5;
27+
assertThat(new Solution().removeNthFromEnd(head, 2).toString(), equalTo("1, 2, 3, 5"));
2628
}
2729
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package s0030.substring.with.concatenation.of.all.words;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void findSubstring() {
11+
assertThat(
12+
new Solution()
13+
.findSubstring("barfoothefoobarman", new String[] {"foo", "bar"})
14+
.toString(),
15+
equalTo("[0, 9]"));
16+
}
17+
}

0 commit comments

Comments
 (0)