Skip to content

Commit e3052b5

Browse files
authored
Merge pull request #4123 from sreevidya-16/main
Add solution to LC problem 3031
2 parents c2485ec + 27342a9 commit e3052b5

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: minimum-time-to-revert-word-to-initial-state-ii
3+
title: Minimum Time to Revert Word to Initial State II
4+
sidebar_label: Minimum Time to Revert Word to Initial State II
5+
tags: [String, Simulation, C++, Python, Java]
6+
description: Solve the problem of finding the minimum time required for a word to revert to its initial state after performing specific operations.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
You are given a 0-indexed string `word` and an integer `k`.
14+
15+
At every second, you must perform the following operations:
16+
17+
1. Remove the first `k` characters of `word`.
18+
2. Add any `k` characters to the end of `word`.
19+
20+
Note that you do not necessarily need to add the same characters that you removed. However, you must perform both operations at every second.
21+
22+
Return the minimum time greater than zero required for `word` to revert to its initial state.
23+
24+
### Examples
25+
26+
**Example 1:**
27+
```
28+
Input: word = "abacaba", k = 3
29+
Output: 2
30+
```
31+
**Explanation:**
32+
- At the 1st second, we remove characters "aba" from the prefix of `word`, and add characters "bac" to the end of `word`. Thus, `word` becomes "cababac".
33+
- At the 2nd second, we remove characters "cab" from the prefix of `word`, and add "aba" to the end of `word`. Thus, `word` becomes "abacaba" and reverts to its initial state.
34+
- It can be shown that 2 seconds is the minimum time greater than zero required for `word` to revert to its initial state.
35+
36+
**Example 2:**
37+
```
38+
Input: word = "abacaba", k = 4
39+
Output: 1
40+
```
41+
**Explanation:**
42+
- At the 1st second, we remove characters "abac" from the prefix of `word`, and add characters "caba" to the end of `word`. Thus, `word` becomes "abacaba" and reverts to its initial state.
43+
- It can be shown that 1 second is the minimum time greater than zero required for `word` to revert to its initial state.
44+
45+
### Constraints
46+
47+
- $1 \leq \text{word.length} \leq 10^6$
48+
- $1 \leq k \leq \text{word.length}$
49+
- `word` consists only of lowercase English letters.
50+
51+
## Solution
52+
53+
### Intuition
54+
55+
To find the minimum time required for the word to revert to its initial state, we can simulate the process of removing and adding characters. We aim to identify when the word returns to its original configuration after `k` characters have been shifted `n` times.
56+
57+
### Time Complexity and Space Complexity Analysis
58+
59+
- **Time Complexity**: The solution requires simulating the shifting process, which has a worst-case complexity of $O(n^2)$. This is because, in the worst-case scenario, we might need to simulate `n` shifts, each of which could involve comparing `n` characters.
60+
- **Space Complexity**: The space complexity is $O(n)$, where `n` is the length of the string `word`.
61+
62+
### Code
63+
64+
#### C++
65+
66+
```cpp
67+
class Solution {
68+
public:
69+
int minimumTimeToRevert(string word, int k) {
70+
string original = word;
71+
int n = word.size();
72+
for (int i = 1; i <= n; ++i) {
73+
string prefix = word.substr(0, k);
74+
word = word.substr(k) + prefix;
75+
if (word == original) {
76+
return i;
77+
}
78+
}
79+
return n; // Default case, should never hit due to problem constraints
80+
}
81+
};
82+
```
83+
84+
#### Python
85+
```python
86+
class Solution:
87+
def minimumTimeToRevert(self, word: str, k: int) -> int:
88+
original = word
89+
n = len(word)
90+
for i in range(1, n + 1):
91+
prefix = word[:k]
92+
word = word[k:] + prefix
93+
if word == original:
94+
return i
95+
return n # Default case, should never hit due to problem constraints
96+
```
97+
#### Java
98+
```java
99+
class Solution {
100+
public int minimumTimeToRevert(String word, int k) {
101+
String original = word;
102+
int n = word.length();
103+
for (int i = 1; i <= n; ++i) {
104+
String prefix = word.substring(0, k);
105+
word = word.substring(k) + prefix;
106+
if (word.equals(original)) {
107+
return i;
108+
}
109+
}
110+
return n; // Default case, should never hit due to problem constraints
111+
}
112+
}
113+
```

0 commit comments

Comments
 (0)