β 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)