Skip to content

Commit 6418c6c

Browse files
committed
Solution of 1088 and 1092
1 parent 43c329d commit 6418c6c

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

1000-1100q/1088.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,31 @@
3030
Note:
3131
3232
1 <= N <= 10^9
33-
'''
33+
'''
34+
35+
class Solution(object):
36+
result = 0
37+
def confusingNumberII(self, N):
38+
"""
39+
:type N: int
40+
:rtype: int
41+
"""
42+
original_a = [0, 1, 6, 8, 9]
43+
o_rotation = [0, 1, 9, 8, 6]
44+
45+
def recursive(original, rotation, digit, N):
46+
if original > N:
47+
return
48+
if original and original != rotation:
49+
self.result += 1
50+
51+
start = original == 0
52+
if digit >= 1000000000:
53+
return
54+
for index in range(start, 5):
55+
recursive(original * 10 + original_a[index], rotation + o_rotation[index]*digit, digit*10, N)
56+
57+
recursive(0, 0, 1, N)
58+
if (N == 1000000000):
59+
self.result += 1
60+
return self.result

1000-1100q/1092.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If multiple answers exist, you may return any of them.
3+
4+
(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)
5+
6+
7+
8+
Example 1:
9+
10+
Input: str1 = "abac", str2 = "cab"
11+
Output: "cabac"
12+
Explanation:
13+
str1 = "abac" is a substring of "cabac" because we can delete the first "c".
14+
str2 = "cab" is a substring of "cabac" because we can delete the last "ac".
15+
The answer provided is the shortest such string that satisfies these properties.
16+
17+
18+
Note:
19+
20+
1 <= str1.length, str2.length <= 1000
21+
str1 and str2 consist of lowercase English letters.
22+
'''
23+
24+
class Solution(object):
25+
def shortestCommonSupersequence(self, str1, str2):
26+
"""
27+
:type str1: str
28+
:type str2: str
29+
:rtype: str
30+
"""
31+
def lcs(A, B):
32+
n, m = len(A)+1, len(B)+1
33+
dp = [["" for _ in range(m)] for _ in range(n)]
34+
for index_i in range(1, n):
35+
for index_j in range(1, m):
36+
if A[index_i-1] == B[index_j-1]:
37+
dp[index_i][index_j] = dp[index_i-1][index_j-1] + A[index_i - 1]
38+
else:
39+
dp[index_i][index_j] = max(dp[index_i-1][index_j], dp[index_i][index_j-1], key=len)
40+
return dp[-1][-1]
41+
42+
result = ""
43+
index_i, index_j = 0, 0
44+
for s in lcs(str1, str2):
45+
while str1[index_i] != s:
46+
result += str1[index_i]
47+
index_i += 1
48+
while str2[index_j] != s:
49+
result += str2[index_j]
50+
index_j += 1
51+
52+
result += s
53+
index_i, index_j = index_i+1, index_j+1
54+
55+
return result + str1[index_i:] + str2[index_j:]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1515
##### [Problems 1000-1100](./1000-1100q/)
1616
| # | Title | Solution | Difficulty |
1717
|---| ----- | -------- | ---------- |
18+
|1092|[Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence)|[Python](./1000-1100q/1092.py)|Hard|
1819
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix)|[Python](./1000-1100q/1091.py)|Medium|
1920
|1090|[Largest Values From Labels ](https://leetcode.com/problems/largest-values-from-labels)|[Python](./1000-1100q/1090.py)|Medium|
2021
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros)|[Python](./1000-1100q/1089.py)|Easy|
21-
|1088|[]
22+
|1088|[Confusing Number II](https://leetcode.com/problems/confusing-number-ii)|[Python](./1000-1100q/1088.py)|Hard|
2223
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion)|[Python](./1000-1100q/1087.py)|Medium|
2324
|1086|[High Five](https://leetcode.com/problems/high-five)|[Python](./1000-1100q/1086.py)|Medium|
2425
|1085|[Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number)|[Python](./1000-1100q/1085.py)|Medium|

0 commit comments

Comments
 (0)