Given a string S consisting only '0's and '1's,  find the last index of the '1' present in it. 
 
Example 1:
Input:
S = 00001
Output:
4
Explanation:
Last index of  1 in given string is 4.
 
Example 2:
Input:
0
Output:
-1
Explanation:
Since, 1 is not present, so output is -1.
 
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 as inputs and returns the last index of '1'. If '1' is not present, return "-1" (without quotes).
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints: 
1 <= |S| <= 10^{6}
S = {0,1}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: #User function Template for python3



class Solution:

    def lastIndex(self, s):

        # code here

        

        



Solutions:
class Solution:

	def lastIndex(self, s):
		s = s[::-1]
		for i in range(len(s)):
			if s[i] == '1':
				return len(s) - i - 1
		return -1


class Solution:

	def lastIndex(self, s):
		return len(s) - 1 - s[::-1].index('1') if '1' in s else -1


class Solution:

	def lastIndex(self, s):
		if '1' not in s:
			return -1
		return s.rfind('1')


class Solution:

	def lastIndex(self, s):
		ret = -1
		for i in range(0, len(s)):
			if int(s[i]):
				ret = i
		return ret


class Solution:

	def lastIndex(self, s):
		ans = 0
		ans1 = False
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '1':
				ans += i
				ans1 = True
				break
		if ans1:
			return ans
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		return s.rfind('1')


class Solution:

	def lastIndex(self, s):
		a = []
		n = len(s)
		for i in range(n):
			if s[i] == '1':
				a.append(i)
		if len(a) == 0:
			return -1
		else:
			return a[len(a) - 1]


class Solution:

	def lastIndex(self, s):
		if '1' not in s:
			return -1
		else:
			for (i, num) in enumerate(reversed(s)):
				if num == '1':
					index = len(s) - 1 - i
					break
			return index


class Solution:

	def lastIndex(self, s):
		l = []
		for i in s:
			l.append(int(i))
		d = {}
		for (key, value) in enumerate(l):
			d[key] = value
		k = []
		for key in d.keys():
			if d[key] == 1:
				k.append(key)
		if len(k):
			return max(k)
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		p = '1'
		pos = -1
		for i in range(len(s) - 1, -1, -1):
			if p == s[i]:
				pos = i
				break
		return pos


class Solution:

	def lastIndex(self, s):
		b = []
		for i in range(0, len(s)):
			if s[i] == '1':
				b.append(i)
			else:
				continue
		if len(b) > 0:
			return b[-1]
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		n = len(s) - 1
		while n >= 0:
			if s[n] == '1':
				return n
			n -= 1
		return -1


class Solution:

	def lastIndex(self, s):
		if '1' not in s:
			return -1
		elif s == '1':
			return 0
		else:
			for i in range(len(s) - 1, -1, -1):
				if s[i] == '1':
					return i


class Solution:

	def lastIndex(self, s):
		if int(s) == 0:
			return -1
		ind = list(s)[::-1].index('1')
		return len(s) - 1 - ind


class Solution:

	def lastIndex(self, s):
		if '1' not in s:
			return -1
		return s.rindex('1')


class Solution:

	def lastIndex(self, s):
		if '1' in str(s):
			return s.rindex('1')
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		list1 = []
		l = list(s)
		for i in range(len(l)):
			if l[i] == '1':
				list1.append(i)
		if list1:
			return list1[-1]
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		last = -1
		index = 0
		result = -1
		for each_char in s:
			if each_char == '1':
				result = index
			index = index + 1
		return result


class Solution:

	def lastIndex(self, s):
		i = len(s) - 1
		while i >= 0:
			if s[i] == '1':
				return i
			i -= 1
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		for index in s:
			index = s.rfind('1')
			return index
		return -1


class Solution:

	def lastIndex(self, s):
		l1 = [-1]
		count = 0
		for i in s:
			if i == '1':
				l1.append(count)
				count += 1
			else:
				count += 1
		return max(l1)


class Solution:

	def lastIndex(self, s):
		count = 0
		if '1' in s:
			for i in reversed(s):
				count += 1
				if i == '1':
					return len(s) - count
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		l = []
		for i in s:
			l.append(int(i))
		n = len(l)
		mini = -1
		for i in range(n):
			if l[i] == 1:
				mini = i
		return mini


class Solution:

	def lastIndex(self, s):
		s = str(s)
		if '1' not in s:
			return -1
		s = s[::-1]
		n = len(s)
		return n - s.index('1') - 1


class Solution:

	def lastIndex(self, s):
		s1 = list(s)
		a = len(s1)
		while a >= 0:
			if s1[a - 1] == '1':
				return a - 1
			a = a - 1
		return -1


class Solution:

	def lastIndex(self, s):
		self.s = s
		l1 = list(s)
		l2 = []
		for x in range(len(l1) - 1, -1, -1):
			if int(l1[x]) == 1:
				a = x
				l2.append(a)
		if len(l2) != 0:
			answer = l2[0]
		else:
			answer = '-1'
		return answer


class Solution:

	def lastIndex(self, s):
		s = s[::-1]
		ind = s.find('1')
		if ind != -1:
			return len(s) - 1 - ind
		return -1


class Solution:

	def lastIndex(self, s):
		lst = [i for i in range(len(s)) if s[i] is '1']
		return max(lst) if len(lst) else -1


class Solution:

	def lastIndex(self, s):
		ans = []
		for i in range(len(s)):
			if s[i] == '1':
				ans.append(i)
		if '1' in s:
			return ans[-1]
		else:
			return -1


class Solution:

	def lastIndex(self, s):
		if s.count('1') == 0:
			return -1
		else:
			return len(s) - 1 - s[::-1].index('1')


class Solution:

	def lastIndex(self, s):
		if '1' not in s:
			return -1
		num = 0
		for i in range(len(s)):
			if s[i] == '1':
				num = i
		return num


class Solution:

	def lastIndex(self, s):
		ans = -1
		for x in range(len(s)):
			if int(s[x]) == 1:
				ans = x
		return ans


class Solution:

	def lastIndex(self, s):
		rs = None
		ct = 0
		for (index, chr) in enumerate(s):
			if chr == '1':
				rs = index
		return rs if rs is not None else -1


class Solution:

	def lastIndex(self, s):
		if '1' in s:
			l = s.rindex('1')
			return l
		return -1


class Solution:

	def lastIndex(self, s):
		l = []
		ansi = -1
		l[:0] = s
		if s == '1':
			return 0
		else:
			for i in range(len(l) - 1, -1, -1):
				if l[i] == '1':
					ansi = i
					break
		return ansi


class Solution:

	def lastIndex(self, s):
		cnt = -1
		a = 0
		for i in s:
			if i == '1':
				cnt = a
				a += 1
				continue
			a += 1
		return cnt


class Solution:

	def lastIndex(self, s):
		if s.count('1') == 1:
			return s.index('1')
		elif '1' not in s:
			return -1
		else:
			return s.rfind('1')