File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1998
1998
2704|[ To Be Or Not To Be] ( ./solutions/2704-to-be-or-not-to-be.js ) |Easy|
1999
1999
2705|[ Compact Object] ( ./solutions/2705-compact-object.js ) |Medium|
2000
2000
2706|[ Buy Two Chocolates] ( ./solutions/2706-buy-two-chocolates.js ) |Easy|
2001
+ 2707|[ Extra Characters in a String] ( ./solutions/2707-extra-characters-in-a-string.js ) |Medium|
2001
2002
2715|[ Timeout Cancellation] ( ./solutions/2715-timeout-cancellation.js ) |Easy|
2002
2003
2721|[ Execute Asynchronous Functions in Parallel] ( ./solutions/2721-execute-asynchronous-functions-in-parallel.js ) |Medium|
2003
2004
2722|[ Join Two Arrays by ID] ( ./solutions/2722-join-two-arrays-by-id.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2707. Extra Characters in a String
3
+ * https://leetcode.com/problems/extra-characters-in-a-string/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a 0-indexed string s and a dictionary of words dictionary. You have to break
7
+ * s into one or more non-overlapping substrings such that each substring is present in
8
+ * dictionary. There may be some extra characters in s which are not present in any of the
9
+ * substrings.
10
+ *
11
+ * Return the minimum number of extra characters left over if you break up s optimally.
12
+ */
13
+
14
+ /**
15
+ * @param {string } s
16
+ * @param {string[] } dictionary
17
+ * @return {number }
18
+ */
19
+ var minExtraChar = function ( s , dictionary ) {
20
+ const n = s . length ;
21
+ const dp = new Array ( n + 1 ) . fill ( Infinity ) ;
22
+ dp [ 0 ] = 0 ;
23
+ const dictSet = new Set ( dictionary ) ;
24
+
25
+ for ( let i = 1 ; i <= n ; i ++ ) {
26
+ dp [ i ] = dp [ i - 1 ] + 1 ;
27
+ for ( let j = 0 ; j < i ; j ++ ) {
28
+ if ( dictSet . has ( s . slice ( j , i ) ) ) {
29
+ dp [ i ] = Math . min ( dp [ i ] , dp [ j ] ) ;
30
+ }
31
+ }
32
+ }
33
+
34
+ return dp [ n ] ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments