Skip to content

Commit 6df3914

Browse files
alril 9
1 parent 9a3312e commit 6df3914

6 files changed

+242
-2
lines changed

Challenges/2021/April-LeetCoding-Challenge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ None
2525
||Title|Solution|Difficulty|
2626
| ----: | --- | --- | --- |
2727
|17.|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Python](/Medium/17.LetterCombinationsofaPhoneNumber.py)|Medium|
28+
|953.|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Python](/Easy/953.VerifyinganAlienDictionary.py)|Easy|
2829

2930
## License
3031
The code is open-source and licensed under the [MIT License](/LICENSE).
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''
2+
You are given a string word that consists of digits and
3+
lowercase English letters.
4+
5+
You will replace every non-digit character with a space.
6+
For example, "a123bc34d8ef34" will become " 123 34 8 34".
7+
Notice that you are left with some integers that are
8+
separated by at least one space: "123", "34", "8", and
9+
"34".
10+
11+
Return the number of different integers after performing
12+
the replacement operations on word.
13+
14+
Two integers are considered different if their decimal
15+
representations without any leading zeros are different.
16+
17+
Example:
18+
Input: word = "a123bc34d8ef34"
19+
Output: 3
20+
Explanation: The three different integers are "123",
21+
"34", and "8". Notice that "34" is only
22+
counted once.
23+
24+
Example:
25+
Input: word = "leet1234code234"
26+
Output: 2
27+
28+
Example:
29+
Input: word = "a1b01c001"
30+
Output: 1
31+
Explanation: The three integers "1", "01", and "001"
32+
all represent the same integer because the
33+
leading zeros are ignored when comparing
34+
their decimal values.
35+
36+
Constraints:
37+
- 1 <= word.length <= 1000
38+
- word consists of digits and lowercase English
39+
letters.
40+
'''
41+
#Dfficulty: Easy
42+
#85 / 85 test cases passed.
43+
#Runtime: 24 ms
44+
#Memory Usage: 14.2 MB
45+
46+
#Runtime: 24 ms, faster than 97.61% of Python3 online submissions for Number of Different Integers in a String.
47+
#Memory Usage: 14.2 MB, less than 56.59% of Python3 online submissions for Number of Different Integers in a String.
48+
49+
class Solution:
50+
def numDifferentIntegers(self, word: str) -> int:
51+
number = ''
52+
numbers = set()
53+
word += 'a'
54+
for i in range(len(word)):
55+
if word[i].isdigit():
56+
number += word[i]
57+
else:
58+
if number:
59+
numbers.add(int(number))
60+
number = ''
61+
return len(numbers)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
You are given coordinates, a string that represents the
3+
coordinates of a square of the chessboard. Below is a
4+
chessboard for your reference.
5+
6+
Return true if the square is white, and false if the
7+
square is black.
8+
9+
The coordinate will always represent a valid chessboard
10+
square. The coordinate will always have the letter first,
11+
and the number second.
12+
13+
Example:
14+
Input: coordinates = "a1"
15+
Output: false
16+
Explanation: From the chessboard above, the square with
17+
coordinates "a1" is black, so return false.
18+
19+
Example:
20+
Input: coordinates = "h3"
21+
Output: true
22+
Explanation: From the chessboard above, the square with
23+
coordinates "h3" is white, so return true.
24+
25+
Example:
26+
Input: coordinates = "c7"
27+
Output: false
28+
29+
Constraints:
30+
- coordinates.length == 2
31+
- 'a' <= coordinates[0] <= 'h'
32+
- '1' <= coordinates[1] <= '8'
33+
'''
34+
#Dfficulty: Easy
35+
#203 / 203 test cases passed.
36+
#Runtime: 32 ms
37+
#Memory Usage: 14.1 MB
38+
39+
#Runtime: 32 ms, faster than 68.51% of Python3 online submissions for Determine Color of a Chessboard Square.
40+
#Memory Usage: 14.1 MB, less than 71.83% of Python3 online submissions for Determine Color of a Chessboard Square.
41+
42+
class Solution:
43+
def squareIsWhite(self, coordinates: str) -> bool:
44+
return not ord(coordinates[0]) % 2 == int(coordinates[1]) % 2

Easy/1816.TruncateSentence.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
A sentence is a list of words that are separated by a
3+
single space with no leading or trailing spaces. Each
4+
of the words consists of only uppercase and lowercase
5+
English letters (no punctuation).
6+
- For example, "Hello World", "HELLO", and "hello
7+
world hello world" are all sentences.
8+
9+
You are given a sentence s​​​​​​ and an integer k​​​​​​. You want
10+
to truncate s​​​​​​ such that it contains only the first k​​​​​​
11+
words. Return s​​​​​​ after truncating it.
12+
13+
Example:
14+
Input: s = "Hello how are you Contestant", k = 4
15+
Output: "Hello how are you"
16+
Explanation: The words in s are ["Hello", "how" "are",
17+
"you", "Contestant"].
18+
The first 4 words are ["Hello", "how", "are", "you"].
19+
Hence, you should return "Hello how are you".
20+
21+
Example:
22+
Input: s = "What is the solution to this problem", k = 4
23+
Output: "What is the solution"
24+
Explanation: The words in s are ["What", "is" "the",
25+
"solution", "to", "this", "problem"].
26+
The first 4 words are ["What", "is", "the",
27+
"solution"].
28+
Hence, you should return "What is the solution".
29+
30+
Example:
31+
Input: s = "chopper is not a tanuki", k = 5
32+
Output: "chopper is not a tanuki"
33+
34+
Constraints:
35+
- 1 <= s.length <= 500
36+
- k is in the range [1, the number of words in s].
37+
- s consist of only lowercase and uppercase English
38+
letters and spaces.
39+
- The words in s are separated by a single space.
40+
- There are no leading or trailing spaces.
41+
'''
42+
#Dfficulty: Easy
43+
#72 / 72 test cases passed.
44+
#Runtime: 32 ms
45+
#Memory Usage: 14.3 MB
46+
47+
#Runtime: 32 ms, faster than 69.33% of Python3 online submissions for Truncate Sentence.
48+
#Memory Usage: 14.3 MB, less than 55.62% of Python3 online submissions for Truncate Sentence.
49+
50+
class Solution:
51+
def truncateSentence(self, s: str, k: int) -> str:
52+
for i in range(len(s)):
53+
if s[i] == ' ':
54+
k -= 1
55+
if not k:
56+
return s[:i]
57+
return s
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'''
2+
In an alien language, surprisingly they also use english
3+
lowercase letters, but possibly in a different order.
4+
The order of the alphabet is some permutation of
5+
lowercase letters.
6+
7+
Given a sequence of words written in the alien language,
8+
and the order of the alphabet, return true if and only
9+
if the given words are sorted lexicographicaly in this
10+
alien language.
11+
12+
Example:
13+
Input: words = ["hello","leetcode"],
14+
order = "hlabcdefgijkmnopqrstuvwxyz"
15+
Output: true
16+
Explanation: As 'h' comes before 'l' in this language,
17+
then the sequence is sorted.
18+
19+
Example:
20+
Input: words = ["word","world","row"],
21+
order = "worldabcefghijkmnpqstuvxyz"
22+
Output: false
23+
Explanation: As 'd' comes after 'l' in this language,
24+
then words[0] > words[1], hence the sequence
25+
is unsorted.
26+
27+
Example:
28+
Input: words = ["apple","app"],
29+
order = "abcdefghijklmnopqrstuvwxyz"
30+
Output: false
31+
Explanation: The first three characters "app" match,
32+
and the second string is shorter (in size.)
33+
According to lexicographical rules
34+
"apple" > "app", because 'l' > '∅', where
35+
'∅' is defined as the blank character which
36+
is less than any other character (More info).
37+
38+
Constraints:
39+
- 1 <= words.length <= 100
40+
- 1 <= words[i].length <= 20
41+
- order.length == 26
42+
- All characters in words[i] and order are English
43+
lowercase letters.
44+
'''
45+
#Difficuty: Easy
46+
#119 / 119 test cases passed.
47+
#Runtime: 36 ms
48+
#Memory Usage: 14.1 MB
49+
50+
#Runtime: 36 ms, faster than 61.59% of Python3 online submissions for Verifying an Alien Dictionary.
51+
#Memory Usage: 14.1 MB, less than 98.44% of Python3 online submissions for Verifying an Alien Dictionary.
52+
53+
class Solution:
54+
def isAlienSorted(self, words: List[str], order: str) -> bool:
55+
alphabet = {}
56+
prev = words[0]
57+
58+
for i, c in enumerate(order):
59+
alphabet[c] = i
60+
61+
for word in words[1:]:
62+
length = min(len(prev), len(word))
63+
for i in range(length):
64+
if alphabet[prev[i]] == alphabet[word[i]]:
65+
if i == length-1 and length != len(prev):
66+
return False
67+
continue
68+
elif alphabet[prev[i]] < alphabet[word[i]]:
69+
prev = word
70+
break
71+
else:
72+
return False
73+
return True

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-532%2F1637-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-536%2F1651-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -22,7 +22,7 @@ In this repository provided my Python solutions of LeetCode problems.
2222
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
2323
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
2424
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
25-
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 6/30
25+
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 7/30
2626
<br><br>
2727
## Solutions
2828
*P.S. If you like this, please leave me a star.*
@@ -366,6 +366,7 @@ In this repository provided my Python solutions of LeetCode problems.
366366
|946.|[Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/)|[Python](/Medium/946.ValidateStackSequences.py)|Medium|`Stack`|
367367
|949.|[Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits/)|[Python](/Easy/949.LargestTimeforGivenDigits.py)|Easy|`Itertools`|
368368
|952.|[Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor/)|[Python](/Hard/952.LargestComponentSizebyCommonFactor.py)|Hard|Googled soln|
369+
|953.|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Python](/Easy/953.VerifyinganAlienDictionary.py)|Easy||
369370
|961.|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Python](/Easy/961.N-RepeatedElementinSize2NArray(BruteForce).py)|Easy|Brute Force|
370371
|961.|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Python](/Easy/961.N-RepeatedElementinSize2NArray.py)|Easy|`Dictionary`, a bit optimized|
371372
|965.|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Python](/Easy/965.UnivaluedBinaryTree.py)|Easy|`Binary Tree`, `Recursion`|
@@ -583,6 +584,9 @@ In this repository provided my Python solutions of LeetCode problems.
583584
|1732.|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/)|[Python](/Easy/1732.FindtheHighestAltitude.py)|Easy||
584585
|1748.|[Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/)|[Python](/Easy/1748.SumofUniqueElements.py)|Easy|`collections.Counter`|
585586
|1752.|[Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/)|[Python](/Easy/1752.CheckifArrayIsSortedandRotated.py)|Easy||
587+
|1805.|[Number of Different Integers in a String](https://leetcode.com/problems/number-of-different-integers-in-a-string/)|[Python](/Easy/1805.NumberofDifferentIntegersinaString.py)|Easy||
588+
|1812.|[Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square/)|[Python](/Easy/1812.DetermineColorofaChessboardSquare.py)|Easy||
589+
|1816.|[Truncate Sentence](https://leetcode.com/problems/truncate-sentence/)|[Python](/Easy/1816.TruncateSentence.py)|Easy||
586590

587591
<div align="right">
588592
<b><a href="#python-solutions-of-leetcode-problems">Back to Top</a></b>

0 commit comments

Comments
 (0)