File tree 2 files changed +26
-0
lines changed
2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 18
18
606|[ Construct String from Binary Tree] ( ./0606-construct-string-from-binary-tree.js ) |Easy|
19
19
617|[ Merge Two Binary Trees] ( ./0617-merge-two-binary-trees.js ) |Easy|
20
20
648|[ Replace Words] ( ./0648-replace-words.js ) |Medium|
21
+ 686|[ Repeated String Match] ( ./0686-repeated-string-match.js ) |Easy|
21
22
739|[ Daily Temperatures] ( ./0739-daily-temperatures.js ) |Medium|
22
23
791|[ Custom Sort String] ( ./0791-custom-sort-string.js ) |Medium|
23
24
804|[ Unique Morse Code Words] ( ./0804-unique-morse-code-words.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 686. Repeated String Match
3
+ * https://leetcode.com/problems/repeated-string-match/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given two strings A and B, find the minimum number of times A has to be
7
+ * repeated such that B is a substring of it. If no such solution, return -1.
8
+ *
9
+ * For example, with A = "abcd" and B = "cdabcdab".
10
+ *
11
+ * Return 3, because by repeating A three times (“abcdabcdabcd”), B is a
12
+ * substring of it; and B is not a substring of A repeated two times ("abcdabcd").
13
+ */
14
+
15
+ /**
16
+ * @param {string } A
17
+ * @param {string } B
18
+ * @return {number }
19
+ */
20
+ var repeatedStringMatch = function ( A , B ) {
21
+ const length = Math . ceil ( B . length / A . length ) ;
22
+ if ( A . repeat ( length ) . includes ( B ) ) return length ;
23
+ if ( ( A . repeat ( length ) + A ) . includes ( B ) ) return length + 1 ;
24
+ return - 1 ;
25
+ } ;
You can’t perform that action at this time.
0 commit comments