Skip to content

Commit 7591d40

Browse files
authored
August Monthly Leetcode Day-1 (codedecks-in#26)
1 parent 7da0070 commit 7591d40

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Python/detect-capital.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#Submission: 95.83%
2+
'''
3+
Step 1) We will generate a array and fill it with 0 or 1
4+
0 If the letter at corresponding index in word is lower.
5+
1 if the letter at corresponding index in word is upper.
6+
Example: USA => 111
7+
Is => 10
8+
country => 0000000
9+
step 2) Word will be valid if:
10+
a) When word starts with Capital and remaining all are small => Then the number for such word will be 10.....0
11+
To make 10...0 we will do: 1<<(size-1)
12+
b) When all are lower => Then the number will be 00...0
13+
To make 00...0 we will do: 0<<(size-1)
14+
c) When all are capital => Then the number will be 1....1
15+
To make 1...1 we will do: (1<<size)-1
16+
Step 3) If the number generated in step 1 matches any of the valid numbers, Then the word is valid.
17+
'''
18+
'''
19+
Time Complexity: O(n)
20+
Space Complexity: O(1)
21+
'''
22+
class Solution:
23+
def detectCapitalUse(self, word: str) -> bool:
24+
num = 0
25+
size = len(word)
26+
27+
#Generating the number
28+
for i in range(size):
29+
num = num*10
30+
if word[i].isupper():
31+
num+=1
32+
num = int(str(num),2)
33+
34+
#Checking if it matches any of the valid number
35+
if num==(1<<(size-1)) or num==(0<<(size-1)) or num==((1<<size)-1):
36+
return True
37+
return False

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
7070
| 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
7171
| 0137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | | _O(n)_ | _O(1)_ | Medium | | |
7272
| 0260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | |
73+
| 0520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
7374

7475
<br/>
7576
<div align="right">

0 commit comments

Comments
 (0)