Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1933 #2000

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add solutions to lc problem: No.1933
No.1933.Check if String Is Decomposable Into Value-Equal Substrings
yanglbme committed Nov 22, 2023

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
commit 63d3da7b8243a007de0ebf1ab4bbcf4fd01c64a3
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@

遍历结束后,判断是否出现过长度为 $2$ 的子字符串,若没有,返回 `false`,否则返回 `true`。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

@@ -87,6 +87,20 @@ class Solution:
return cnt2 == 1
```

```python
class Solution:
def isDecomposable(self, s: str) -> bool:
cnt2 = 0
for _, g in groupby(s):
m = len(list(g))
if m % 3 == 1:
return False
cnt2 += m % 3 == 2
if cnt2 > 1:
return False
return cnt2 == 1
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -120,13 +134,19 @@ class Solution {
class Solution {
public:
bool isDecomposable(string s) {
int i = 0, n = s.size();
int cnt2 = 0;
while (i < n) {
for (int i = 0, n = s.size(); i < n;) {
int j = i;
while (j < n && s[j] == s[i]) ++j;
if ((j - i) % 3 == 1) return false;
if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
while (j < n && s[j] == s[i]) {
++j;
}
if ((j - i) % 3 == 1) {
return false;
}
cnt2 += (j - i) % 3 == 2;
if (cnt2 > 1) {
return false;
}
i = j;
}
return cnt2 == 1;
@@ -160,6 +180,29 @@ func isDecomposable(s string) bool {
}
```

### **TypeScript**

```ts
function isDecomposable(s: string): boolean {
const n = s.length;
let cnt2 = 0;
for (let i = 0; i < n; ) {
let j = i;
while (j < n && s[j] === s[i]) {
++j;
}
if ((j - i) % 3 === 1) {
return false;
}
if ((j - i) % 3 === 2 && ++cnt2 > 1) {
return false;
}
i = j;
}
return cnt2 === 1;
}
```

### **...**

```
Original file line number Diff line number Diff line change
@@ -52,6 +52,14 @@

## Solutions

**Solution 1: Two Pointers**

We traverse the string $s$, using two pointers $i$ and $j$ to count the length of each equal substring. If the length modulo $3$ is $1$, it means that the length of this substring does not meet the requirements, so we return `false`. If the length modulo $3$ is $2$, it means that a substring of length $2$ has appeared. If a substring of length $2$ has appeared before, return `false`, otherwise assign the value of $j$ to $i$ and continue to traverse.

After the traversal, check whether a substring of length $2$ has appeared. If not, return `false`, otherwise return `true`.

The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
@@ -74,6 +82,20 @@ class Solution:
return cnt2 == 1
```

```python
class Solution:
def isDecomposable(self, s: str) -> bool:
cnt2 = 0
for _, g in groupby(s):
m = len(list(g))
if m % 3 == 1:
return False
cnt2 += m % 3 == 2
if cnt2 > 1:
return False
return cnt2 == 1
```

### **Java**

```java
@@ -105,13 +127,19 @@ class Solution {
class Solution {
public:
bool isDecomposable(string s) {
int i = 0, n = s.size();
int cnt2 = 0;
while (i < n) {
for (int i = 0, n = s.size(); i < n;) {
int j = i;
while (j < n && s[j] == s[i]) ++j;
if ((j - i) % 3 == 1) return false;
if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
while (j < n && s[j] == s[i]) {
++j;
}
if ((j - i) % 3 == 1) {
return false;
}
cnt2 += (j - i) % 3 == 2;
if (cnt2 > 1) {
return false;
}
i = j;
}
return cnt2 == 1;
@@ -145,6 +173,29 @@ func isDecomposable(s string) bool {
}
```

### **TypeScript**

```ts
function isDecomposable(s: string): boolean {
const n = s.length;
let cnt2 = 0;
for (let i = 0; i < n; ) {
let j = i;
while (j < n && s[j] === s[i]) {
++j;
}
if ((j - i) % 3 === 1) {
return false;
}
if ((j - i) % 3 === 2 && ++cnt2 > 1) {
return false;
}
i = j;
}
return cnt2 === 1;
}
```

### **...**

```
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
class Solution {
public:
bool isDecomposable(string s) {
int i = 0, n = s.size();
int cnt2 = 0;
while (i < n) {
int j = i;
while (j < n && s[j] == s[i]) ++j;
if ((j - i) % 3 == 1) return false;
if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
i = j;
}
return cnt2 == 1;
}
class Solution {
public:
bool isDecomposable(string s) {
int cnt2 = 0;
for (int i = 0, n = s.size(); i < n;) {
int j = i;
while (j < n && s[j] == s[i]) {
++j;
}
if ((j - i) % 3 == 1) {
return false;
}
cnt2 += (j - i) % 3 == 2;
if (cnt2 > 1) {
return false;
}
i = j;
}
return cnt2 == 1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function isDecomposable(s: string): boolean {
const n = s.length;
let cnt2 = 0;
for (let i = 0; i < n; ) {
let j = i;
while (j < n && s[j] === s[i]) {
++j;
}
if ((j - i) % 3 === 1) {
return false;
}
if ((j - i) % 3 === 2 && ++cnt2 > 1) {
return false;
}
i = j;
}
return cnt2 === 1;
}