DEV Community

Cover image for LeetCode Challenge: 13. Roman to Integer - JavaScript Solution πŸš€
Rahul Kumar Barnwal
Rahul Kumar Barnwal

Posted on β€’ Edited on

1 1 1 1 1

LeetCode Challenge: 13. Roman to Integer - JavaScript Solution πŸš€

Top Interview 150

Converting Roman numerals to integers involves understanding both their additive and subtractive notation rules. Let’s break down LeetCode 13: Roman to Integer and solve it step by step.


πŸš€ Problem Description

Roman numerals are represented by seven symbols, each with a fixed value.

  • Subtractive Rule: When a smaller numeral appears before a larger one, subtract the smaller numeral.

Given a Roman numeral string, return its integer equivalent.


πŸ’‘ Examples

Example 1

Input: s = "III"  
Output: 3  
Explanation: III = 3 (1 + 1 + 1).
Enter fullscreen mode Exit fullscreen mode

Example 2

Input: s = "LVIII"  
Output: 58  
Explanation: L = 50, V = 5, III = 3.
Enter fullscreen mode Exit fullscreen mode

Example 3

Input: s = "MCMXCIV"  
Output: 1994  
Explanation: M = 1000, CM = 900, XC = 90, IV = 4.  
             1994 = 1000 + 900 + 90 + 4.
Enter fullscreen mode Exit fullscreen mode

πŸ† JavaScript Solution

To solve this problem, we use a mapping dictionary for Roman symbols and traverse the string to calculate the integer.

Implementation

var romanToInt = function(s) {
    const romanMap = {
        '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 current = romanMap[s[i]];
        const next = romanMap[s[i + 1]];

        if (next && current < next) {
            total -= current;
        } else {
            total += current;
        }
    }

    return total;
};
Enter fullscreen mode Exit fullscreen mode

πŸ” How It Works

  1. Map Roman Symbols: Use a dictionary to map Roman symbols to their values.
  2. Traverse the String: For each character:
    • Add the value to the total if the current numeral is greater than or equal to the next.
    • Subtract the value if the current numeral is smaller than the next (subtractive rule).
  3. Return the Total: After traversing the string, total holds the integer equivalent.

πŸ”‘ Complexity Analysis

  • > Time Complexity: O(n), where n is the length of the string. We traverse the string once.
  • > Space Complexity: O(1), as the mapping dictionary size is constant.

πŸ“‹ Dry Run
Input: s = "MCMXCIV"

Roman to Integer
Output: 1994


✨ Pro Tips for Interviews

  1. Clarify constraints: Confirm that the Roman numeral input will always be valid (as per problem assumptions).
  2. Highlight efficiency: Emphasize O(n) time complexity and how the subtractive rule is handled efficiently.
  3. Edge cases:
    • Smallest numeral ("I" β†’ 1).
    • Mixed additive and subtractive cases ("XIV" β†’ 14).

πŸ“š Learn More

Check out the full explanation and code walkthrough on my Dev.to post:
πŸ‘‰ Trapping Rain Water - JavaScript Solution

What’s your approach to solving this problem? Let’s discuss! πŸš€

JavaScript #LeetCode #CodingInterview #ProblemSolving

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
rahulgithubweb profile image
Rahul Kumar Barnwal β€’

Follow Me on GitHub πŸš€

If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.

Don't forget to follow for more updates!

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more