Skip to content

Commit e64a415

Browse files
committed
Add solution #3029
1 parent e81783a commit e64a415

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,7 @@
21382138
3025|[Find the Number of Ways to Place People I](./solutions/3025-find-the-number-of-ways-to-place-people-i.js)|Medium|
21392139
3027|[Find the Number of Ways to Place People II](./solutions/3027-find-the-number-of-ways-to-place-people-ii.js)|Hard|
21402140
3028|[Ant on the Boundary](./solutions/3028-ant-on-the-boundary.js)|Easy|
2141+
3029|[Minimum Time to Revert Word to Initial State I](./solutions/3029-minimum-time-to-revert-word-to-initial-state-i.js)|Medium|
21412142
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
21422143
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
21432144
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 3029. Minimum Time to Revert Word to Initial State I
3+
* https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-i/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed string word and an integer k.
7+
*
8+
* At every second, you must perform the following operations:
9+
* - Remove the first k characters of word.
10+
* - Add any k characters to the end of word.
11+
*
12+
* Note that you do not necessarily need to add the same characters that you removed.
13+
* However, you must perform both operations at every second.
14+
*
15+
* Return the minimum time greater than zero required for word to revert to its initial state.
16+
*/
17+
18+
/**
19+
* @param {string} word
20+
* @param {number} k
21+
* @return {number}
22+
*/
23+
var minimumTimeToInitialState = function(word, k) {
24+
const n = word.length;
25+
26+
for (let i = 1; i * k <= n; i++) {
27+
if (word.slice(i * k) === word.slice(0, n - i * k)) {
28+
return i;
29+
}
30+
}
31+
32+
return Math.ceil(n / k);
33+
};

0 commit comments

Comments
 (0)