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 blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read 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!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay