Skip to content

Commit 5e9dc13

Browse files
committed
Repeated String Match πŸ“š
1 parent 55e3857 commit 5e9dc13

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

β€Žrepeated-string-match.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} A
3+
* @param {string} B
4+
* @return {number}
5+
*/
6+
const repeatedStringMatch = (A, B) => {
7+
const lenA = A.length;
8+
const lenB = B.length;
9+
10+
for (let i = 0; i < lenA; i++) {
11+
let j = 0;
12+
13+
while (j < lenB && B[j] === A[(i + j) % lenA]) {
14+
j++;
15+
}
16+
17+
if (j === lenB) {
18+
return Math.ceil((i + j) / lenA);
19+
}
20+
}
21+
22+
return -1;
23+
};
24+
25+
export { repeatedStringMatch };

0 commit comments

Comments
Β (0)