Skip to content

Commit 9a2d5db

Browse files
committed
Solution of Next Time and License Key
1 parent 7141799 commit 9a2d5db

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

400-500Q/482.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.
3+
4+
Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.
5+
6+
Given a non-empty string S and a number K, format the string according to the rules described above.
7+
8+
Example 1:
9+
Input: S = "5F3Z-2e-9-w", K = 4
10+
11+
Output: "5F3Z-2E9W"
12+
13+
Explanation: The string S has been split into two parts, each part has 4 characters.
14+
Note that the two extra dashes are not needed and can be removed.
15+
Example 2:
16+
Input: S = "2-5g-3-J", K = 2
17+
18+
Output: "2-5G-3J"
19+
20+
Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
21+
Note:
22+
The length of string S will not exceed 12,000, and K is a positive integer.
23+
String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
24+
String S is non-empty.
25+
'''
26+
27+
class Solution(object):
28+
def licenseKeyFormatting(self, S, K):
29+
"""
30+
:type S: str
31+
:type K: int
32+
:rtype: str
33+
"""
34+
S = S.replace('-','').upper()
35+
result = ""
36+
37+
if len(S)%K == 0:
38+
for index in range(0, len(S), K):
39+
result += S[index:index+K] + "-"
40+
else:
41+
result = S[:len(S)%K] + "-"
42+
for index in range(len(S)%K, len(S), K):
43+
result += S[index:index+K] + "-"
44+
return result[:-1]

600-700q/681.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.
3+
4+
You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.
5+
6+
Example 1:
7+
8+
Input: "19:34"
9+
Output: "19:39"
10+
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later.
11+
Example 2:
12+
13+
Input: "23:59"
14+
Output: "22:22"
15+
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.
16+
'''
17+
18+
class Solution(object):
19+
def nextClosestTime(self, time):
20+
current_time = 60*int(time[:2]) + int(time[3:])
21+
allowed = {int(x) for x in time if x != ':'}
22+
result = 24*60
23+
ans = current_time
24+
for h1, h2, m1, m2 in itertools.product(allowed, repeat=4):
25+
hours, minutes = 10*h1+h2, 10*m1+m2
26+
if hours < 24 and minutes < 60:
27+
elapsed = 60*hours + minutes
28+
diff = (current_time - elapsed)%(24*60)
29+
if 0 < diff < result:
30+
result = diff
31+
ans = elapsed
32+
33+
return "{:02d}:{:02d}".format(divmod(ans, 60))

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
8585
##### [Problems 600-700](./600-700q/)
8686
| # | Title | Solution | Difficulty |
8787
|---| ----- | -------- | ---------- |
88+
|681|[Next Closest Time ](https://leetcode.com/problems/next-closest-time)|[Python](./600-700q/681.py)|Medium|
8889
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence)|[Python](./600-700/674.py)|Easy|
8990
|673|[Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence)|[Python](./600-700q/673.py)|Medium|
9091

9192

9293
##### [Problems 400-500](./400-500Q/)
9394
| # | Title | Solution | Difficulty |
9495
|---| ----- | -------- | ---------- |
96+
|482|[License Key Formatting](https://leetcode.com/problems/license-key-formatting)|[Python](./400-500q/482.py)|Easy|
9597
|454|[4Sum II](https://leetcode.com/problems/4sum-ii/)|[Python](./400-500Q/454.py)|Medium|
9698
|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array)|[Python](./400-500q/448.py)|Easy|
9799
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array)|[Python](./400-500q/442.py)|Easy|

0 commit comments

Comments
 (0)