Given a string in roman no format (s)  your task is to convert it to an integer . Various symbols and their values are given below.
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Example 1:
Input:
s = V
Output: 5
Example 2:
Input:
s = III 
Output: 3
Your Task:
Complete the function romanToDecimal() which takes a string as input parameter and returns the equivalent decimal number. 
Expected Time Complexity: O(|S|), |S| = length of string S.
Expected Auxiliary Space: O(1)
Constraints:
1<=roman no range<=3999

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def romanToDecimal(self, S): 

        # code here

Solutions:
class Solution:

	def romanToDecimal(self, S):
		k = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		ans = 0
		for i in range(len(S) - 1):
			if k[S[i]] < k[S[i + 1]]:
				ans -= k[S[i]]
			else:
				ans += k[S[i]]
		ans += k[S[-1]]
		return ans


class Solution:

	def romanToDecimal(self, s):
		ans = []
		for i in range(len(s)):
			if s[i] == 'I':
				ans.append(1)
			if s[i] == 'V':
				ans.append(5)
			if s[i] == 'X':
				ans.append(10)
			if s[i] == 'L':
				ans.append(50)
			if s[i] == 'C':
				ans.append(100)
			if s[i] == 'D':
				ans.append(500)
			if s[i] == 'M':
				ans.append(1000)
		sum = 0
		for i in range(len(ans)):
			sum = sum + ans[i]
		if 'IV' in s:
			sum -= 2
		if 'IX' in s:
			sum -= 2
		if 'XL' in s:
			sum -= 20
		if 'XC' in s:
			sum -= 20
		if 'CD' in s:
			sum -= 200
		if 'CM' in s:
			sum -= 200
		return sum


class Solution:

	def romanToDecimal(self, S):
		dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		num = dic[S[-1]]
		prev = dic[S[-1]]
		for ind in range(len(S) - 2, -1, -1):
			if dic[S[ind]] < prev:
				num -= dic[S[ind]]
			else:
				num += dic[S[ind]]
			prev = dic[S[ind]]
		return num


class Solution:

	def romanToDecimal(self, S):
		mapper = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		i = 0
		res = 0
		while i < len(S):
			if i + 1 < len(S):
				value1 = mapper[S[i]]
				val2 = mapper[S[i + 1]]
				if value1 < val2:
					res = res + val2 - value1
					i = i + 2
				else:
					res = res + value1
					i = i + 1
			else:
				res = res + mapper[S[i]]
				i = i + 1
		return res


class Solution:

	def romanToDecimal(self, S):
		roman_to_decimal = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		S = S.replace('IV', 'IIII').replace('IX', 'VIIII').replace('XL', 'XXXX').replace('XC', 'LXXXX').replace('CD', 'CCCC').replace('CM', 'DCCCC')
		return sum(map(lambda x: roman_to_decimal[x], S))


class Solution:

	def romanToDecimal(self, s):
		dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		sum = 0
		for i in range(len(s) - 1):
			if dic[s[i]] < dic[s[i + 1]]:
				sum -= dic[s[i]]
			else:
				sum += dic[s[i]]
		return sum + dic[s[-1]]


class Solution:

	def romanToDecimal(self, S):
		num = 0
		roman = {'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, 'L': 50, 'XL': 40, 'XC': 90, 'C': 100, 'D': 500, 'CD': 400, 'CM': 900, 'M': 1000}
		i = 0
		while i < len(S):
			if not i + 1 >= len(S):
				if S[i] + S[i + 1] in roman:
					num += roman[S[i] + S[i + 1]]
					i += 1
				else:
					num += roman[S[i]]
			else:
				num += roman[S[i]]
			i += 1
		return num


class Solution:

	def romanToDecimal(self, s):
		dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		ans = 0
		for i in range(len(s)):
			el = s[i]
			ans += dic[el]
			if i - 1 >= 0 and dic[el] > dic[s[i - 1]]:
				ans -= 2 * dic[s[i - 1]]
		return ans


class Solution:

	def romanToDecimal(self, S):
		d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		c = 0
		S = S[::-1]
		for (i, j) in enumerate(S):
			if i != 0:
				if d[j] >= d[S[i - 1]]:
					c = c + d[j]
				else:
					c = c - d[j]
			else:
				c = c + d[j]
		return c


class Solution:

	def romanToDecimal(self, S):
		d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		c = 0
		S = S.replace('IV', 'IIII').replace('IX', 'VIIII')
		S = S.replace('XL', 'XXXX').replace('XC', 'LXXXX')
		S = S.replace('CD', 'CCCC').replace('CM', 'DCCCC')
		for i in S:
			c = c + d[i]
		return c


class Solution:

	def romanToDecimal(self, S):
		dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		val = dict[S[0]]
		j = 0
		for i in range(1, len(S)):
			if dict[S[i]] > dict[S[j]]:
				val += int(dict[S[i]]) - 2 * int(dict[S[j]])
			else:
				val += dict[S[i]]
			j += 1
		return val


class Solution:

	def romanToDecimal(self, S):

		def convert(x):
			if x == 'I':
				return 1
			elif x == 'V':
				return 5
			elif x == 'X':
				return 10
			elif x == 'L':
				return 50
			elif x == 'C':
				return 100
			elif x == 'D':
				return 500
			elif x == 'M':
				return 1000
		s = list(S)
		s = list(map(convert, s))
		sum = 0
		if len(s) == 1:
			return s[0]
		for i in range(len(s) - 1):
			if s[i] < s[i + 1]:
				sum = sum - s[i]
			elif s[i] >= s[i + 1]:
				sum = sum + s[i]
		sum = sum + s[-1]
		return sum


class Solution:

	def romanToDecimal(self, s):
		ans = 0
		flag = 0
		g = {}
		g['I'] = 1
		g['V'] = 5
		g['X'] = 10
		g['L'] = 50
		g['C'] = 100
		g['D'] = 500
		g['M'] = 1000
		for i in range(len(s) - 1, -1, -1):
			x = s[i]
			if g[x] >= flag:
				ans = ans + g[x]
				flag = g[x]
			else:
				ans = ans - g[x]
		return ans


class Solution:

	def romanToDecimal(self, S):
		roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		n = len(S)
		res = 0
		for i in range(n):
			if i < n - 1 and roman[S[i]] < roman[S[i + 1]]:
				res -= roman[S[i]]
			else:
				res += roman[S[i]]
		return res


class Solution:

	def romanToDecimal(self, S):
		d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		ans = 0
		prev = 0
		for i in reversed(S):
			value = d[i]
			if value >= prev:
				ans += value
			else:
				ans -= value
			prev = value
		return ans


class Solution:

	def romanToDecimal(self, S):
		d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		s = d[S[-1]]
		for i in range(len(S) - 2, -1, -1):
			if d[S[i]] < d[S[i + 1]]:
				s -= d[S[i]]
			else:
				s += d[S[i]]
		return s


class Solution:

	def romanToDecimal(self, S):
		l = list(S)
		l.reverse()
		c = 0
		dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		for i in range(len(l)):
			if i == 0:
				c += dic[l[0]]
				continue
			j = i - 1
			if dic[l[i]] < dic[l[j]]:
				c -= dic[l[i]]
			else:
				c += dic[l[i]]
		return c


class Solution:

	def romanToDecimal(self, S):
		roman_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		int_val = 0
		for i in range(len(S)):
			if i > 0 and roman_val[S[i]] > roman_val[S[i - 1]]:
				int_val += roman_val[S[i]] - 2 * roman_val[S[i - 1]]
			else:
				int_val += roman_val[S[i]]
		return int_val


class Solution:

	def romanToDecimal(self, S):
		d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		ans = 0
		i = 0
		while i < len(S):
			if i + 1 < len(S):
				if d[S[i]] >= d[S[i + 1]]:
					ans += d[S[i]]
					i += 1
				else:
					ans -= d[S[i]]
					ans += d[S[i + 1]]
					i += 2
			else:
				ans += d[S[i]]
				i += 1
		return ans


class Solution:

	def romanToDecimal(self, S):
		dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		Sum = 0
		for i in range(len(S) - 1):
			if dict[S[i]] < dict[S[i + 1]]:
				Sum = Sum - dict[S[i]]
			if dict[S[i]] >= dict[S[i + 1]]:
				Sum = Sum + dict[S[i]]
		return Sum + dict[S[len(S) - 1]]


class Solution:

	def romanToDecimal(self, s):
		roman_values = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		result = 0
		for i in range(len(s)):
			if i > 0 and roman_values[s[i]] > roman_values[s[i - 1]]:
				result += roman_values[s[i]] - 2 * roman_values[s[i - 1]]
			else:
				result += roman_values[s[i]]
		return result


class Solution:

	def romanToDecimal(self, S):
		roman1 = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		num = 0
		for i in range(len(S)):
			a = S[i]
			b = S[i - 1]
			if i > 0 and roman1[a] > roman1[b]:
				num += roman1[a] - 2 * roman1[b]
			else:
				num += roman1[a]
		return num


class Solution:

	def romanToDecimal(self, S):
		roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		decimal_num = 0
		prev_value = 0
		for i in range(len(S) - 1, -1, -1):
			curr_value = roman_map[S[i]]
			if curr_value < prev_value:
				decimal_num -= curr_value
			else:
				decimal_num += curr_value
			prev_value = curr_value
		return decimal_num


class Solution:

	def romanToDecimal(self, S):
		dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		i = 0
		sm = 0
		while i < len(S):
			if i + 1 < len(S) and dic[S[i]] < dic[S[i + 1]]:
				sm += dic[S[i + 1]] - dic[S[i]]
				i += 2
			else:
				sm += dic[S[i]]
				i += 1
		return sm


class Solution:

	def romanToDecimal(self, S):
		s = S
		D = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000, 'CM': 900, 'IV': 4, 'XC': 90}
		length = len(s)
		output = D.get(s[0])
		for i in range(1, length):
			current = D.get(s[i])
			prev = D.get(s[i - 1])
			if current > prev:
				output += current - 2 * prev
			else:
				output += current
		return output


class Solution:

	def romanToDecimal(self, S):
		d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		prev = -1
		res = 0
		for i in range(len(S) - 1, -1, -1):
			cur = d[S[i]]
			res += cur if cur >= prev else -cur
			prev = cur
		return res


class Solution:

	def romanToDecimal(self, s):
		d = {}
		d['I'] = 1
		d['V'] = 5
		d['X'] = 10
		d['L'] = 50
		d['C'] = 100
		d['D'] = 500
		d['M'] = 1000
		le = len(s)
		j = 0
		sum = 0
		while j < le - 1:
			if d[s[j]] >= d[s[j + 1]]:
				sum += d[s[j]]
				j += 1
			else:
				sum += d[s[j + 1]] - d[s[j]]
				j += 2
		if j < le:
			sum += d[s[j]]
		return sum


class Solution:

	def printValue(self, st):
		dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		return dic[st]

	def romanToDecimal(self, S):
		i = 0
		res = 0
		while i < len(S):
			s1 = self.printValue(S[i])
			if i + 1 < len(S):
				s2 = self.printValue(S[i + 1])
				if s1 >= s2:
					res += s1
					i += 1
				else:
					res += s2 - s1
					i += 2
			else:
				res += s1
				i += 1
		return res


class Solution:

	def romanToDecimal(self, S):
		c = 0
		d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		s = 0
		i = 0
		n = len(S)
		while i < n:
			if i != n - 1 and d[S[i + 1]] > d[S[i]]:
				s += d[S[i + 1]] - d[S[i]]
				i += 1
			else:
				s += d[S[i]]
			i += 1
		return s


class Solution:

	def romanToDecimal(self, S):
		ans = 0
		if len(S) == 1:
			return Solution.RtoD(S[0])
		i = 0
		while i < len(S):
			s1 = Solution.RtoD(S[i])
			if i + 1 < len(S):
				s2 = Solution.RtoD(S[i + 1])
				if s1 >= s2:
					ans += s1
					i += 1
				else:
					ans += s2 - s1
					i += 2
			else:
				ans += s1
				i += 1
		return ans

	def RtoD(r):
		if r == 'I':
			return 1
		elif r == 'V':
			return 5
		elif r == 'X':
			return 10
		elif r == 'L':
			return 50
		elif r == 'C':
			return 100
		elif r == 'D':
			return 500
		elif r == 'M':
			return 1000
		return -1


class Solution:

	def romanToDecimal(self, S):
		roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		result = 0
		prev_value = 0
		for c in S:
			curr_value = roman_dict[c]
			if curr_value > prev_value:
				result -= 2 * prev_value
			result += curr_value
			prev_value = curr_value
		return result


def value(k):
	if k == 'I':
		return 1
	elif k == 'V':
		return 5
	elif k == 'X':
		return 10
	elif k == 'L':
		return 50
	elif k == 'C':
		return 100
	elif k == 'D':
		return 500
	elif k == 'M':
		return 1000

class Solution:

	def romanToDecimal(self, s):
		i = 0
		j = 0
		while i < len(s):
			p = value(s[i])
			if i + 1 < len(s):
				q = value(s[i + 1])
				if p >= q:
					j += p
					i += 1
				else:
					j = j + (q - p)
					i += 2
			else:
				j += p
				i += 1
		return j


def value(k):
	if k == 'I':
		return 1
	elif k == 'V':
		return 5
	elif k == 'X':
		return 10
	elif k == 'L':
		return 50
	elif k == 'C':
		return 100
	elif k == 'D':
		return 500
	elif k == 'M':
		return 1000

class Solution:

	def romanToDecimal(self, S):
		res = 0
		i = 0
		while i < len(S):
			s1 = value(S[i])
			if i + 1 < len(S):
				s2 = value(S[i + 1])
				if s1 >= s2:
					res = res + s1
					i = i + 1
				else:
					res = res + s2 - s1
					i = i + 2
			else:
				res = res + s1
				i = i + 1
		return res


class Solution:

	def romanToDecimal(self, S):
		k = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		prev = S[0]
		sums = 0
		for i in S:
			if k[prev] < k[i]:
				sums -= k[prev]
				sums += k[i] - k[prev]
			else:
				sums += k[i]
			prev = i
		return sums


class Solution:

	def romanToDecimal(self, s):
		num = 0
		dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		dou = {'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900}
		i = 0
		while i < len(s):
			if i < len(s) - 1 and s[i:i + 2] in dou:
				num += dou[s[i:i + 2]]
				i += 2
			else:
				num += dict[s[i]]
				i += 1
		return num


RomanDict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}

class Solution:

	def check(self, a, b):
		global RomanDict
		if RomanDict[a] < RomanDict[b]:
			return '<'
		elif RomanDict[a] > RomanDict[b]:
			return '>'
		else:
			return '='

	def romanToDecimal(self, s):
		global RomanDict
		arr = []
		start = 0
		end = 0
		for i in range(len(s)):
			if i == len(s) - 1:
				arr.append(s[start:])
				continue
			sign = self.check(s[i], s[i + 1])
			if sign == '<':
				start = min(start, i)
			else:
				end = i + 1
				arr.append(s[start:end])
				start = i + 1
		num = 0
		for elem in arr:
			if len(elem) == 1:
				num = num + RomanDict[elem]
			elif len(elem) > 1:
				num = num + RomanDict[elem[-1]]
				for i in range(len(elem) - 2, -1, -1):
					num = num - RomanDict[elem[i]]
		return num


def roman(ch):
	if ch == 'I':
		return 1
	elif ch == 'V':
		return 5
	elif ch == 'X':
		return 10
	elif ch == 'L':
		return 50
	elif ch == 'C':
		return 100
	elif ch == 'D':
		return 500
	elif ch == 'M':
		return 1000

class Solution:

	def romanToDecimal(self, s):
		i = 0
		res = 0
		while i < len(s):
			num1 = roman(s[i])
			if i + 1 < len(s):
				num2 = roman(s[i + 1])
				if num1 >= num2:
					res += num1
					i += 1
				else:
					res += num2 - num1
					i += 2
			else:
				res += num1
				i += 1
		return res


class Solution:

	def romanToDecimal(self, S):
		dec = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		res = 0
		prev = 0
		for c in S:
			value = dec[c]
			if value > prev:
				res -= prev
			else:
				res += prev
			prev = value
		res += prev
		return res


class Solution:

	def romanToDecimal(self, S):
		a = list(S)
		romandict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		sum = 0
		i = 0
		while i < len(a) - 1:
			if romandict[a[i]] < romandict[a[i + 1]]:
				sum += romandict[a[i + 1]] - romandict[a[i]]
				i += 1
			else:
				sum += romandict[a[i]]
			i += 1
		if i != len(a):
			sum += romandict[a[-1]]
		return sum


class Solution:

	def romanToDecimal(self, s):
		d = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1, 'O': 0}
		ans = 0
		s = list(s)
		s.append('O')
		for i in range(len(s) - 1):
			ans += d[s[i]]
			if d[s[i + 1]] > d[s[i]]:
				ans -= 2 * d[s[i]]
		return ans


class Solution:

	def romanToDecimal(self, S):
		i = 0
		Sum = 0
		l = len(S)
		dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
		while i < l:
			if i == l - 1:
				Sum += dic[S[i]]
				i += 1
			elif dic[S[i]] >= dic[S[i + 1]]:
				Sum += dic[S[i]]
				i += 1
			else:
				diff = dic[S[i + 1]] - dic[S[i]]
				Sum += diff
				i += 2
		return Sum