Last index of One | Practice | GeeksforGeeks

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| <= 106
S = {0,1}
 
 

 
 
0
akashmarkad22101 day ago
JAVA Solution
 
public int lastIndex( String s) {                   for(int i=s.length()-1;i>=0;i--)       {           if(s.charAt(i)== '1')           {               return i;           }       }              return -1;          }
0
harshscode4 days ago
 for(int i=s.length()-1;i>=0;i--)           if(s[i]=='1')           return i;           return -1;
0
sumitk074 days ago
char c[s.length()];
        for(int i = 0;i<sizeof(c);i++){
            c[i] = s[i];
        }
        for(int i = s.length()-1;i>=0;i--){
            if(c[i] == '1')
            return i;
        }
        return -1;
0
ninthale6 days ago
 int lastIndex(string s)    {       for(int i=s.length()-1;i>=0;i--)         {           if(s[i]=='1')             {               return i;             }         }        return -1;   }};
0
swapniltayal4223 weeks ago
class Solution:   def lastIndex(self, s):       # code here       for i in range(len(s)-1, -1, -1):           if s[i] == "1":               return i;       return -1
0
divakar16693 weeks ago
// JAVA SOLUTION O(1)class Solution {   public int lastIndex( String s) {             if( s.contains("1") )      {          int x=s.lastIndexOf("1");          return x;      }      else      {          return -1;      }          }}
0
atif836141 month ago
JAVA SOLUTION :
class Solution {
    public int lastIndex( String s) {
     if (s.length() == 0) {
			return -1;
		}
		int res = 0;
		int flag = 0;
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '1') {
				res = i;
				flag = 1;
			}

		}
		if (flag == 1) {
			return res;
		}
		return -1;
	}
       
    }
0
coderh1 month ago
class Solution:
    def lastIndex(self, s):
        c=len(s)-1
        for i in range(len(s)-1,-1,-1):
            if s[i]=="1":
                return c
            c-=1
        return -1
0
adityagagtiwari1 month ago
This is a basic trivial solution. Using a boolean marker.
class Solution {   public int lastIndex( String s) {       int idx = 0;       if(s.length()==1 && s.charAt(0)=='1')       return 0;       boolean visited = false;       for(int i=0;i<s.length();i++)       {           char ch = s.charAt(i);           if(ch=='1')           {               visited = true;               idx = i;           }       }       if(idx>=0 && visited)       {           return idx;       }       else       {           return -1;       }          }}
0
kashyapabhishek221 month ago
def lastIndex(self, s):       # code here       if '1' not in s:           return -1       s = s[::-1]       res = len(s)-s.index('1') -1        return res
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.