diff --git a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README.md b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README.md index 6b682e28d7534..07bd82aeb1596 100644 --- a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README.md +++ b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README.md @@ -70,19 +70,118 @@ ```python +class Solution: + def minimumTimeToInitialState(self, word: str, k: int) -> int: + n = len(word) + for i in range(1, 10001): + re = i * k + if re >= n: + return i + if word[re:] == word[:n - re]: + return i + return 0 ``` ```java - +class Solution { + public int minimumTimeToInitialState(String word, int k) { + int n = word.length(); + for (int i = 1; i <= 10000; i++) { + int re = i * k; + if (re >= n) { + return i; + } + String str = word.substring(re); + boolean flag = true; + for (int j = 0; j < str.length(); j++) { + if (str.charAt(j) != word.charAt(j)) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; + } +} ``` ```cpp - +class Solution { +public: + int minimumTimeToInitialState(string word, int k) { + int n = word.length(); + for (int i = 1; i <= 10000; i++) { + int re = i * k; + if (re >= n) { + return i; + } + string str = word.substr(re); + bool flag = true; + for (int j = 0; j < str.length(); j++) { + if (str[j] != word[j]) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; + } +}; ``` ```go +func minimumTimeToInitialState(word string, k int) int { + n := len(word) + for i := 1; i <= 10000; i++ { + re := i * k + if re >= n { + return i + } + str := word[re:] + flag := true + for j := 0; j < len(str); j++ { + if str[j] != word[j] { + flag = false + break + } + } + if flag { + return i + } + } + return 0 +} +``` +```ts +function minimumTimeToInitialState(word: string, k: number): number { + const n = word.length; + for (let i = 1; i <= 10000; i++) { + const re = i * k; + if (re >= n) { + return i; + } + const str = word.substring(re); + let flag = true; + for (let j = 0; j < str.length; j++) { + if (str[j] !== word[j]) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; +} ``` diff --git a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README_EN.md b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README_EN.md index c0808e1e03619..7aa1f3dd01549 100644 --- a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README_EN.md +++ b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/README_EN.md @@ -63,19 +63,118 @@ It can be shown that 4 seconds is the minimum time greater than zero required fo ```python +class Solution: + def minimumTimeToInitialState(self, word: str, k: int) -> int: + n = len(word) + for i in range(1, 10001): + re = i * k + if re >= n: + return i + if word[re:] == word[:n - re]: + return i + return 0 ``` ```java - +class Solution { + public int minimumTimeToInitialState(String word, int k) { + int n = word.length(); + for (int i = 1; i <= 10000; i++) { + int re = i * k; + if (re >= n) { + return i; + } + String str = word.substring(re); + boolean flag = true; + for (int j = 0; j < str.length(); j++) { + if (str.charAt(j) != word.charAt(j)) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; + } +} ``` ```cpp - +class Solution { +public: + int minimumTimeToInitialState(string word, int k) { + int n = word.length(); + for (int i = 1; i <= 10000; i++) { + int re = i * k; + if (re >= n) { + return i; + } + string str = word.substr(re); + bool flag = true; + for (int j = 0; j < str.length(); j++) { + if (str[j] != word[j]) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; + } +}; ``` ```go +func minimumTimeToInitialState(word string, k int) int { + n := len(word) + for i := 1; i <= 10000; i++ { + re := i * k + if re >= n { + return i + } + str := word[re:] + flag := true + for j := 0; j < len(str); j++ { + if str[j] != word[j] { + flag = false + break + } + } + if flag { + return i + } + } + return 0 +} +``` +```ts +function minimumTimeToInitialState(word: string, k: number): number { + const n = word.length; + for (let i = 1; i <= 10000; i++) { + const re = i * k; + if (re >= n) { + return i; + } + const str = word.substring(re); + let flag = true; + for (let j = 0; j < str.length; j++) { + if (str[j] !== word[j]) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; +} ``` diff --git a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.cpp b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.cpp new file mode 100644 index 0000000000000..58457d00178a5 --- /dev/null +++ b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int minimumTimeToInitialState(string word, int k) { + int n = word.length(); + for (int i = 1; i <= 10000; i++) { + int re = i * k; + if (re >= n) { + return i; + } + string str = word.substr(re); + bool flag = true; + for (int j = 0; j < str.length(); j++) { + if (str[j] != word[j]) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; + } +}; diff --git a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.go b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.go new file mode 100644 index 0000000000000..08418e0058627 --- /dev/null +++ b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.go @@ -0,0 +1,21 @@ +func minimumTimeToInitialState(word string, k int) int { + n := len(word) + for i := 1; i <= 10000; i++ { + re := i * k + if re >= n { + return i + } + str := word[re:] + flag := true + for j := 0; j < len(str); j++ { + if str[j] != word[j] { + flag = false + break + } + } + if flag { + return i + } + } + return 0 +} diff --git a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.java b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.java new file mode 100644 index 0000000000000..b29531ef7c092 --- /dev/null +++ b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public int minimumTimeToInitialState(String word, int k) { + int n = word.length(); + for (int i = 1; i <= 10000; i++) { + int re = i * k; + if (re >= n) { + return i; + } + String str = word.substring(re); + boolean flag = true; + for (int j = 0; j < str.length(); j++) { + if (str.charAt(j) != word.charAt(j)) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; + } +} diff --git a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.py b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.py new file mode 100644 index 0000000000000..95f841995b931 --- /dev/null +++ b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def minimumTimeToInitialState(self, word: str, k: int) -> int: + n = len(word) + for i in range(1, 10001): + re = i * k + if re >= n: + return i + if word[re:] == word[: n - re]: + return i + return 0 diff --git a/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.ts b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.ts new file mode 100644 index 0000000000000..96a00ac553457 --- /dev/null +++ b/solution/3000-3099/3029.Minimum Time to Revert Word to Initial State I/Solution.ts @@ -0,0 +1,21 @@ +function minimumTimeToInitialState(word: string, k: number): number { + const n = word.length; + for (let i = 1; i <= 10000; i++) { + const re = i * k; + if (re >= n) { + return i; + } + const str = word.substring(re); + let flag = true; + for (let j = 0; j < str.length; j++) { + if (str[j] !== word[j]) { + flag = false; + break; + } + } + if (flag) { + return i; + } + } + return 0; +}