LeetCode Challenge: Roman to Integer Conversion with HashMap

𝗗𝗮𝘆 𝟭𝟬/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved Roman to Integer using HashMap + Single Traversal. ➤ Approach (O(n), O(1) space): • Store Roman symbols and their values in a HashMap • Traverse the string from left to right • If the current value is less than the next value → subtract • Otherwise → add ➤ Key Insight: Roman numerals mostly add up left to right, but when a smaller value appears before a larger one, it indicates subtraction (like IV, IX, XL). By checking the next character during traversal, we can decide whether to add or subtract — all in one pass. #LeetCode #Java #DSA #HashMap #StringManipulation #ProblemSolving #20DaysChallenge #Consistency

  • text

To view or add a comment, sign in

Explore content categories