Open In App

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

Last Updated : 20 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

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++





Java





Python3





C#





PHP





Javascript





Output

Yes

Time Complexity: O(n), where n represents the size of the given vector.
Auxiliary Space: O(1), no extra space is required, so it is a constant.



Similar Reads

Check if the given string of words can be formed from words present in the dictionary
Given a string array of M words and a dictionary of N words. The task is to check if the given string of words can be formed from words present in the dictionary. Examples: dict[] = { find, a, geeks, all, for, on, geeks, answers, inter } Input: str[] = { "find", "all", "answers", "on", "geeks", "for", "geeks" }; Output: YES all words of str[] are p
17 min read
Python Program to check if two sentences can be made the same by rearranging the words
Given two strings S1 and S2 representing two sentences, the task is to check if two sentences can be made same by rearranging the words of any of the strings. Examples: Input: S1 = "please select a category", S2 = "category a please select"Output: Yes Input: S1 = "hello world this is python language", S2 = "python language is this modern world" Out
5 min read
Number of words that can be made using exactly P consonants and Q vowels from the given string
Given a string str and two integers P and Q. The task is to find the total count of words that can be formed by choosing exactly P consonants and Q vowels from the given string.Examples: Input: str = "geek", P = 1, Q = 1 Output: 8 "ge", "ge", "eg", "ek", "eg", "ek", "ke" and "ke" are the possible words.Input: str = "crackathon", P = 4, Q = 3 Output
7 min read
Check if given string can be made Palindrome by removing only single type of character
Given a string S, the task is to whether a string can be made palindrome after removing the occurrences of the same character, any number of times Examples: Input: S = "abczdzacb" Output: Yes Explanation: Remove first and second occurrence of character 'a', string S becomes "bczdzcb", which is a palindrome . Input: S = "madem"Output: No Approach: T
7 min read
Check if given string can be made Palindrome by removing only single type of character | Set-2
Given a string S, the task is to whether a string can be made palindrome after removing the occurrences of the same character, any number of times Examples: Input: S = "abczdzacb"Output: YesExplanation: Remove first and second occurrence of character ‘a’. String S becomes “bczdzcb”, which is a palindrome. Input: S = "madem"Output: NoExplanation: Si
9 min read
Check if two strings can be made equal by swapping one character among each other
Given two strings A and B of length N, the task is to check whether the two strings can be made equal by swapping any character of A with any other character of B only once.Examples: Input: A = "SEEKSFORGEEKS", B = "GEEKSFORGEEKG" Output: Yes "SEEKSFORGEEKS" and "GEEKSFORGEEKG" After removing the elements which are the same and have the same index
10 min read
Check if two non-duplicate strings can be made equal after at most two swaps in one string
Given two strings A and B consisting of unique lowercase letters, the task is to check if both these strings can be made equal by using at most two swaps. Print Yes if they can. Otherwise, print No. Example: Input: A="abcd", B="badc"Output: YesFirst swap a and b, and then swap c and d, in string A.Input: A="abcd", B="cbda"Output: No Approach: In th
17 min read
Check if given Binary String can be made Palindrome using K flips
Given a binary string str, the task is to determine if string str can be converted into a palindrome in K moves. In one move any one bit can be flipped i.e. 0 to 1 or 1 to 0. Examples: Input: str = "101100", K = 1Output: YESExplanation: Flip last bit of str from 0 to 1. Input: str = "0101101", K = 2Output: NOExplanation: Three moves required to mak
5 min read
Count N-length arrays made from first M natural numbers whose subarrays can be made palindromic by replacing less than half of its elements
Given two integer N and M, the task is to find the count of arrays of size N with elements from the range [1, M] in which all subarrays of length greater than 1 can be made palindromic by replacing less than half of its elements i.e., floor(length/2). Examples: Input: N = 2, M = 3Output: 6Explanation:There are 9 arrays possible of length 2 using va
4 min read
Check if given strings can be made same by swapping two characters of same or different strings
Given an array of equal-length strings, arr[] of size N, the task is to check if all the strings can be made equal by repeatedly swapping any pair of characters of same or different strings from the given array. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: arr[] = { "acbdd", "abcee" } Output: YES Explanation: Swapp
11 min read
Article Tags :
Practice Tags :