Check if a pair of strings exists that starts with and without the character K or not
Last Updated :
25 Jan, 2022
Given an array arr[] consisting of N strings of lowercase characters and a character K such that any string may start with the character K, the task is to check if there exists any pair of strings that are starting and not starting (‘!’) with the character K. If found to be true, then print “Yes“. Otherwise, print “No“.
Examples:
Input: arr[] = {“a”, “!a”, “b”, “!c”, “d”, “!d”}, K = ‘!’
Output: Yes
Explanation:
There exists valid pairs of the strings are {(“a”, “!a”), (“!d”, “d”)}.
Input: arr[] = {“red”, “red”, “red”, “!orange”, “yellow”, “!blue”, “cyan”, “!green”, “brown”, “!gray”}, K = ‘!’
Output: No
Naive Approach: The simplest approach to solve the given problem is to find all possible pairs from the array and check if the strings pair satisfy the given condition or not.
Time Complexity: O(N2*M), where M is the maximum length of the string in the given array arr[].
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be solved by using dictionary. Follow the steps below to solve the problem:
- Initialize a dictionary, say, visited to store the previously visited strings.
- Iterate over the list arr[] and in each iteration, if the starting character of the current string is the character K then check for string without the character K in visited otherwise, check for the string with the character K in visited. If the string is found then return “Yes“.
- In each iteration, add the string S into the map visited.
- After completing the above steps, print “No” if the above conditions are not satisfied.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string checkhappy(vector<string> arr, char K, int N)
{
set<string> visited;
for (string s : arr) {
if (s[0] == K)
if (visited.find(s.substr(1)) != visited.end())
return "Yes" ;
else
if (visited.find((K + s)) != visited.end())
return "Yes" ;
visited.insert(s);
}
return "No" ;
}
int main() {
vector<string> arr = { "a" , "! a" , "b" , "! c" , "d" , "! d" };
char K = '!' ;
int N = arr.size();
cout << checkhappy(arr, K, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static String checkhappy(String[] arr, char K, int N)
{
HashSet<String> visited = new HashSet<String> ();
for (String s : arr) {
if (s.charAt( 0 ) == K)
if (visited.contains(s.substring( 1 )))
return "Yes" ;
else
if (visited.contains((K + s)))
return "Yes" ;
visited.add(s);
}
return "No" ;
}
public static void main(String[] args) {
String[] arr = { "a" , "! a" , "b" , "! c" , "d" , "! d" };
char K = '!' ;
int N = arr.length;
System.out.print(checkhappy(arr, K, N) + "\n" );
}
}
|
Python3
def checkhappy(arr, K, N):
visited = set ()
for s in arr:
if (s[ 0 ] = = K):
if s[ 1 :] in visited:
return 'Yes'
else :
if (K + s) in visited:
return 'Yes'
visited.add(s)
return "No"
if __name__ = = '__main__' :
arr = [ 'a' , '! a' , 'b' , '! c' , 'd' , '! d' ]
K = '!'
N = len (arr)
print (checkhappy(arr, K, N))
|
Javascript
<script>
function checkhappy(arr, K, N)
{
let visited = new Set();
for (let s of arr)
{
if (s[0] == K)
{
if (visited.has(s.slice(1)))
return "Yes" ;
}
else
{
if (visited.has(K + s))
return "Yes" ;
}
visited.add(s);
}
return "No" ;
}
let arr = [ "a" , "! a" , "b" , "! c" ,
"d" , "! d" ];
let K = "!" ;
let N = arr.length;
document.write(checkhappy(arr, K, N));
</script>
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static String checkhappy(String[] arr, char K, int N)
{
HashSet<String> visited = new HashSet<String> ();
foreach (String s in arr) {
if (s[0] == K)
if (visited.Contains(s.Substring(1)))
return "Yes" ;
else
if (visited.Contains((K + s)))
return "Yes" ;
visited.Add(s);
}
return "No" ;
}
public static void Main(String[] args) {
String[] arr = { "a" , "! a" , "b" , "! c" , "d" , "! d" };
char K = '!' ;
int N = arr.Length;
Console.Write(checkhappy(arr, K, N) + "\n" );
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...