{"id":19951224,"url":"https://github.com/emilwijayasekara/leetcode-13-roman-to-integer","last_synced_at":"2026-06-09T03:04:31.371Z","repository":{"id":212901864,"uuid":"732574353","full_name":"EmilWijayasekara/LeetCode-13-Roman-to-Integer","owner":"EmilWijayasekara","description":"LeetCode Problem 12. Integer to Roman - The task is to sum the values of these symbols according to the rules of Roman numeral representation, with the added complexity of subtraction when a smaller numeral precedes a larger one. The goal is to return the integer value equivalent to the given Roman numeral string.","archived":false,"fork":false,"pushed_at":"2023-12-17T07:33:26.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-01T14:43:33.371Z","etag":null,"topics":["java","leetcode","leetcode-java","leetcode-solutions"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EmilWijayasekara.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-12-17T05:59:19.000Z","updated_at":"2023-12-18T12:20:52.000Z","dependencies_parsed_at":"2023-12-17T07:19:35.324Z","dependency_job_id":"4dc3c2bf-bade-4fc1-91dd-a1e0226476b5","html_url":"https://github.com/EmilWijayasekara/LeetCode-13-Roman-to-Integer","commit_stats":null,"previous_names":["emilwijayasekara/leetcode-13-roman-to-integer"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/EmilWijayasekara/LeetCode-13-Roman-to-Integer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmilWijayasekara%2FLeetCode-13-Roman-to-Integer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmilWijayasekara%2FLeetCode-13-Roman-to-Integer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmilWijayasekara%2FLeetCode-13-Roman-to-Integer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmilWijayasekara%2FLeetCode-13-Roman-to-Integer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EmilWijayasekara","download_url":"https://codeload.github.com/EmilWijayasekara/LeetCode-13-Roman-to-Integer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmilWijayasekara%2FLeetCode-13-Roman-to-Integer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34089343,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","leetcode","leetcode-java","leetcode-solutions"],"created_at":"2024-11-13T01:07:13.980Z","updated_at":"2026-06-09T03:04:31.343Z","avatar_url":"https://github.com/EmilWijayasekara.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LeetCode Practice  (Day 3)\n## Achievements\n[![Untitled-1.jpg](https://i.postimg.cc/G3XyMFp9/Untitled-1.jpg)](https://postimg.cc/jCJ22JPr)\n\n## About the problem\n- *Problem Number* : 13\n- *Problem Name* :  [Roman to Integer](https://leetcode.com/problems/roman-to-integer/)\n- *Problem difficulty* : Easy (60.01%) 🟢\n- *Category* : Algorithms\n- *Programming language used* - Java\n\n## Problem\n\nRoman numerals are represented by seven different symbols: `I`,  `V`,  `X`,  `L`,  `C`,  `D`  and  `M`.\n\n```\nSymbol       Value\nI             1\nV             5\nX             10\nL             50\nC             100\nD             500\nM             1000\n```\n\nFor example, `2`  is written as  `II` in Roman numeral, just two ones added together.  `12`  is written as `XII`, which is simply  `X + II`. The number  `27`  is written as  `XXVII`, which is  `XX + V + II`.\n\nRoman numerals are usually written largest to smallest from left to right. However, the numeral for four is not  `IIII`. Instead, the number four is written as  `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as  `IX`. There are six instances where subtraction is used:\n\n-   `I`  can be placed before  `V`  (5) and  `X`  (10) to make 4 and 9.\n-   `X`  can be placed before  `L`  (50) and  `C`  (100) to make 40 and 90.\n-   `C`  can be placed before  `D`  (500) and  `M`  (1000) to make 400 and 900.\n\nGiven a roman numeral, convert it to an integer.\n\n**Example 1:**\n\n```\nInput: s = \"III\"\nOutput: 3\nExplanation: III = 3.\n```\n\n**Example 2:**\n\n```\nInput: s = \"LVIII\"\nOutput: 58\nExplanation: L = 50, V= 5, III = 3.\n```\n\n**Example 3:**\n\n```\nInput: s = \"MCMXCIV\"\nOutput: 1994\nExplanation: M = 1000, CM = 900, XC = 90 and IV = 4.\n```\n\n**Constraints:**\n\n-   `1 \u003c= s.length \u003c= 15`\n-   `s`  contains only the characters  `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.\n-   It is  **guaranteed** that  `s`  is a valid roman numeral in the range  `[1, 3999]`.\n\n\n## Approach Explanation\nI've approached this problem by iterating through the Roman numeral string from `right to left`. \nFor each numeral, I use a switch statement to map it to its corresponding numerical value, or this can be done using a HashMap also.\n\nI used three variables which are:\n```cpp\nint correspondingNaturalNumber = 0;  \nint answer = 0;  \nint previous =0;\n```\n`previous` variable used to get the previous number currently iterating through.\n`answer` is final return value that i added all roman number values.\n`correspondingNaturalNumber` in switch statement i mapped roman number to natural numbers.\n\nAfter that using if else statement, I compare the current numeral's value (`correspondingNaturalNumber`) with the value of the previous numeral (`previous`). If the current numeral is smaller than the previous one, its value is subtracted from the result; otherwise, it is added.\n\nAfter processing each numeral, I update the `previous` variable to store the current numeral's value. This ensures that the correct value is considered in the next iteration.\n\n### If you have suggestions for improvement or would like to contribute to this solution, feel free to create a pull request. 🙌😇\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femilwijayasekara%2Fleetcode-13-roman-to-integer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femilwijayasekara%2Fleetcode-13-roman-to-integer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femilwijayasekara%2Fleetcode-13-roman-to-integer/lists"}