File tree 2 files changed +73
-0
lines changed
2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public static String mostCommonWord (String paragraph , String [] banned ) {
3
+ paragraph = paragraph .toLowerCase ();
4
+ List <String > bannedWords = Arrays .asList (banned );
5
+
6
+ String [] words = paragraph .trim ().split ("\\ s+" );
7
+ Map <String , Integer > map = new LinkedHashMap <>();
8
+ int maxValue = Integer .MIN_VALUE ;
9
+
10
+ for (String word : words ) {
11
+
12
+ if (!Character .isAlphabetic (word .charAt (word .length ()-1 ))) {
13
+ word = word .substring (0 , word .length ()-1 );
14
+ }
15
+
16
+ if (!(word .equals (" " ) || word .equals ("" )) && bannedWords .indexOf (word ) == -1 ) {
17
+ map .put (word , map .getOrDefault (word , 0 ) + 1 );
18
+ maxValue = Math .max (maxValue , map .get (word ));
19
+ }
20
+ }
21
+
22
+ for (String word : map .keySet ()) {
23
+ if (map .get (word ) == maxValue ) {
24
+ return word ;
25
+ }
26
+ }
27
+
28
+ return "" ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+ public ListNode mergeKLists (ListNode [] lists ) {
11
+ if (lists .length == 0 ) {
12
+ return null ;
13
+ }
14
+
15
+ PriorityQueue <ListNode > queue = new PriorityQueue <>(lists .length , new Comparator <ListNode >() {
16
+ @ Override
17
+ public int compare (ListNode o1 , ListNode o2 ) {
18
+ return o1 .val - o2 .val ;
19
+ }
20
+ });
21
+
22
+ for (int i = 0 ; i < lists .length ; i ++) {
23
+ if (lists [i ] != null ) {
24
+ queue .add (lists [i ]);
25
+ }
26
+ }
27
+
28
+ ListNode head = new ListNode (0 );
29
+ ListNode p = head ;
30
+
31
+ while (!queue .isEmpty ()) {
32
+ ListNode node = queue .poll ();
33
+ p .next = node ;
34
+ if (node .next != null ) {
35
+ queue .add (node .next );
36
+ }
37
+
38
+ p = p .next ;
39
+ }
40
+
41
+ return head .next ;
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments