📌  相关文章
📜  查找字符串字符的最后一个索引

📅  最后修改于: 2021-05-05 00:00:44             🧑  作者: Mango

给定一个字符串str和一个字符x,请在str中找到x的最后一个索引。
例子 :

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 

方法1(简单:从左遍历):
当x与当前字符匹配时,从左到右遍历给定的字符串并保持更新索引。

C++
// CPP program to find last index of
// character x in given string.
#include 
using namespace std;
 
// Returns last index of x if it is present.
// Else returns -1.
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;
}
 
// Driver code
int main()
{
    // String in which char is to be found
    string str = "geeksforgeeks";
 
    // char whose index is to be found
    char x = 'e';
    int index = findLastIndex(str, x);
    if (index == -1)
        cout << "Character not found";
    else
        cout << "Last index is " << index;
    return 0;
}


Java
// Java program to find last index
// of character x in given string.
import java.io.*;
 
class GFG {
  
// Returns last index of x if
// it is present Else returns -1.
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;
}
  
// Driver code
public static void main(String[] args)
{
    // String in which char is to be found
    String str = "geeksforgeeks";
 
    // char whose index is to be found
    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);
}
}
 
/* This code is contributed by Prerna Saini */


Python3
# A Python program to find last
# index of character x in given
# string.
 
# Returns last index of x if it
# is present. Else returns -1.
def findLastIndex(str, x):
    index = -1
    for i in range(0, len(str)):
        if str[i] == x:
            index = i
    return index
 
# Driver program
 
# String in which char is to be found
str = "geeksforgeeks"
 
# char whose index is to be found
x = 'e'
 
index = findLastIndex(str, x)
 
if index == -1:
    print("Character not found")
else:
    print('Last index is', index)
 
# This code is contributed by shrikant13.


C#
// C# program to find last index
// of character x in given string.
using System;
 
class GFG {
 
    // Returns last index of x if
    // it is present Else returns -1.
    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;
    }
     
    // Driver code
    public static void Main()
    {
        // String in which char is to be found
        string str = "geeksforgeeks";
     
        // char whose index is to be found
        char x = 'e';
     
        int index = findLastIndex(str, x);
        if (index == -1)
            Console.WriteLine("Character not found");
        else
            Console.WriteLine("Last index is " + index);
    }
}
 
/* This code is contributed by vt_m */


PHP


Javascript


CPP
// Simple CPP program to find last index of
// character x in given string.
#include 
using namespace std;
 
// Returns last index of x if it is present.
// Else returns -1.
int findLastIndex(string& str, char x)
{
    // Traverse from right
    for (int i = str.length() - 1; i >= 0; i--)
        if (str[i] == x)
            return i;
 
    return -1;
}
 
// Driver code
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
// Java code to find last index
// character x in given string.
import java.io.*;
class GFG {
  
// Returns last index of x if
// it is present. Else returns -1.
static int findLastIndex(String str, Character x)
{
    // Traverse from right
    for (int i = str.length() - 1; i >= 0; i--)
        if (str.charAt(i) == x)
            return i;
 
    return -1;
}
  
// Driver code
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);
}
}
// This code is contributed by Prerna Saini


Python3
# Simple Python3 program to find last
# index of character x in given string.
 
# Returns last index of x if it is
# present. Else returns -1.
def findLastIndex(str, x):
 
    # Traverse from right
    for i in range(len(str) - 1, -1,-1):
        if (str[i] == x):
            return i
 
    return -1
 
# Driver code
str = "geeksforgeeks"
x = 'e'
index = findLastIndex(str, x)
 
if (index == -1):
    print("Character not found")
else:
    print("Last index is " ,index)
 
# This code is contributed by Smitha


C#
// C# code to find last index
// character x in given string.
using System;
 
class GFG {
 
    // Returns last index of x if
    // it is present. Else returns -1.
    static int findLastIndex(string str, char x)
    {
        // Traverse from right
        for (int i = str.Length - 1; i >= 0; i--)
            if (str[i] == x)
                return i;
     
        return -1;
    }
     
    // Driver code
    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);
    }
}
// This code is contributed by vt_m


PHP
= 0; $i--)
        if ($str[$i] == $x)
            return $i;
 
    return -1;
}
 
// Driver code
$str = "geeksforgeeks";
$x = 'e';
$index = findLastIndex($str, $x);
if ($index == -1)
    echo("Character not found");
else
    echo("Last index is " . $index);
 
// This code is contributed by Ajit.
?>


输出:

Last index is 10

时间复杂度:Θ(n)
方法2(高效:从右向后移动):
在上面的方法1中,我们总是遍历完整的字符串。通过这种方法,在存在x的所有情况下,我们都可以避免完全遍历。这个想法是从右边穿过并在我们找到字符停下。

CPP

// Simple CPP program to find last index of
// character x in given string.
#include 
using namespace std;
 
// Returns last index of x if it is present.
// Else returns -1.
int findLastIndex(string& str, char x)
{
    // Traverse from right
    for (int i = str.length() - 1; i >= 0; i--)
        if (str[i] == x)
            return i;
 
    return -1;
}
 
// Driver code
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

// Java code to find last index
// character x in given string.
import java.io.*;
class GFG {
  
// Returns last index of x if
// it is present. Else returns -1.
static int findLastIndex(String str, Character x)
{
    // Traverse from right
    for (int i = str.length() - 1; i >= 0; i--)
        if (str.charAt(i) == x)
            return i;
 
    return -1;
}
  
// Driver code
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);
}
}
// This code is contributed by Prerna Saini

Python3

# Simple Python3 program to find last
# index of character x in given string.
 
# Returns last index of x if it is
# present. Else returns -1.
def findLastIndex(str, x):
 
    # Traverse from right
    for i in range(len(str) - 1, -1,-1):
        if (str[i] == x):
            return i
 
    return -1
 
# Driver code
str = "geeksforgeeks"
x = 'e'
index = findLastIndex(str, x)
 
if (index == -1):
    print("Character not found")
else:
    print("Last index is " ,index)
 
# This code is contributed by Smitha

C#

// C# code to find last index
// character x in given string.
using System;
 
class GFG {
 
    // Returns last index of x if
    // it is present. Else returns -1.
    static int findLastIndex(string str, char x)
    {
        // Traverse from right
        for (int i = str.Length - 1; i >= 0; i--)
            if (str[i] == x)
                return i;
     
        return -1;
    }
     
    // Driver code
    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);
    }
}
// This code is contributed by vt_m

的PHP

= 0; $i--)
        if ($str[$i] == $x)
            return $i;
 
    return -1;
}
 
// Driver code
$str = "geeksforgeeks";
$x = 'e';
$index = findLastIndex($str, $x);
if ($index == -1)
    echo("Character not found");
else
    echo("Last index is " . $index);
 
// This code is contributed by Ajit.
?>

输出:

Last index is 10

时间复杂度: O(n)