Skip to content

Commit 8ae3485

Browse files
authored
Added tasks 12-27
1 parent 84ffa7a commit 8ae3485

File tree

15 files changed

+479
-0
lines changed

15 files changed

+479
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# #Medium #String #Hash_Table #Math #Top_Interview_150_Array/String #Big_O_Time_O(1)_Space_O(1)
2+
# #2025_09_12_Time_0_ms_(100.00%)_Space_17.69_MB_(91.96%)
3+
4+
class Solution:
5+
def intToRoman(self, num: int) -> str:
6+
sb: list[str] = []
7+
8+
def numerals(remaining: int, one: int, c_ten: str, c_five: str, c_one: str) -> int:
9+
div = remaining // one
10+
if div == 9:
11+
sb.append(c_one)
12+
sb.append(c_ten)
13+
elif div == 8:
14+
sb.append(c_five)
15+
sb.append(c_one)
16+
sb.append(c_one)
17+
sb.append(c_one)
18+
elif div == 7:
19+
sb.append(c_five)
20+
sb.append(c_one)
21+
sb.append(c_one)
22+
elif div == 6:
23+
sb.append(c_five)
24+
sb.append(c_one)
25+
elif div == 5:
26+
sb.append(c_five)
27+
elif div == 4:
28+
sb.append(c_one)
29+
sb.append(c_five)
30+
elif div == 3:
31+
sb.append(c_one)
32+
sb.append(c_one)
33+
sb.append(c_one)
34+
elif div == 2:
35+
sb.append(c_one)
36+
sb.append(c_one)
37+
elif div == 1:
38+
sb.append(c_one)
39+
return remaining - (div * one)
40+
41+
num = numerals(num, 1000, ' ', ' ', 'M')
42+
num = numerals(num, 100, 'M', 'D', 'C')
43+
num = numerals(num, 10, 'C', 'L', 'X')
44+
numerals(num, 1, 'X', 'V', 'I')
45+
return "".join(sb)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from Solution0012 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_intToRoman(self):
6+
self.assertEqual(Solution().intToRoman(3), "III")
7+
8+
def test_intToRoman2(self):
9+
self.assertEqual(Solution().intToRoman(4), "IV")
10+
11+
def test_intToRoman3(self):
12+
self.assertEqual(Solution().intToRoman(9), "IX")
13+
14+
def test_intToRoman4(self):
15+
self.assertEqual(Solution().intToRoman(58), "LVIII")
16+
17+
def test_intToRoman5(self):
18+
self.assertEqual(Solution().intToRoman(1994), "MCMXCIV")
19+
20+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
12\. Integer to Roman
2+
3+
Medium
4+
5+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6+
7+
Symbol Value
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
16+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
17+
18+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
19+
20+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
21+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
22+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
23+
24+
Given an integer, convert it to a roman numeral.
25+
26+
**Example 1:**
27+
28+
**Input:** num = 3
29+
30+
**Output:** "III"
31+
32+
**Example 2:**
33+
34+
**Input:** num = 4
35+
36+
**Output:** "IV"
37+
38+
**Example 3:**
39+
40+
**Input:** num = 9
41+
42+
**Output:** "IX"
43+
44+
**Example 4:**
45+
46+
**Input:** num = 58
47+
48+
**Output:** "LVIII"
49+
50+
**Explanation:** L = 50, V = 5, III = 3.
51+
52+
**Example 5:**
53+
54+
**Input:** num = 1994
55+
56+
**Output:** "MCMXCIV"
57+
58+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
59+
60+
**Constraints:**
61+
62+
* `1 <= num <= 3999`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# #Easy #Top_Interview_Questions #String #Hash_Table #Math #Top_Interview_150_Array/String
2+
# #Big_O_Time_O(n)_Space_O(1) #2025_09_12_Time_1_ms_(86.91%)_Space_17.75_MB_(64.49%)
3+
4+
class Solution:
5+
def romanToInt(self, s: str) -> int:
6+
x = 0
7+
for i, ch in enumerate(s):
8+
if ch == 'I':
9+
x = self._accumulate(s, x, i, 1, 'V', 'X')
10+
elif ch == 'V':
11+
x += 5
12+
elif ch == 'X':
13+
x = self._accumulate(s, x, i, 10, 'L', 'C')
14+
elif ch == 'L':
15+
x += 50
16+
elif ch == 'C':
17+
x = self._accumulate(s, x, i, 100, 'D', 'M')
18+
elif ch == 'D':
19+
x += 500
20+
elif ch == 'M':
21+
x += 1000
22+
return x
23+
24+
def _accumulate(self, s: str, x: int, i: int, unit: int, five: str, ten: str) -> int:
25+
if i + 1 == len(s):
26+
x += unit
27+
elif s[i + 1] == five:
28+
x -= unit
29+
elif s[i + 1] == ten:
30+
x -= unit
31+
else:
32+
x += unit
33+
return x
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from Solution0013 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_romanToInt(self):
6+
self.assertEqual(Solution().romanToInt("III"), 3)
7+
8+
def test_romanToInt2(self):
9+
self.assertEqual(Solution().romanToInt("IV"), 4)
10+
11+
def test_romanToInt3(self):
12+
self.assertEqual(Solution().romanToInt("IX"), 9)
13+
14+
def test_romanToInt4(self):
15+
self.assertEqual(Solution().romanToInt("LVIII"), 58)
16+
17+
def test_romanToInt5(self):
18+
self.assertEqual(Solution().romanToInt("MCMXCIV"), 1994)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
13\. Roman to Integer
2+
3+
Easy
4+
5+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6+
7+
Symbol Value
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
16+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
17+
18+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
19+
20+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
21+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
22+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
23+
24+
Given a roman numeral, convert it to an integer.
25+
26+
**Example 1:**
27+
28+
**Input:** s = "III"
29+
30+
**Output:** 3
31+
32+
**Example 2:**
33+
34+
**Input:** s = "IV"
35+
36+
**Output:** 4
37+
38+
**Example 3:**
39+
40+
**Input:** s = "IX"
41+
42+
**Output:** 9
43+
44+
**Example 4:**
45+
46+
**Input:** s = "LVIII"
47+
48+
**Output:** 58
49+
50+
**Explanation:** L = 50, V= 5, III = 3.
51+
52+
**Example 5:**
53+
54+
**Input:** s = "MCMXCIV"
55+
56+
**Output:** 1994
57+
58+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
59+
60+
**Constraints:**
61+
62+
* `1 <= s.length <= 15`
63+
* `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
64+
* It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# #Easy #Top_Interview_Questions #String #Level_2_Day_2_String #Udemy_Strings
2+
# #Top_Interview_150_Array/String #Big_O_Time_O(n*m)_Space_O(m)
3+
# #2025_09_12_Time_0_ms_(100.00%)_Space_17.73_MB_(78.70%)
4+
5+
from typing import List
6+
7+
class Solution:
8+
def longestCommonPrefix(self, strs: List[str]) -> str:
9+
if len(strs) < 1:
10+
return ""
11+
if len(strs) == 1:
12+
return strs[0]
13+
temp = strs[0]
14+
i = 1
15+
while temp and i < len(strs):
16+
if len(temp) > len(strs[i]):
17+
temp = temp[: len(strs[i])]
18+
cur = strs[i][: len(temp)]
19+
if cur != temp:
20+
temp = temp[: len(temp) - 1]
21+
else:
22+
i += 1
23+
return temp
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest
2+
from Solution0014 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_longestCommonPrefix(self):
6+
self.assertEqual(Solution().longestCommonPrefix(["flower", "flow", "flight"]), "fl")
7+
8+
def test_longestCommonPrefix2(self):
9+
self.assertEqual(Solution().longestCommonPrefix(["dog", "racecar", "car"]), "")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
14\. Longest Common Prefix
2+
3+
Easy
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string `""`.
8+
9+
**Example 1:**
10+
11+
**Input:** strs = ["flower","flow","flight"]
12+
13+
**Output:** "fl"
14+
15+
**Example 2:**
16+
17+
**Input:** strs = ["dog","racecar","car"]
18+
19+
**Output:** ""
20+
21+
**Explanation:** There is no common prefix among the input strings.
22+
23+
**Constraints:**
24+
25+
* `1 <= strs.length <= 200`
26+
* `0 <= strs[i].length <= 200`
27+
* `strs[i]` consists of only lower-case English letters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# #Easy #Top_Interview_Questions #Array #Two_Pointers #Udemy_Two_Pointers
2+
# #Top_Interview_150_Array/String #2025_09_12_Time_0_ms_(100.00%)_Space_18.78_MB_(94.72%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def removeDuplicates(self, nums: List[int]) -> int:
8+
n = len(nums)
9+
i = 0
10+
j = 1
11+
if n <= 1:
12+
return n
13+
while j <= n - 1:
14+
if nums[i] != nums[j]:
15+
nums[i + 1] = nums[j]
16+
i += 1
17+
j += 1
18+
return i + 1

0 commit comments

Comments
 (0)