Skip to content

Commit

Permalink
Solution of 1088 and 1092
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit244 committed Jun 25, 2019
1 parent 43c329d commit 6418c6c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
29 changes: 28 additions & 1 deletion 1000-1100q/1088.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,31 @@
Note:
1 <= N <= 10^9
'''
'''

class Solution(object):
result = 0
def confusingNumberII(self, N):
"""
:type N: int
:rtype: int
"""
original_a = [0, 1, 6, 8, 9]
o_rotation = [0, 1, 9, 8, 6]

def recursive(original, rotation, digit, N):
if original > N:
return
if original and original != rotation:
self.result += 1

start = original == 0
if digit >= 1000000000:
return
for index in range(start, 5):
recursive(original * 10 + original_a[index], rotation + o_rotation[index]*digit, digit*10, N)

recursive(0, 0, 1, N)
if (N == 1000000000):
self.result += 1
return self.result
55 changes: 55 additions & 0 deletions 1000-1100q/1092.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'''
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.
(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.)
Example 1:
Input: str1 = "abac", str2 = "cab"
Output: "cabac"
Explanation:
str1 = "abac" is a substring of "cabac" because we can delete the first "c".
str2 = "cab" is a substring of "cabac" because we can delete the last "ac".
The answer provided is the shortest such string that satisfies these properties.
Note:
1 <= str1.length, str2.length <= 1000
str1 and str2 consist of lowercase English letters.
'''

class Solution(object):
def shortestCommonSupersequence(self, str1, str2):
"""
:type str1: str
:type str2: str
:rtype: str
"""
def lcs(A, B):
n, m = len(A)+1, len(B)+1
dp = [["" for _ in range(m)] for _ in range(n)]
for index_i in range(1, n):
for index_j in range(1, m):
if A[index_i-1] == B[index_j-1]:
dp[index_i][index_j] = dp[index_i-1][index_j-1] + A[index_i - 1]
else:
dp[index_i][index_j] = max(dp[index_i-1][index_j], dp[index_i][index_j-1], key=len)
return dp[-1][-1]

result = ""
index_i, index_j = 0, 0
for s in lcs(str1, str2):
while str1[index_i] != s:
result += str1[index_i]
index_i += 1
while str2[index_j] != s:
result += str2[index_j]
index_j += 1

result += s
index_i, index_j = index_i+1, index_j+1

return result + str1[index_i:] + str2[index_j:]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
##### [Problems 1000-1100](./1000-1100q/)
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1092|[Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence)|[Python](./1000-1100q/1092.py)|Hard|
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix)|[Python](./1000-1100q/1091.py)|Medium|
|1090|[Largest Values From Labels ](https://leetcode.com/problems/largest-values-from-labels)|[Python](./1000-1100q/1090.py)|Medium|
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros)|[Python](./1000-1100q/1089.py)|Easy|
|1088|[]
|1088|[Confusing Number II](https://leetcode.com/problems/confusing-number-ii)|[Python](./1000-1100q/1088.py)|Hard|
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion)|[Python](./1000-1100q/1087.py)|Medium|
|1086|[High Five](https://leetcode.com/problems/high-five)|[Python](./1000-1100q/1086.py)|Medium|
|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|
Expand Down

0 comments on commit 6418c6c

Please sign in to comment.