查找一个字符串在另一个字符串中所有出现的索引

给定str1和str2两个字符串,任务是打印str1中str2出现的索引(考虑从0开始的索引)。如果没有出现此类索引,请打印“无”。

null

例如:

Input : GeeksforGeeks        GeeksOutput : 0 8Input : GFG        gOutput : NONE

A. 简单解决方案 就是逐个检查给定字符串的所有子字符串。如果子字符串匹配,请打印其索引。

C++

// C++ program to find indices of all
// occurrences of one string in other.
#include <iostream>
using namespace std;
void printIndex(string str, string s)
{
bool flag = false ;
for ( int i = 0; i < str.length(); i++) {
if (str.substr(i, s.length()) == s) {
cout << i << " " ;
flag = true ;
}
}
if (flag == false )
cout << "NONE" ;
}
int main()
{
string str1 = "GeeksforGeeks" ;
string str2 = "Geeks" ;
printIndex(str1, str2);
return 0;
}


JAVA

// Java program to find indices of all
// occurrences of one String in other.
class GFG {
static void printIndex(String str, String s)
{
boolean flag = false ;
for ( int i = 0 ; i < str.length() - s.length() + 1 ; i++) {
if (str.substring(i, i + s.length()).equals(s)) {
System.out.print(i + " " );
flag = true ;
}
}
if (flag == false ) {
System.out.println( "NONE" );
}
}
// Driver code
public static void main(String[] args)
{
String str1 = "GeeksforGeeks" ;
String str2 = "Geeks" ;
printIndex(str1, str2);
}
}
// This code is contributed by Rajput-JI


Python3

# Python program to find indices of all
# occurrences of one String in other.
def printIndex( str , s):
flag = False ;
for i in range ( len ( str )):
if ( str [i:i + len (s)] = = s):
print ( i, end = " " );
flag = True ;
if (flag = = False ):
print ( "NONE" );
# Driver code
str1 = "GeeksforGeeks" ;
str2 = "Geeks" ;
printIndex(str1, str2);
# This code contributed by PrinciRaj1992


C#

// C# program to find indices of all
// occurrences of one String in other.
using System;
class GFG {
static void printIndex(String str, String s)
{
bool flag = false ;
for ( int i = 0; i < str.Length - s.Length + 1; i++) {
if (str.Substring(i,
s.Length)
.Equals(s)) {
Console.Write(i + " " );
flag = true ;
}
}
if (flag == false ) {
Console.WriteLine( "NONE" );
}
}
// Driver code
public static void Main(String[] args)
{
String str1 = "GeeksforGeeks" ;
String str2 = "Geeks" ;
printIndex(str1, str2);
}
}
// This code is contributed by 29AjayKumar


PHP

<?php
// PHP program to find indices of all
// occurrences of one string in other.
function printIndex( $str , $s )
{
$flag = false;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
if ( substr ( $str , $i , strlen ( $s )) == $s )
{
echo $i . " " ;
$flag = true;
}
}
if ( $flag == false)
echo "NONE" ;
}
// Driver Code
$str1 = "GeeksforGeeks" ;
$str2 = "Geeks" ;
printIndex( $str1 , $str2 );
// This code is contributed by mits
?>


Javascript

<script>
// JavaScript program to find indices of all
// occurrences of one String in other.
function printIndex(str, s) {
var flag = false ;
for ( var i = 0; i < str.length - s.length + 1; i++) {
if (str.substring(i, s.length + i) == s) {
document.write(i + " " );
flag = true ;
}
}
if (flag === false ) {
document.write( "NONE" );
}
}
// Driver code
var str1 = "GeeksforGeeks" ;
var str2 = "Geeks" ;
printIndex(str1, str2);
</script>


输出:

0 8

时间复杂性: O(n*n)

有效解决方案 就是 KMP字符串匹配算法 .

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享