Given a string S  and a character X, the task is to find the last index (0 based indexing) of X in string S. If no index found then the answer will be -1.
Example 1:
Input: S = "Geeks", P = 'e'
Output: 2
Explanation: Last index of 'e' 
is 2.
Example 2:
Input: S = "okiyh", P = 'z'
Output: -1
Explanation: There is no character
as 'z'.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function LastIndex() which takes the string s and character p as inputs and returns the answer.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5}
S and P contain only lower and upper case alphabets.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def LastIndex(self, s, p):

        # code here

Solutions:
class Solution:

	def LastIndex(self, s, p):
		r = 0
		for i in range(0, len(s)):
			if s[i] == p:
				r = i
		if r == 0:
			return -1
		else:
			return r


class Solution:

	def LastIndex(self, s, p):
		l = list(s)
		k = -1
		for i in range(len(l)):
			if s[i] == p:
				k = i
		return k


class Solution:

	def LastIndex(self, s, p):
		j = -1
		for i in range(len(s)):
			if s[i] == p:
				j = i
		return j


class Solution:

	def LastIndex(self, s, p):
		if p not in s:
			return -1
		for i in range(len(s)):
			if s[i] == p:
				c = i
		return c


class Solution:

	def LastIndex(self, s, p):
		a = s[::-1]
		try:
			i = a.index(p)
		except:
			return -1
		return len(s) - 1 - i


class Solution:

	def LastIndex(self, s, p):
		return s.rfind(p)


class Solution:

	def LastIndex(self, s, p):
		count = 0
		for i in s:
			if i == p:
				count += 1
				return s.rfind(i)
		return -1


class Solution:

	def LastIndex(self, s, p):
		flag = 0
		for i in range(-1, -(len(s) + 1), -1):
			if s[i] == p:
				return i + len(s)
		if flag == 0:
			return -1


class Solution:

	def LastIndex(self, s, p):
		if p not in s:
			return -1
		else:
			for i in range(len(s) - 1, -1, -1):
				if s[i] == p:
					break
			return i


class Solution:

	def LastIndex(self, s, p):
		l = []
		m = 0
		for i in range(len(s)):
			if s[i] == p:
				l.append(i)
			else:
				m += 1
		if m == len(s):
			return -1
		else:
			return max(l)


class Solution:

	def LastIndex(self, s, p):
		if p not in s:
			return -1
		return s.rindex(p)


class Solution:

	def LastIndex(self, s, p):
		if p in s:
			return s.rfind(p)
		else:
			return -1


class Solution:

	def LastIndex(self, s, p):
		o = 0
		s = s[::-1]
		for i in s:
			if i == p:
				o = len(s) - 1 - s.index(i)
		if o == 0:
			return -1
		else:
			return o


class Solution:

	def LastIndex(self, s, p):
		if p in s:
			s = s[::-1]
			k = len(s)
			f1 = s.find(p) + 1
			return k - f1
		else:
			return -1


class Solution:

	def LastIndex(self, s, p):
		s = s[::-1]
		n = len(s)
		return n - 1 - s.index(p) if p in s else -1


class Solution:

	def LastIndex(self, s, p):
		try:
			k = s.rindex(p)
			if k >= 0:
				return k
		except:
			return -1
		return -1


class Solution:

	def LastIndex(self, s, p):
		if s.count(p) >= 1:
			s = s[::-1]
			num = s.index(p)
			return len(s) - 1 - num
		else:
			return -1


class Solution:

	def LastIndex(self, s, p):
		l = []
		for i in range(len(s)):
			if s[i] == p:
				l.append(i)
		try:
			return l[len(l) - 1]
		except:
			return -1


class Solution:

	def LastIndex(self, s, p):
		if p not in s:
			return -1
		else:
			a = s[::-1].index(p)
			return len(s) - a - 1


class Solution:

	def LastIndex(self, s, p):
		s = s[::-1]
		if p in s:
			x = s.index(p)
			return len(s) - x - 1
		return -1


class Solution:

	def LastIndex(self, s, p):
		try:
			return s.rindex(p)
		except:
			return -1


class Solution:

	def LastIndex(self, s, p):
		S = s[::-1]
		n = len(S)
		t = 0
		if p not in s:
			return -1
		for i in range(len(S)):
			if S[i] == p:
				t = n - i - 1
				break
		return t


class Solution:

	def LastIndex(self, s, p):
		if p not in s:
			return -1
		else:
			y = s[::-1]
			z = len(y) - y.index(p) - 1
			return z


class Solution:

	def LastIndex(self, s, p):
		yup = s.rfind(p)
		if yup >= 0:
			return s.rindex(p)
		else:
			return -1


class Solution:

	def LastIndex(self, s, p):
		if p not in s:
			return -1
		else:
			result = []
			output = {}
			for (i, item) in enumerate(s):
				output[i] = item
			for char in range(len(s)):
				value = output.get(char)
				if value == p:
					result.append(char)
			return max(result)


class Solution:

	def LastIndex(self, s, p):
		ind = None
		for i in range(len(s)):
			if s[i] == p:
				ind = i
		if ind is None:
			return -1
		return ind


class Solution:

	def LastIndex(self, s, p):
		try:
			return s.replace(p, ' ', s.count(p) - 1).index(p)
		except:
			return -1


class Solution:

	def LastIndex(self, s, p):
		last_occurence = -1
		result = -1
		for i in range(len(s)):
			if s[i] == p:
				last_occurence = i
				result = max(last_occurence, result)
		return result


class Solution:

	def LastIndex(self, s, p):
		if p not in s:
			return -1
		val = -1
		for i in range(0, len(s)):
			if s[i] == p:
				val = i
		return val


class Solution:

	def LastIndex(self, s, p):
		a = s[::-1]
		f = False
		b = len(s)
		for i in a:
			if i == p:
				return b - (a.index(p) + 1)
				f = True
				break
		if f == False:
			return -1


class Solution:

	def LastIndex(self, s, p):
		i = -1
		c = 0
		m = -1
		for i in range(len(s)):
			if s[i] == p:
				c += 1
				if i >= m:
					m = i
		if c != 0:
			return m
		return -1


class Solution:

	def LastIndex(self, s, p):
		index = -1
		for a in range(len(s)):
			if s[a] == p:
				index = a
		return index


class Solution:

	def LastIndex(self, s, x):
		n = len(s)
		for i in range(n - 1, -1, -1):
			if s[i] == x:
				return i
		return -1


class Solution:

	def LastIndex(self, s, p):
		x = []
		if p not in s:
			return -1
		for i in range(len(s)):
			if p == s[i]:
				x.append(i)
		return x[-1]


class Solution:

	def LastIndex(self, s, p):
		result = 0
		a = False
		for i in range(len(s)):
			if s[i] == p:
				result = max(result, i)
				a = True
			else:
				continue
		if a == True:
			return result
		return -1