检查给定的字符串是否是给定数组的前缀子数组
给定一个字符串str和一个单词数组word[] ,任务是找出str是否是word[]的前缀字符串。
例子:
Input: str = “indiaismycountry”,
word[] = {“india”, “is”, “my”, “country”, “and”, “i”, “love”, “india”}
Output: true
Explanation: String str can be made by concatenating “india”, “is”, “my” and “country” together.
Input: str = “indianism”,
word[] = {“india”, “is”, “my”, “country”, “and”, “i”, “love”, “india”}
Output: false
Explanation: It is impossible to make str using the prefixes of the word array.
方法:这是一个简单的实现相关问题。请按照以下步骤操作:
- 取一个名为ans的空字符串。
- 遍历单词数组并继续将单词数组的每个元素添加到ans 。
- 添加到ans后,将其与s进行比较,如果它们都匹配,则返回 true,否则继续。
- 如果迭代结束并且ans与s不匹配,则返回 false。
下面是实现上述方法的 C++ 程序——
C++
// C++ program to implement the
// given approach
#include
using namespace std;
// Function to check weather string
// is prefix
bool isPrefixString(string s,
vector& word)
{
// ans is taken as an empty string
string ans = "";
// N is used to store
// the size of word array
int N = word.size();
// Iterating over the word array
for (int i = 0; i < N; i++) {
// Adding element by element
// of the array
ans += word[i];
// If ans and str are same
// return true
if (ans == s)
return true;
}
// As iteration is ending which means
// string is not prefix so return false.
return false;
}
// Driver code
int main()
{
string str = "indiaismycountry";
vector word
= { "india", "is", "my",
"country", "and", "i",
"love", "india" };
bool ans = isPrefixString(str, word);
if (ans)
cout << "True";
else
cout << "False";
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to check weather string
// is prefix
static Boolean isPrefixString(String s,
String word[])
{
// ans is taken as an empty string
String ans = "";
// N is used to store
// the size of word array
int N = word.length;
// Iterating over the word array
for (int i = 0; i < N; i++) {
// Adding element by element
// of the array
ans += word[i];
// If ans and str are same
// return true
if (ans.equals(s))
return true;
}
// As iteration is ending which means
// string is not prefix so return false.
return false;
}
// Driver code
public static void main (String[] args)
{
String str = "indiaismycountry";
String word[]
= { "india", "is", "my",
"country", "and", "i",
"love", "india" };
Boolean ans = isPrefixString(str, word);
if (ans)
System.out.println("True");
else
System.out.println("False");
}
}
// This code is contributed by hrithikgarg03188.
Python
# Pyhton program to implement the
# given approach
# Function to check weather string
# is prefix
def isPrefixString(s, word):
# ans is taken as an empty string
ans = ""
# N is used to store
# the size of word array
N = len(word)
# Iterating over the word array
for i in range(0, N):
# Adding element by element
# of the array
ans = ans + (word[i])
# If ans and str are same
# return true
if (ans == s):
return True
# As iteration is ending which means
# string is not prefix so return false.
return False
# Driver code
str = "indiaismycountry"
word = ["india", "is", "my", "country", "and", "i", "love", "india"]
ans = isPrefixString(str, word)
if (ans == True):
print("True")
else:
print("False")
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for the above approach
using System;
class GFG {
// Function to check weather string
// is prefix
static bool isPrefixString(string s,
string []word)
{
// ans is taken as an empty string
string ans = "";
// N is used to store
// the size of word array
int N = word.Length;
// Iterating over the word array
for (int i = 0; i < N; i++)
{
// Adding element by element
// of the array
ans += word[i];
// If ans and str are same
// return true
if (ans.Equals(s))
return true;
}
// As iteration is ending which means
// string is not prefix so return false.
return false;
}
// Driver code
public static void Main ()
{
string str = "indiaismycountry";
string []word
= { "india", "is", "my",
"country", "and", "i",
"love", "india" };
bool ans = isPrefixString(str, word);
if (ans)
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
True
时间复杂度: O(N),N是字数组的大小。
空间复杂度: O(M) 其中 M 是 str 的长度