Given a string str and a character x, find last index of x in str.
Examples :
Input : str = "geeks", x = 'e'
Output : 2
Last index of 'e' in "geeks" is: 2
Input : str = "Hello world!", x = 'o'
Output : 7
Last index of 'o' is: 7
Method 1 (Simple : Traverse from left): Traverse given string from left to right and keep updating index whenever x matches with current character.
Implementation:
C++
#include <iostream>
using namespace std;
int findLastIndex(string& str, char x)
{
int index = -1;
for (int i = 0; i < str.length(); i++)
if (str[i] == x)
index = i;
return index;
}
int main()
{
string str = "geeksforgeeks";
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
cout << "Character not found";
else
cout << "Last index is " << index;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findLastIndex(String str, Character x)
{
int index = -1;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == x)
index = i;
return index;
}
public static void main(String[] args)
{
String str = "geeksforgeeks";
Character x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
System.out.println("Character not found");
else
System.out.println("Last index is " + index);
}
}
|
Python3
def findLastIndex(str, x):
index = -1
for i in range(0, len(str)):
if str[i] == x:
index = i
return index
str = "geeksforgeeks"
x = 'e'
index = findLastIndex(str, x)
if index == -1:
print("Character not found")
else:
print('Last index is', index)
|
C#
using System;
class GFG {
static int findLastIndex(string str, char x)
{
int index = -1;
for (int i = 0; i < str.Length; i++)
if (str[i] == x)
index = i;
return index;
}
public static void Main()
{
string str = "geeksforgeeks";
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
Console.WriteLine("Character not found");
else
Console.WriteLine("Last index is " + index);
}
}
|
PHP
<?php
function findLastIndex($str, $x)
{
$index = -1;
for ($i = 0; $i < strlen($str); $i++)
if ($str[$i] == $x)
$index = $i;
return $index;
}
$str = "geeksforgeeks";
$x = 'e';
$index = findLastIndex($str, $x);
if ($index == -1)
echo("Character not found");
else
echo("Last index is " . $index);
?>
|
Javascript
<script>
function findLastIndex(str, x)
{
let index = -1;
for (let i = 0; i < str.length; i++)
if (str[i] == x)
index = i;
return index;
}
let str = "geeksforgeeks";
let x = 'e';
let index = findLastIndex(str, x);
if (index == -1)
document.write("Character not found");
else
document.write("Last index is " + index);
</script>
|
Time Complexity : O(n)
Auxiliary Space: O(1)
Method 2 (Efficient : Traverse from right): In above method 1, we always traverse complete string. In this method, we can avoid complete traversal in all those cases when x is present. The idea is to traverse from right side and stop as soon as we find character.
Implementation:
CPP
#include <iostream>
using namespace std;
int findLastIndex(string& str, char x)
{
for (int i = str.length() - 1; i >= 0; i--)
if (str[i] == x)
return i;
return -1;
}
int main()
{
string str = "geeksforgeeks";
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
cout << "Character not found";
else
cout << "Last index is " << index;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findLastIndex(String str, Character x)
{
for (int i = str.length() - 1; i >= 0; i--)
if (str.charAt(i) == x)
return i;
return -1;
}
public static void main(String[] args)
{
String str = "geeksforgeeks";
Character x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
System.out.println("Character not found");
else
System.out.println("Last index is " + index);
}
}
|
Python3
def findLastIndex(str, x):
for i in range(len(str) - 1, -1,-1):
if (str[i] == x):
return i
return -1
str = "geeksforgeeks"
x = 'e'
index = findLastIndex(str, x)
if (index == -1):
print("Character not found")
else:
print("Last index is " ,index)
|
C#
using System;
class GFG {
static int findLastIndex(string str, char x)
{
for (int i = str.Length - 1; i >= 0; i--)
if (str[i] == x)
return i;
return -1;
}
public static void Main()
{
string str = "geeksforgeeks";
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
Console.WriteLine("Character not found");
else
Console.WriteLine("Last index is " + index);
}
}
|
PHP
<?php
function findLastIndex($str, $x)
{
for ($i = strlen($str) - 1; $i >= 0; $i--)
if ($str[$i] == $x)
return $i;
return -1;
}
$str = "geeksforgeeks";
$x = 'e';
$index = findLastIndex($str, $x);
if ($index == -1)
echo("Character not found");
else
echo("Last index is " . $index);
?>
|
Javascript
<script>
function findLastIndex(str, x)
{
for (let i = str.length - 1; i >= 0; i--)
if (str[i] == x)
return i;
return -1;
}
let str = "geeksforgeeks";
let x = 'e';
let index = findLastIndex(str, x);
if (index == -1)
document.write("Character not found");
else
document.write("Last index is " + index);
</script>
|
Time Complexity : O(n)
Auxiliary Space: O(1)
This article is contributed by Aarti_Rathi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.