Metode 1 (Sederhana. Melintasi dari kiri). Lintasi string yang diberikan dari kiri ke kanan dan terus perbarui indeks setiap kali x cocok dengan karakter saat ini.
Penerapan
C++
// CPP program to find last index of
// character x in given string.
#include <iostream>
using namespace std;
// Returns last index of x if it is present.
Last index is 100
Last index is 101 Last index is 102Last index is 103 Last index is 104
Last index is 105
Last index is 106Last index is 101 Last index is 108
Last index is 106Last index is 100 Last index is 101Last index is 101 Last index is 103
Last index is 104Last index is 105 Last index is 106
Last index is 107Last index is 108
Last index is 106// CPP program to find last index of0 // CPP program to find last index of1
// CPP program to find last index of2
// CPP program to find last index of_3
Last index is 101 // CPP program to find last index of5
Last index is 105
Last index is 106// CPP program to find last index of8
Last index is 106// character x in given string.0// character x in given string.1// character x in given string.2
Last index is 106// character x in given string.4
Last index is 106Last index is 103 // character x in given string.7// character x in given string.8// character x in given string.2
Last index is 106Last index is 101 #include <iostream>2
Last index is 106Last index is 105 #include <iostream>5
Last index is 104#include <iostream>7#include <iostream>8// character x in given string.2
Last index is 106using1
Last index is 104#include <iostream>7using4 using5
Last index is 106// CPP program to find last index of0 using8
// CPP program to find last index of2
Jawa
namespace_0
namespace_1
namespace2 namespace3
namespace4 namespace5
namespace_6
namespace_7
namespace_8
namespace9 Last index is 101 std;1
Last index is 105
Last index is 106Last index is 101 std;5std;6// character x in given string.2
Last index is 106Last index is 100 Last index is 101Last index is 101 // Returns last index of x if it is present.2// Returns last index of x if it is present.3// Returns last index of x if it is present.4
Last index is 104Last index is 105 // Returns last index of x if it is present.7
Last index is 107Last index is 108
Last index is 106// CPP program to find last index of0 // CPP program to find last index of1
// CPP program to find last index of2
namespace_6
// CPP program to find last index of_3
Last index is 1006 namespace9 Last index is 1008 Last index is 1009
Last index is 105
Last index is 106// CPP program to find last index of8
Last index is 106Last index is 1014// character x in given string.1// character x in given string.2
Last index is 106// character x in given string.4
Last index is 106Last index is 1020// character x in given string.8// character x in given string.2
Last index is 106Last index is 101 #include <iostream>2
Last index is 106Last index is 105 Last index is 1028std;6Last index is 1030
Last index is 104Last index is 1032#include <iostream>8Last index is 1034
Last index is 106using1
Last index is 104Last index is 1032using4 Last index is 1040
// CPP program to find last index of2
// CPP program to find last index of2
Last index is 10_43
Python3
Last index is 10_44
Last index is 10_45
Last index is 10_46
Last index is 10_47
Last index is 10_48
Last index is 1049 Last index is 1050Last index is 1051Last index is 1052
Last index is 106Last index is 1054Last index is 1055 Last index is 1056________471______6
Last index is 106Last index is 100 Last index is 1060Last index is 1061 Last index is 1062Last index is 101// Returns last index of x if it is present.3Last index is 1065Last index is 1066Last index is 101Last index is 1051_________0
Last index is 104Last index is 105 Last index is 1051Last index is 1073Last index is 1055Last index is 1055 Last index is 1076
Last index is 107Last index is 1054Last index is 1055 Last index is 1060
Last index is 106// CPP program to find last index of0 Last index is 1054
Last index is 10_84
Last index is 10_85
Last index is 1051 Last index is 1055 // character x in given string.1
Last index is 10_89
Last index is 1090Last index is 1055 // character x in given string.8
Last index is 1054Last index is 1055 Last index is 1050Last index is 1051Last index is 1097
Last index is 105 Last index is 1054Last index is 1055Last index is 1055 Last index is 1056std;6Last index is 1004
Last index is 106Last index is 1006Last index is 101#include <iostream>8Last index is 1030
using1Last index is 1004
Last index is 106Last index is 1006Last index is 101Last index is 1015Last index is 1016
Last index is 10_17
C#
Last index is 10_18
namespace_1
using Last index is 1021
namespace4 namespace5
Last index is 106namespace7
Last index is 106namespace8
Last index is 106namespace9 Last index is 101 Last index is 1050Last index is 1032 Last index is 1033Last index is 103 Last index is 104
Last index is 106Last index is 105
Last index is 104Last index is 101 Last index is 108
Last index is 104Last index is 100 Last index is 101Last index is 101 Last index is 1045
Last index is 107Last index is 105 Last index is 106
Last index is 1049Last index is 108
Last index is 104// CPP program to find last index of0 // CPP program to find last index of1
Last index is 106// CPP program to find last index of2
Last index is 10_6
Last index is 106// CPP program to find last index of3
Last index is 106Last index is 1006 namespace9 Last index is 1008 Last index is 1063
Last index is 106Last index is 105
Last index is 104// CPP program to find last index of8
Last index is 104Last index is 1032 Last index is 1070// character x in given string.1// character x in given string.2
Last index is 10_6
Last index is 104// character x in given string.4
Last index is 104Last index is 103 // character x in given string.7// character x in given string.8// character x in given string.2
Last index is 10_6
Last index is 104Last index is 101 #include <iostream>2
Last index is 104Last index is 105 #include <iostream>5
Last index is 107Last index is 1089#include <iostream>8Last index is 1034
Last index is 104using1
Last index is 107Last index is 1089using4 Last index is 1040
Last index is 106// CPP program to find last index of2
// CPP program to find last index of2
// CPP program to find last index of_01
PHP
// CPP program to find last index of_02
// CPP program to find last index of_03
// character x in given string.
// CPP program to find last index of_05
// CPP program to find last index of_06
Last index is 100
// CPP program to find last index of08 Last index is 1050// CPP program to find last index of10Last index is 1065// CPP program to find last index of12Last index is 1030
Last index is 105
Last index is 106// CPP program to find last index of16 // CPP program to find last index of17
Last index is 106Last index is 100 Last index is 101// CPP program to find last index of21 // CPP program to find last index of22// CPP program to find last index of____21 // CPP program to find last index of_____________________________________________________________________5466666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666660
Last index is 104Last index is 105 Last index is 101// CPP program to find last index of10// CPP program to find last index of35// CPP program to find last index of21// CPP program to find last index of37// CPP program to find last index of12Last index is 1030
Last index is 107// CPP program to find last index of16 Last index is 1055// CPP program to find last index of21// character x in given string.2
Last index is 106// CPP program to find last index of0 // CPP program to find last index of16// character x in given string.2
// CPP program to find last index of2
// CPP program to find last index of_3
// CPP program to find last index of_51
// CPP program to find last index of_52
// CPP program to find last index of10 Last index is 1055// character x in given string.1// character x in given string.2
// CPP program to find last index of_57
// CPP program to find last index of_58
// CPP program to find last index of12 Last index is 1055// character x in given string.8// character x in given string.2
// CPP program to find last index of16 // CPP program to find last index of64// CPP program to find last index of10Last index is 1065// CPP program to find last index of12Last index is 1034
Last index is 105 Last index is 101// CPP program to find last index of16 // CPP program to find last index of72
Last index is 106// CPP program to find last index of74Last index is 101#include <iostream>8________0______34
using_1
Last index is 106// CPP program to find last index of74Last index is 101using4 ________466______83// CPP program to find last index of16Last index is 1034
// CPP program to find last index of_86
// CPP program to find last index of_87
Javascript
// CPP program to find last index of_88
// CPP program to find last index of_89
namespace_1
namespace_7
namespace_8
// CPP program to find last index of_08 // CPP program to find last index of94
Last index is 105
Last index is 106// CPP program to find last index of97
Last index is 106Last index is 100 // character x in given string.00
Last index is 104Last index is 105 Last index is 106
Last index is 107Last index is 108
Last index is 106// CPP program to find last index of0 // CPP program to find last index of1
// CPP program to find last index of2
// character x in given string._10
// CPP program to find last index of_3
Last index is 106// CPP program to find last index of8
Last index is 106// character x in given string.15// character x in given string.1// character x in given string.2
// character x in given string._10
Last index is 106// character x in given string.4
Last index is 106// character x in given string.22// character x in given string.8// character x in given string.2
// character x in given string._10
Last index is 106// character x in given string.27
Last index is 106Last index is 105 #include <iostream>5
Last index is 104// character x in given string.32#include <iostream>8Last index is 1034
Last index is 106using1
________467______37// character x in given string.32using4 Last index is 1040
// character x in given string._41
// character x in given string.41// character x in given string.43
// character x in given string._44
KeluaranLast index is 10
Kompleksitas Waktu. Pada)
Ruang Bantu. O(1)
Metode 2 (Efisien. Melintasi dari kanan). Dalam metode 1 di atas, kami selalu melintasi string lengkap. Dalam metode ini, kita dapat menghindari traversal lengkap dalam semua kasus ketika x hadir. Idenya adalah untuk melintasi dari sisi kanan dan berhenti segera setelah kita menemukan karakter.
Penerapan
BPK
// character x in given string._45
// character x in given string.
#include <iostream>
using namespace std;
// Returns last index of x if it is present.
Last index is 100
Last index is 101 Last index is 102Last index is 103 Last index is 104
Last index is 105
Last index is 106// character x in given string.59
Last index is 106Last index is 100 Last index is 101Last index is 101 // character x in given string.64
Last index is 104Last index is 105 Last index is 106
Last index is 107// CPP program to find last index of0 // character x in given string.70
Last index is 106// CPP program to find last index of0 // character x in given string.73
// CPP program to find last index of2
// CPP program to find last index of_3
Last index is 101 // CPP program to find last index of5
Last index is 105
Last index is 106// character x in given string.0// character x in given string.1// character x in given string.2
Last index is 106Last index is 103 // character x in given string.7// character x in given string.8// character x in given string.2
Last index is 106Last index is 101 #include <iostream>2
Last index is 106Last index is 105 #include <iostream>5
Last index is 104#include <iostream>7#include <iostream>8// character x in given string.2
Last index is 106using1
Last index is 104#include <iostream>7using4 using5
Last index is 106// CPP program to find last index of0 using8
// CPP program to find last index of2
Jawa
#include <iostream>_08
// character x in given string.
namespace2 namespace3
namespace4 namespace5
namespace_6
namespace_7
#include <iostream>_16
namespace9 Last index is 101 std;1
Last index is 105
Last index is 106// character x in given string.59
Last index is 106Last index is 100 Last index is 101Last index is 101 #include <iostream>27std;6#include <iostream>29// Returns last index of x if it is present.3#include <iostream>31
Last index is 104Last index is 105 // Returns last index of x if it is present.7
Last index is 107// CPP program to find last index of0 // character x in given string.70
Last index is 106// CPP program to find last index of0 Last index is 1056std;6// character x in given string.2
// CPP program to find last index of2
namespace_6
// CPP program to find last index of_3
Last index is 1006 namespace9 Last index is 1008 Last index is 1009
Last index is 105
Last index is 106Last index is 1014// character x in given string.1// character x in given string.2
Last index is 106Last index is 1020// character x in given string.8// character x in given string.2
Last index is 106Last index is 101 #include <iostream>2
Last index is 106Last index is 105 Last index is 1028std;6Last index is 1030
Last index is 104Last index is 1032#include <iostream>8Last index is 1034
Last index is 106using1
Last index is 104Last index is 1032using4 Last index is 1040
// CPP program to find last index of2
// CPP program to find last index of2
#include <iostream>_79
Python3
#include <iostream>_80
#include <iostream>_81
#include <iostream>_82
#include <iostream>_83
Last index is 1049 Last index is 1050Last index is 1051Last index is 1052
Last index is 106#include <iostream>89
Last index is 106Last index is 100 Last index is 1060Last index is 1061 Last index is 1062Last index is 101Last index is 1066Last index is 101Last index is 1051Last index is 1030Last index is 1056 std;6Last index is 1065Last index is 1056std;6Last index is 1065Last index is 1056std;6using08
Last index is 104Last index is 105 Last index is 101Last index is 1051Last index is 1073Last index is 1055Last index is 1055 using16
Last index is 107// CPP program to find last index of0 Last index is 1060
Last index is 106// CPP program to find last index of0 Last index is 1056std;6
using_24
Last index is 1051 Last index is 1055 // character x in given string.1
Last index is 1090Last index is 1055 // character x in given string.8
Last index is 1054Last index is 1055 Last index is 1050Last index is 1051Last index is 1097
Last index is 105 using37Last index is 1055Last index is 1055 Last index is 1056std;6using08
Last index is 106Last index is 1006Last index is 101#include <iostream>8Last index is 1030
using1Last index is 1004
Last index is 106Last index is 1006Last index is 101using4 using54
using_55
C#
using_56
// character x in given string.
using Last index is 1021
namespace4 namespace5
Last index is 106namespace7
Last index is 106#include <iostream>16
Last index is 106namespace9 Last index is 101 Last index is 1050Last index is 1032 Last index is 1033Last index is 103 Last index is 104
Last index is 106Last index is 105
Last index is 104// character x in given string.59
Last index is 104Last index is 100 Last index is 101Last index is 101 using82
Last index is 107Last index is 105 Last index is 106
Last index is 1049// CPP program to find last index of0 // character x in given string.70
Last index is 10_6
Last index is 104// CPP program to find last index of0 // character x in given string.73
Last index is 106// CPP program to find last index of2
Last index is 10_6
Last index is 106// CPP program to find last index of3
Last index is 106Last index is 1006 namespace9 Last index is 1008 Last index is 1063
Last index is 106Last index is 105
Last index is 104Last index is 1032 Last index is 1070// character x in given string.1// character x in given string.2
Last index is 104Last index is 103 // character x in given string.7// character x in given string.8// character x in given string.2
Last index is 104Last index is 101 #include <iostream>2
Last index is 104Last index is 105 #include <iostream>5
Last index is 107Last index is 1089#include <iostream>8Last index is 1034
Last index is 104using1
Last index is 107Last index is 1089using4 Last index is 1040
Last index is 106// CPP program to find last index of2
// CPP program to find last index of2
namespace_34
PHP
// CPP program to find last index of_02
namespace_36
namespace_1
namespace_38
namespace_39
// CPP program to find last index of08 Last index is 1050// CPP program to find last index of10Last index is 1065// CPP program to find last index of12Last index is 1030
Last index is 105
Last index is 10_6
Last index is 106// character x in given string.59
Last index is 106Last index is 100 Last index is 101// CPP program to find last index of21 Last index is 1055// CPP program to find last index of___________________________________// CPP program to find last index of_______________________56666666666666666666666666666666666666666666666666666666666666.
Last index is 104Last index is 105 Last index is 101// CPP program to find last index of10// CPP program to find last index of35// CPP program to find last index of21// CPP program to find last index of37// CPP program to find last index of12Last index is 1030
Last index is 107// CPP program to find last index of0 // CPP program to find last index of21// character x in given string.2
Last index is 106// CPP program to find last index of0 // character x in given string.73
// CPP program to find last index of2
// CPP program to find last index of_3
// CPP program to find last index of10 Last index is 1055// character x in given string.1// character x in given string.2
// CPP program to find last index of12 Last index is 1055// character x in given string.8// character x in given string.2
// CPP program to find last index of16 // CPP program to find last index of64// CPP program to find last index of10Last index is 1065// CPP program to find last index of12Last index is 1034
Last index is 105 Last index is 101// CPP program to find last index of16 // CPP program to find last index of72
Last index is 106// CPP program to find last index of74Last index is 101#include <iostream>8________0______34
using_1
Last index is 106// CPP program to find last index of74Last index is 101using4 ________466______83// CPP program to find last index of16Last index is 1034
// CPP program to find last index of_86
// CPP program to find last index of_87
Javascript
// CPP program to find last index of_88
Last index is 106std;16
Last index is 10_6
Last index is 106namespace7
Last index is 106#include <iostream>16
Last index is 106// CPP program to find last index of08 // CPP program to find last index of94
Last index is 106Last index is 105
Last index is 104// character x in given string.59
Last index is 104Last index is 100 std;31
Last index is 107Last index is 105 Last index is 106
Last index is 1049// CPP program to find last index of0 // character x in given string.70
// character x in given string._41
Last index is 104// CPP program to find last index of0 // character x in given string.73
Last index is 106// CPP program to find last index of2
Last index is 10_6
Last index is 106// character x in given string.15// character x in given string.1// character x in given string.2
Last index is 106// character x in given string.22// character x in given string.8// character x in given string.2
Last index is 106// character x in given string.27
Last index is 106Last index is 105 #include <iostream>5
std;58// character x in given string.32#include <iostream>8Last index is 1034
Last index is 106using1
std;58// character x in given string.32using4 Last index is 1040
Last index is 10_7
// character x in given string._44
KeluaranLast index is 10
Kompleksitas Waktu. Pada)
Ruang Bantu. O(1)
Artikel ini disumbangkan oleh Aarti_Rathi. Jika Anda menyukai GeeksforGeeks dan ingin berkontribusi, Anda juga dapat menulis artikel menggunakan tulis. geeksforgeeks. org atau kirimkan artikel Anda ke review-team@geeksforgeeks. org. Lihat artikel Anda muncul di halaman utama GeeksforGeeks dan bantu Geeks lainnya.