检查是否可以使用给定的单词字符串两个字符
给定一个由两个字符组成的字符串和由两个字符组成的 n 个不同单词。任务是找出是否有可能以这样一种方式排列给定的单词,即连接的字符串将给定的两个字符作为子字符串。我们可以多次附加一个单词。
例子:
Input : str = "ya"
words[] = {"ah", "oy", "to", "ha"}
Output : YES
We can join "oy" and then "ah", and
then "ha" to form the string "oyahha"
which contains the string "ya".
So, the answer is "YES"
Input : str[] = "ha"
words[] = "ah"
Output :YES
The string "ahah" contains "ha"
as a substring.
Input : str = "hp"
words[] = {"ht", "tp"|
Output :NO
We can't produce a string containing
"hp" as a sub-string. Note that we
can join "ht" and then "tp" producing
"http", but it doesn't contain the
"hp" as a sub-string.
如果我们仔细查看给定的示例,我们可以看到,如果以下任何条件为真,我们的答案将是“是”,
- str 等于 N 个单词中的任何一个
- str 等于任何单词的反转。
- str 的第一个字母等于任何给定 N 个字符串的最后一个字母,最后一个字母等于任何给定 N 个字符串的第一个字母。
否则我们的输出将永远是 NO。
下面是上述方法的实现。
C++
// CPP code to check if a two character string can
// be made using given strings
#include
using namespace std;
// Function to check if str can be made using
// given words
bool makeAndCheckString(vector words, string str)
{
int n = words.size();
bool first = false, second = false;
for (int i = 0; i < n; i++) {
// If str itself is present
if (words[i] == str)
return true;
// Match first character of str
// with second of word and vice versa
if (str[0] == words[i][1])
first = true;
if (str[1] == words[i][0])
second = true;
// If both characters found.
if (first && second)
return true;
}
return false;
}
// Driver Code
int main()
{
string str = "ya";
vector words = { "ah", "oy", "to", "ha"};
if (makeAndCheckString(words, str))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java code to check if a two character string can
// be made using given strings
import java.util.*;
class GFG
{
// Function to check if str can be made using
// given words
static boolean makeAndCheckString(Vector words,
String str)
{
int n = words.size();
boolean first = false, second = false;
for (int i = 0; i < n; i++)
{
// If str itself is present
if (words.get(i) == str)
return true;
// Match first character of str
// with second of word and vice versa
if (str.charAt(0) == words.get(i).charAt(1))
first = true;
if (str.charAt(1) == words.get(i).charAt(0))
second = true;
// If both characters found.
if (first && second)
return true;
}
return false;
}
// Driver Code
public static void main(String[] args)
{
String str = "ya";
String[] array = { "ah", "oy", "to", "ha"};
Vector words = new Vector(Arrays.asList(array));
if (makeAndCheckString(words, str))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 code to check if a two character string can
# be made using given strings
# Function to check if str can be made using
# given words
def makeAndCheckString(words, str):
n = len(words)
first = second = False
for i in range(n):
# If str itself is present
if words[i]==str:
return True
# Match first character of str
# with second of word and vice versa
if str[0] == words[i][1]:
first = True
if str[1] == words[i][0]:
second = True
# If both characters found.
if first and second:
return True
return False
# Driver Code
str = 'ya'
words = ['ah', 'oy', 'to', 'ha']
if makeAndCheckString(words, str):
print('YES')
else:
print('NO')
# This code is contributed
# by SamyuktaSHegde
C#
// C# code to check if a two character string can
// be made using given strings
using System;
using System.Collections.Generic;
class GFG
{
// Function to check if str can be made using
// given words
static bool makeAndCheckString(List words,
String str)
{
int n = words.Count;
bool first = false, second = false;
for (int i = 0; i < n; i++)
{
// If str itself is present
if (words[i] == str)
return true;
// Match first character of str
// with second of word and vice versa
if (str[0] == words[i][1])
first = true;
if (str[1] == words[i][0])
second = true;
// If both characters found.
if (first && second)
return true;
}
return false;
}
// Driver Code
public static void Main(String[] args)
{
String str = "ya";
String[] array = { "ah", "oy", "to", "ha"};
List words = new List(array);
if (makeAndCheckString(words, str))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Princi Singh
PHP
Javascript
输出:
YES