Skip to content

Commit

Permalink
feat: update solutions to lc problems: No.0859,0860
Browse files Browse the repository at this point in the history
* No.0859.Buddy Strings
* No.0860.Lemonade Change
  • Loading branch information
yanglbme committed Jul 9, 2022
1 parent 2809659 commit dc607f2
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 70 deletions.
4 changes: 2 additions & 2 deletions solution/0800-0899/0859.Buddy Strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class Solution:
m, n = len(s), len(goal)
if m != n:
return False
diff = sum(1 for i in range(n) if s[i] != goal[i])
cnt1, cnt2 = Counter(s), Counter(goal)
if cnt1 != cnt2:
return False
return diff == 2 or (diff == 0 and any(e > 1 for e in cnt1.values()))
diff = sum(s[i] != goal[i] for i in range(n))
return diff == 2 or (diff == 0 and any(v > 1 for v in cnt1.values()))
```

### **Java**
Expand Down
4 changes: 2 additions & 2 deletions solution/0800-0899/0859.Buddy Strings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class Solution:
m, n = len(s), len(goal)
if m != n:
return False
diff = sum(1 for i in range(n) if s[i] != goal[i])
cnt1, cnt2 = Counter(s), Counter(goal)
if cnt1 != cnt2:
return False
return diff == 2 or (diff == 0 and any(e > 1 for e in cnt1.values()))
diff = sum(s[i] != goal[i] for i in range(n))
return diff == 2 or (diff == 0 and any(v > 1 for v in cnt1.values()))
```

### **Java**
Expand Down
4 changes: 2 additions & 2 deletions solution/0800-0899/0859.Buddy Strings/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ def buddyStrings(self, s: str, goal: str) -> bool:
m, n = len(s), len(goal)
if m != n:
return False
diff = sum(1 for i in range(n) if s[i] != goal[i])
cnt1, cnt2 = Counter(s), Counter(goal)
if cnt1 != cnt2:
return False
return diff == 2 or (diff == 0 and any(e > 1 for e in cnt1.values()))
diff = sum(s[i] != goal[i] for i in range(n))
return diff == 2 or (diff == 0 and any(v > 1 for v in cnt1.values()))
95 changes: 74 additions & 21 deletions solution/0800-0899/0860.Lemonade Change/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@
```python
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five, ten = 0, 0
for bill in bills:
if bill == 5:
five = ten = 0
for v in bills:
if v == 5:
five += 1
elif bill == 10:
elif v == 10:
ten += 1
five -= 1
elif bill == 20:
if ten > 0:
else:
if ten:
ten -= 1
five -= 1
else:
Expand All @@ -87,31 +87,84 @@ class Solution:
```java
class Solution {
public boolean lemonadeChange(int[] bills) {
int fives = 0, tens = 0;
for (int bill : bills) {
if (bill == 5) {
++fives;
} else if (bill == 10) {
++tens;
if (--fives < 0) {
return false;
}
int five = 0, ten = 0;
for (int v : bills) {
if (v == 5) {
++five;
} else if (v == 10) {
++ten;
--five;
} else {
if (tens >= 1 && fives >= 1) {
--tens;
--fives;
} else if (fives >= 3) {
fives -= 3;
if (ten > 0) {
--ten;
--five;
} else {
return false;
five -= 3;
}
}
if (five < 0) {
return false;
}
}
return true;
}
}
```

### **C++**

```cpp
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five = 0, ten = 0;
for (int v : bills)
{
if (v == 5) ++five;
else if (v == 10)
{
++ten;
--five;
}
else
{
if (ten) --ten, --five;
else five -= 3;
}
if (five < 0) return false;
}
return true;
}
};
```
### **Go**
```go
func lemonadeChange(bills []int) bool {
five, ten := 0, 0
for _, v := range bills {
if v == 5 {
five++
} else if v == 10 {
ten++
five--
} else {
if ten > 0 {
ten--
five--
} else {
five -= 3
}
}
if five < 0 {
return false
}
}
return true
}
```

### **TypeScript**

```ts
Expand Down
95 changes: 74 additions & 21 deletions solution/0800-0899/0860.Lemonade Change/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ Since not every customer received the correct change, the answer is false.
```python
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five, ten = 0, 0
for bill in bills:
if bill == 5:
five = ten = 0
for v in bills:
if v == 5:
five += 1
elif bill == 10:
elif v == 10:
ten += 1
five -= 1
elif bill == 20:
if ten > 0:
else:
if ten:
ten -= 1
five -= 1
else:
Expand All @@ -75,31 +75,84 @@ class Solution:
```java
class Solution {
public boolean lemonadeChange(int[] bills) {
int fives = 0, tens = 0;
for (int bill : bills) {
if (bill == 5) {
++fives;
} else if (bill == 10) {
++tens;
if (--fives < 0) {
return false;
}
int five = 0, ten = 0;
for (int v : bills) {
if (v == 5) {
++five;
} else if (v == 10) {
++ten;
--five;
} else {
if (tens >= 1 && fives >= 1) {
--tens;
--fives;
} else if (fives >= 3) {
fives -= 3;
if (ten > 0) {
--ten;
--five;
} else {
return false;
five -= 3;
}
}
if (five < 0) {
return false;
}
}
return true;
}
}
```

### **C++**

```cpp
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five = 0, ten = 0;
for (int v : bills)
{
if (v == 5) ++five;
else if (v == 10)
{
++ten;
--five;
}
else
{
if (ten) --ten, --five;
else five -= 3;
}
if (five < 0) return false;
}
return true;
}
};
```
### **Go**
```go
func lemonadeChange(bills []int) bool {
five, ten := 0, 0
for _, v := range bills {
if v == 5 {
five++
} else if v == 10 {
ten++
five--
} else {
if ten > 0 {
ten--
five--
} else {
five -= 3
}
}
if five < 0 {
return false
}
}
return true
}
```

### **TypeScript**

```ts
Expand Down
22 changes: 22 additions & 0 deletions solution/0800-0899/0860.Lemonade Change/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five = 0, ten = 0;
for (int v : bills)
{
if (v == 5) ++five;
else if (v == 10)
{
++ten;
--five;
}
else
{
if (ten) --ten, --five;
else five -= 3;
}
if (five < 0) return false;
}
return true;
}
};
22 changes: 22 additions & 0 deletions solution/0800-0899/0860.Lemonade Change/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
func lemonadeChange(bills []int) bool {
five, ten := 0, 0
for _, v := range bills {
if v == 5 {
five++
} else if v == 10 {
ten++
five--
} else {
if ten > 0 {
ten--
five--
} else {
five -= 3
}
}
if five < 0 {
return false
}
}
return true
}
31 changes: 15 additions & 16 deletions solution/0800-0899/0860.Lemonade Change/Solution.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
class Solution {
public boolean lemonadeChange(int[] bills) {
int fives = 0, tens = 0;
for (int bill : bills) {
if (bill == 5) {
++fives;
} else if (bill == 10) {
++tens;
if (--fives < 0) {
return false;
}
int five = 0, ten = 0;
for (int v : bills) {
if (v == 5) {
++five;
} else if (v == 10) {
++ten;
--five;
} else {
if (tens >= 1 && fives >= 1) {
--tens;
--fives;
} else if (fives >= 3) {
fives -= 3;
if (ten > 0) {
--ten;
--five;
} else {
return false;
five -= 3;
}
}
if (five < 0) {
return false;
}
}
return true;
}
}
}
12 changes: 6 additions & 6 deletions solution/0800-0899/0860.Lemonade Change/Solution.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five, ten = 0, 0
for bill in bills:
if bill == 5:
five = ten = 0
for v in bills:
if v == 5:
five += 1
elif bill == 10:
elif v == 10:
ten += 1
five -= 1
elif bill == 20:
if ten > 0:
else:
if ten:
ten -= 1
five -= 1
else:
Expand Down

0 comments on commit dc607f2

Please sign in to comment.