Skip to content

Commit eb35369

Browse files
committed
Add solution #686
1 parent 7725f7c commit eb35369

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
606|[Construct String from Binary Tree](./0606-construct-string-from-binary-tree.js)|Easy|
1919
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
2020
648|[Replace Words](./0648-replace-words.js)|Medium|
21+
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
2122
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
2223
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
2324
804|[Unique Morse Code Words](./0804-unique-morse-code-words.js)|Easy|
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
};

0 commit comments

Comments
 (0)