Roman to Integer In C,CPP,JAVA,PYTHON,C#,JS
Problem Statement: Roman to Integer The task is to convert a given Roman numeral to its corresponding integer value. Roman numerals are written by combining symbols, and a smaller numeral placed before a larger numeral means subtraction. For example: You are to implement a function that converts a given Roman numeral string into an integer. Roman Numeral Symbols: Example: Input: Output: Input: Output: Input: Output: Input: Output: Input: Output: Approach: Time Complexity: Code Implementation C Code: include <stdio.h>#include <string.h>int romanToInt(char *s) { // Roman numeral to integer mappings int roman[] = {1000, 500, 100, 50, 10, 5, 1}; char *romans[] = {“M”, “D”, “C”, “L”, “X”, “V”, “I”}; int result = 0; int len = strlen(s); for (int i = 0; i < len; i++) { int value = 0; // Find the value of current character for (int j = 0; j < 7; j++) { if (s[i] == romans[j][0]) { value = roman[j]; break; } } // If next character represents a larger value, subtract the current value if (i < len – 1) { int nextValue = 0; for (int j = 0; j < 7; j++) { if (s[i + 1] == romans[j][0]) { nextValue = roman[j]; break; } } if (value < nextValue) { result -= value; } else { result += value; } } else { result += value; } } return result;}int main() { char s[] = “MCMXCIV”; printf(“Integer value of %s is: %d\n”, s, romanToInt(s)); // Output: 1994 return 0;} C++ Code: #include <iostream>#include <unordered_map>#include <string>using namespace std;int romanToInt(string s) { unordered_map<char, int> romanMap = { {‘I’, 1}, {‘V’, 5}, {‘X’, 10}, {‘L’, 50}, {‘C’, 100}, {‘D’, 500}, {‘M’, 1000} }; int result = 0; int len = s.length(); for (int i = 0; i < len; i++) { int value = romanMap[s[i]]; // If the next character represents a larger value, subtract the current value if (i < len – 1 && value < romanMap[s[i + 1]]) { result -= value; } else { result += value; } } return result;}int main() { string s = “MCMXCIV”; cout << “Integer value of ” << s << ” is: ” << romanToInt(s) << endl; // Output: 1994 return 0;} Java Code: import java.util.HashMap;public class Solution { public int romanToInt(String s) { // Roman numeral to integer mapping HashMap<Character, Integer> romanMap = new HashMap<>(); romanMap.put(‘I’, 1); romanMap.put(‘V’, 5); romanMap.put(‘X’, 10); romanMap.put(‘L’, 50); romanMap.put(‘C’, 100); romanMap.put(‘D’, 500); romanMap.put(‘M’, 1000); int result = 0; int len = s.length(); for (int i = 0; i < len; i++) { int value = romanMap.get(s.charAt(i)); // If next character represents a larger value, subtract the current value if (i < len – 1 && value < romanMap.get(s.charAt(i + 1))) { result -= value; } else { result += value; } } return result; } public static void main(String[] args) { Solution solution = new Solution(); String s = “MCMXCIV”; System.out.println(“Integer value of ” + s + ” is: ” + solution.romanToInt(s)); // Output: 1994 }} Python Code: def romanToInt(s: str) -> int: roman_map = {‘I’: 1, ‘V’: 5, ‘X’: 10, ‘L’: 50, ‘C’: 100, ‘D’: 500, ‘M’: 1000} result = 0 length = len(s) for i in range(length): value = roman_map[s[i]] # If next character represents a larger value, subtract the current value if i < length – 1 and value < roman_map[s[i + 1]]: result -= value else: result += value return result# Example Usages = “MCMXCIV”print(f”Integer value of {s} is: {romanToInt(s)}”) # Output: 1994 C# Code: using System;using System.Collections.Generic;public class Solution { public int RomanToInt(string s) { // Roman numeral to integer mapping var romanMap = new Dictionary<char, int> { {‘I’, 1}, {‘V’, 5}, {‘X’, 10}, {‘L’, 50}, {‘C’, 100}, {‘D’, 500}, {‘M’, 1000} }; int result = 0; int len = s.Length; for (int i = 0; i < len; i++) { int value = romanMap[s[i]]; // If next character represents a larger value, subtract the current value if (i < len – 1 && value < romanMap[s[i + 1]]) { result -= value; } else { result += value; } } return result; } public static void Main() { Solution solution = new Solution(); string s = “MCMXCIV”; Console.WriteLine($”Integer value of {s} is: {solution.RomanToInt(s)}”); // Output: 1994 }} JavaScript Code: var romanToInt = function(s) { const romanMap = { ‘I’: 1, ‘V’: 5, ‘X’: 10, ‘L’: 50, ‘C’: 100, ‘D’: 500, ‘M’: 1000 }; let result = 0; let length = s.length; for (let i = 0; i < length; i++) { let value = romanMap[s[i]]; // If next character represents a larger value, subtract the current value if (i < length – 1 && value < romanMap[s[i + 1]]) { result -= value; } else { result += value; } } return result;};console.log(romanToInt(“MCMXCIV”)); // Output: 1994 Summary:
Roman to Integer In C,CPP,JAVA,PYTHON,C#,JS Read More »