โ Problem 1: Roman Numerals to Integer
๐ Difficulty: Easy
๐ Source: LeetCode #13
๐ Topic Tags: String, Hash Map, Math
๐ง Problem Description
Roman numerals are represented by seven symbols:
Symbol | Value |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Write a function to convert a Roman numeral into an integer.
๐ก Rules:
- Roman numerals are usually written from largest to smallest.
- However, if a smaller number appears before a larger one, it must be subtracted.
- Example:
IV
= 4 (notIIII
),IX
= 9,XL
= 40
๐งพ Use this chart if you're unsure about how Roman numerals work:
๐ Roman Numerals 1โ100 Chart
๐งฉ Examples
Input: "III" โ Output: 3
Input: "IV" โ Output: 4
Input: "IX" โ Output: 9
Input: "LVIII" โ Output: 58 (50 + 5 + 3)
Input: "MCMXCIV" โ Output: 1994
Approach (Step-by-Step)
-
Create a map (object)
- Store Roman letters and their integer values.
-
Loop through each character
- Iterate over the input string one character at a time.
-
Compare the current character with the next one
- Check if the next character's value is greater than the current character's value.
-
Subtraction case
- If the next character is greater, subtract the current value from the total.
-
Otherwise, addition case
- If the next character is not greater, add the current value to the total.
-
Return the total
- After processing all characters, return the total integer value.
โ JavaScript Solution
function romanToInt(s) {
const roman = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
let total = 0;
for (let i = 0; i < s.length; i++) {
const currentVal = roman[s[i]];
const nextVal = roman[s[i + 1]];
if (nextVal > currentVal) {
total -= currentVal;
} else {
total += currentVal;
}
}
return total;
}
๐งช Test Cases
console.log(romanToInt("III")); // 3
console.log(romanToInt("IV")); // 4
console.log(romanToInt("IX")); // 9
console.log(romanToInt("LVIII")); // 58
console.log(romanToInt("MCMXCIV")); // 1994
โ Problem 2: Longest Common Prefix
๐ Difficulty: Easy
๐ Source: LeetCode #14
๐ Topic Tags: String, Array, Math ,loop
๐ง Problem Description
Write a function to find the longest common prefix string among an array of strings.
If there is no common prefix, return an empty string ""
.
๐ก Constraints
1 <= strs.length <= 200
0 <= strs[i].length <= 200
- Each
strs[i]
consists of only lowercase English letters (if it is non-empty).
โ๏ธ Approach (Step-by-Step)
Edge case check:
If the input array is empty, return an empty string.Assume the first string in the array is the initial common prefix.
-
Iterate through the rest of the strings in the array:
- While the current string does not start with the prefix, shrink the prefix from the end (remove one character at a time).
- If the prefix becomes an empty string during this process, return
""
.
Return the resulting prefix after checking all strings.
๐ฅ Input
- An array of strings:
strs[]
๐ค Output
- A single string representing the longest common prefix.
๐งช Example
Input: strs = ["flower", "flow", "flight"]
Output: "fl"
Input: strs = ["dog", "racecar", "car"]
Output: ""
๐ป JavaScript Solution:
var longestCommonPrefix = function(strs) {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
};
console.log(longestCommonPrefix(["flower", "flow", "flight"])); // Output: "fl"
console.log(longestCommonPrefix(["dog", "racecar", "car"])); // Output: ""
Top comments (0)