Skip to content

Commit 5b9cd0c

Browse files
committed
Solution for 1003, 1004 problems
1 parent 116ba7a commit 5b9cd0c

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

1000-1100q/1003.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'''
2+
We are given that the string "abc" is valid.
3+
4+
From any valid string V, we may split V into two pieces X and Y such that X + Y (X concatenated with Y) is equal to V. (X or Y may be empty.) Then, X + "abc" + Y is also valid.
5+
6+
If for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". Examples of invalid strings are: "abccba", "ab", "cababc", "bac".
7+
8+
Return true if and only if the given string S is valid.
9+
10+
11+
12+
Example 1:
13+
14+
Input: "aabcbc"
15+
Output: true
16+
Explanation:
17+
We start with the valid string "abc".
18+
Then we can insert another "abc" between "a" and "bc", resulting in "a" + "abc" + "bc" which is "aabcbc".
19+
Example 2:
20+
21+
Input: "abcabcababcc"
22+
Output: true
23+
Explanation:
24+
"abcabcabc" is valid after consecutive insertings of "abc".
25+
Then we can insert "abc" before the last letter, resulting in "abcabcab" + "abc" + "c" which is "abcabcababcc".
26+
Example 3:
27+
28+
Input: "abccba"
29+
Output: false
30+
Example 4:
31+
32+
Input: "cababc"
33+
Output: false
34+
35+
36+
Note:
37+
38+
1 <= S.length <= 20000
39+
S[i] is 'a', 'b', or 'c'
40+
'''
41+
42+
class Solution(object):
43+
def isValid(self, S):
44+
"""
45+
:type S: str
46+
:rtype: bool
47+
"""
48+
stack = []
49+
if not S:
50+
return False
51+
52+
for char in S:
53+
if char == 'a':
54+
stack.append('a')
55+
if char == 'b':
56+
if not stack:
57+
return False
58+
if stack[-1] == 'a':
59+
stack.pop()
60+
stack.append(char)
61+
if char == 'c':
62+
if not stack:
63+
return False
64+
if stack[-1] == 'b':
65+
stack.pop()
66+
return len(stack) == 0
67+

1000-1100q/1004.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given an array A of 0s and 1s, we may change up to K values from 0 to 1.
3+
4+
Return the length of the longest (contiguous) subarray that contains only 1s.
5+
6+
7+
8+
Example 1:
9+
10+
Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2
11+
Output: 6
12+
Explanation:
13+
[1,1,1,0,0,1,1,1,1,1,1]
14+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
15+
Example 2:
16+
17+
Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3
18+
Output: 10
19+
Explanation:
20+
[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
21+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
22+
23+
24+
Note:
25+
26+
1 <= A.length <= 20000
27+
0 <= K <= A.length
28+
A[i] is 0 or 1
29+
'''
30+
31+
class Solution(object):
32+
def longestOnes(self, A, K):
33+
"""
34+
:type A: List[int]
35+
:type K: int
36+
:rtype: int
37+
"""
38+
start_index = 0
39+
for end_index in range(0, len(A)):
40+
K -= 1-A[end_index]
41+
if K < 0:
42+
K += 1-A[start_index]
43+
start_index += 1
44+
return end_index-start_index+1

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1414
|1014|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/) | [Python](./1000-1100q/1014.py) | Medium|
1515
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Python](./1000-1100q/1013.py)|Easy|
1616
|1012|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [Python](./1000-1100q/1012.py)|Easy|
17+
|1004|[Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii)|[Python](./1000-1100q/1004.py)|Medium|
18+
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions)|[Python](./1000-1100q/1003.py)|Medium|
1719
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters)|[Python](./1000-1100q/1002.py)|Easy|
1820

1921
##### [Problems 900-1000](./900-1000q/)

0 commit comments

Comments
 (0)