# Check if a two character string can be made using given words

Given a string of two characters and n distinct words of two characters. The task is to find if it is possible to arrange given words in such a way that the concatenated string has the given two character string as a substring. We can append a word multiple times.

Examples:

```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".

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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

If we look at the given examples carefully, we can see that our answer will be “YES” if any of the following conditions is true,

1. str is equal to any one of the N words
2. str is equal to reverse of any of the words.
3. It first letter of str is equal to last letter of any of the given N strings and last letter is equal to the first letter of any of the given N strings.

Otherwise our output will always be NO.

Below is the implementation of the above approach.

## 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 == words[i])  ` `            ``first = ``true``;             ` `        ``if` `(str == words[i])  ` `            ``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 == words[i])  ` `            ``first = ``true``;          ` `        ``if` `(str == words[i])  ` `            ``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

 ` `

Output:

```YES
```

This article is contributed by Sarthak Kohli. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.