13. Roman to Integer

 

Converting Roman Numerals to Integers in JavaScript

Introduction

Roman numerals are a fascinating numeral system from ancient Rome, still in use today in various contexts. However, converting these numerals to our modern integer system can be a bit tricky. In this blog post, we'll explore how to efficiently convert Roman numerals to integers using JavaScript. We'll break down the approach, understand the logic, and analyze the complexity of our solution. If you want to see the complete solution and dive deeper, check out my LeetCode solution: Roman to Integer - LeetCode .

Problem Statement

Given a Roman numeral, we need to convert it to an integer. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.

Intuition

To solve the problem of converting Roman numerals to integers, we need to process each character from right to left, applying rules to decide whether to add or subtract the corresponding value based on the previous character. This ensures that special cases like "IV" (4) and "IX" (9) are handled correctly.

Approach

  1. Map Roman Numerals to Integers:

    • Create an object RomanToInteger that maps each Roman numeral character to its corresponding integer value.

  2. Initialize Result:

    • Initialize result to 0, which will store the final integer value.

  3. Convert String to Array and Reverse:

    • Convert the input string s to an array of characters using Array.from(s) and then reverse it with .reverse(). This helps in processing characters from the lowest value to the highest (right to left in Roman numeral rules).

  4. Process Each Character:

    • Iterate over each character using forEach.

    • If result is greater than 4 * RomanToInteger[e], subtract the value (for cases like IV, IX, etc.).

    • Otherwise, add the value to result.

  5. Return Result:

    • Finally, return the computed integer value.

LeetCode Solution

Here's the complete JavaScript solution for the Roman to Integer problem:

javascript
var romanToInt = function(s) {
    var RomanToInteger = {
        'I' : 1,
        'V' : 5,
        'X' : 10,
        'L' : 50,
        'C' : 100,
        'D' : 500,
        'M' : 1000
    }
    let result = 0;
    Array.from(s).reverse().forEach(e => {
        if(result > 4 * RomanToInteger[e]) {
            result -= RomanToInteger[e];
        } else {
            result += RomanToInteger[e];
        }
    });
    return result;
};

Complexity Analysis

Time Complexity: O(n)

  • The time complexity of the program is O(n), where n is the length of the input string s. This is because the function processes each character in the string exactly once using the forEach loop.

Space Complexity: O(1)

  • The space complexity of the program is O(1). Although we use an object RomanToInteger for mapping Roman numerals to their integer values, it contains a fixed number of entries (seven Roman numeral characters), which means the space used by this object is constant. No additional space that grows with the input size is used.

Conclusion

This solution efficiently handles Roman numeral conversions by leveraging a simple iteration and checking conditions based on Roman numeral rules. The approach ensures that the solution is both time-efficient and space-efficient. If you're interested in seeing the complete solution or comparing different approaches, be sure to check out my LeetCode solution: Roman to Integer - LeetCode .

Happy coding!

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

LeetCode Problem: Implement pow(x, n) leetcode:- 50