Given a string s consisting only of '0' and '1', find the last index at which '1' occurs. If '1' is not present in the string, return -1.
Examples:
Input: s = "01001"
Output: 4
Explanation: Last index of 1 in given string is 4.Input: s = "0"
Output: -1
Explanation: Since, 1 is not present, so output is -1.
Table of Content
Traverse the String from Left to Right - O(n) Time and O(1) Space
The idea is to traverse the complete string from left to right and keep updating the answer whenever '1' is found. After traversing the entire string, the stored index will represent the last occurrence of '1'.
Working of Approach:
- Initialize res with -1.
- Traverse the string from index 0 to n - 1.
- Whenever the current character is '1', update res with its index.
- Continue traversing until the end of the string.
- Return res, which remains -1 if no '1' is found.
#include <iostream>
#include <string>
using namespace std;
// Function to find the last index of '1' in a string
int lastIndex(string &s)
{
// Initialize answer as -1
int res = -1;
// Traverse the string from left to right
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '1')
{
res = i;
}
}
// Return the last index of '1'
return res;
}
int main()
{
string s = "01001";
cout << lastIndex(s) << endl;
return 0;
}
import java.util.*;
// Function to find the last index of '1' in a string
public class GFG {
public static int lastIndex(String s)
{
// Initialize answer as -1
int res = -1;
// Traverse the string from left to right
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
res = i;
}
}
// Return the last index of '1'
return res;
}
public static void main(String[] args)
{
String s = "01001";
System.out.println(lastIndex(s));
}
}
# Function to find the last index of '1' in a string
def lastIndex(s):
# Initialize answer as -1
res = -1
# Traverse the string from left to right
for i in range(len(s)):
if s[i] == '1':
res = i
# Return the last index of '1'
return res
if __name__ == "__main__":
s = "01001"
print(lastIndex(s))
using System;
// Function to find the last index of '1' in a string
public class GFG {
public static int lastIndex(string s)
{
// Initialize answer as -1
int res = -1;
// Traverse the string from left to right
for (int i = 0; i < s.Length; i++) {
if (s[i] == '1') {
res = i;
}
}
// Return the last index of '1'
return res;
}
public static void Main()
{
string s = "01001";
Console.WriteLine(lastIndex(s));
}
}
// Function to find the last index of '1' in a string
function lastIndex(s)
{
// Initialize answer as -1
let res = -1;
// Traverse the string from left to right
for (let i = 0; i < s.length; i++) {
if (s[i] === "1") {
res = i;
}
}
// Return the last index of '1'
return res;
}
// Driver Code
let s = "01001";
console.log(lastIndex(s));
Output
4
Traverse the String from Right to Left - O(n) Time and O(1) Space
The idea is to traverse the string from right to left. The first '1' encountered will be the last occurrence of '1' in the string, so we can immediately return its index.
Working of Approach:
- Start traversing the string from the last index.
- Check each character while moving towards the beginning.
- If the current character is '1', return its index immediately.
- This is the last occurrence because we are traversing from right to left.
- If no '1' is found, return -1.
Let us understand with an example:
Input: s = "01001"
- Start traversing the string from the last index, i = 4.
- At i = 4, s[4] = '1', so the condition s[i] == '1' becomes true.
- Since we are traversing from right to left, this is the last occurrence of '1' in the string.
- Immediately return index 4 without checking the remaining characters.
- Therefore, the last index of '1' in "01001" is 4.
#include <iostream>
#include <string>
using namespace std;
// Function to find the last index of '1' in a string
int lastIndex(string &s)
{
int n = s.length();
// Traverse the string from right to left
for (int i = n - 1; i >= 0; i--)
{
// Return the first '1' found from the right
if (s[i] == '1')
{
return i;
}
}
// Return -1 if '1' is not present
return -1;
}
int main()
{
string s = "01001";
cout << lastIndex(s) << endl;
return 0;
}
import java.util.*;
public class GFG {
// Function to find the last index of '1' in a string
public static int lastIndex(String s)
{
int n = s.length();
// Traverse the string from right to left
for (int i = n - 1; i >= 0; i--) {
// Return the first '1' found from the right
if (s.charAt(i) == '1') {
return i;
}
}
// Return -1 if '1' is not present
return -1;
}
public static void main(String[] args)
{
String s = "01001";
System.out.println(lastIndex(s));
}
}
def lastIndex(s):
# Function to find the last index of '1' in a string
n = len(s)
# Traverse the string from right to left
for i in range(n - 1, -1, -1):
# Return the first '1' found from the right
if s[i] == '1':
return i
# Return -1 if '1' is not present
return -1
if __name__ == '__main__':
s = "01001"
print(lastIndex(s))
using System;
class GFG {
// Function to find the last index of '1' in a string
static int lastIndex(string s)
{
int n = s.Length;
// Traverse the string from right to left
for (int i = n - 1; i >= 0; i--) {
// Return the first '1' found from the right
if (s[i] == '1') {
return i;
}
}
// Return -1 if '1' is not present
return -1;
}
static void Main()
{
string s = "01001";
Console.WriteLine(lastIndex(s));
}
}
function lastIndex(s)
{
// Function to find the last index of '1' in a string
let n = s.length;
// Traverse the string from right to left
for (let i = n - 1; i >= 0; i--) {
// Return the first '1' found from the right
if (s[i] === "1") {
return i;
}
}
// Return -1 if '1' is not present
return -1;
}
// Driver Code
let s = "01001";
console.log(lastIndex(s));
Output
4