Check if given Strings can be made equal by inserting at most 1 String
Last Updated :
20 Dec, 2023
Given two sentences S1 and S2, the task is to check if the sentences can be made equal by inserting at most one sentence(possibly, empty) into any one of the two sentences.
Examples:
Input : S1 = “Start practicing on GeeksforGeeks”, S2 =”Start GeeksforGeeks”
Output :
True
Explanation: “practicing on” can be inserted between “Start” and “GeeksforGeeks” in s2 to make it equal to S1.
Input: S1= “New Delhi is capital of INDIA”, S2 = “is capital of”
Output:
False
Approach: The following observations help in solving the problem:
- If the sizes of two sentences are equal, but they themselves are not the same, they cannot be made equal.
- After the longest common prefix and the longest common suffix of the two sentences are removed, if at least one of them is empty, it means that they can be made equal.
The problem can be solved with the help of deques. Follow the steps below to solve the problem:
- Check if the size of S1 and S2 are equal. If they are equal, do the following:
- If S1 is equal to S2, return true.
- Otherwise, return false.
- Initialize two deques X and Y.
- Push all words of S1 into X.
- Push all words of S2 into Y.
- While the fronts of X and Y are the same, pop from the fronts of both X and Y.
- While the backs of X and Y are the same, pop from the backs of both X and Y.
- Check if any one of X or Y is empty. If any of them is empty, return true.
- Otherwise, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool areSimilar(string S1, string S2)
{
int N = S1.size();
int M = S2.size();
if (N == M) {
if (S1 == S2)
return true ;
return false ;
}
deque<string> X, Y;
S1.push_back( ' ' );
S2.push_back( ' ' );
string temp = "" ;
for ( int i = 0; i < N + 1; i++) {
if (S1[i] == ' ' ) {
X.push_back(temp);
temp = "" ;
}
else {
temp += S1[i];
}
}
for ( int i = 0; i < M + 1; i++) {
if (S2[i] == ' ' ) {
Y.push_back(temp);
temp = "" ;
}
else {
temp += S2[i];
}
}
while (X.size() > 0 && Y.size() > 0
&& X.front() == Y.front()) {
X.pop_front();
Y.pop_front();
}
while (X.size() > 0 && Y.size() > 0
&& X.back() == Y.back()) {
X.pop_back();
Y.pop_back();
}
if (X.size() == 0 || Y.size() == 0)
return true ;
return false ;
}
int main()
{
string S1 = "Start practicing on GeeksforGeeks" ;
string S2 = "Start GeeksforGeeks" ;
if (areSimilar(S1, S2))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean areSimilar(String S1, String S2) {
int N = S1.length();
int M = S2.length();
if (N == M) {
if (S1.equals(S2))
return true ;
return false ;
}
Deque<String> X = new LinkedList<>();
Deque<String> Y = new LinkedList<>();
S1 += " " ;
S2 += " " ;
String temp = "" ;
for ( int i = 0 ; i < N + 1 ; i++) {
if (S1.charAt(i) == ' ' ) {
X.addLast(temp);
temp = "" ;
} else {
temp += S1.charAt(i);
}
}
for ( int i = 0 ; i < M + 1 ; i++) {
if (S2.charAt(i) == ' ' ) {
Y.addLast(temp);
temp = "" ;
} else {
temp += S2.charAt(i);
}
}
while (!X.isEmpty() && !Y.isEmpty()
&& X.peekFirst().equals(Y.peekFirst())) {
X.removeFirst();
Y.removeFirst();
}
while (!X.isEmpty() && !Y.isEmpty()
&& X.peekLast().equals(Y.peekLast())) {
X.removeLast();
Y.removeLast();
}
if (X.isEmpty() || Y.isEmpty())
return true ;
return false ;
}
public static void main(String[] args) {
String S1 = "Start practicing on GeeksforGeeks" ;
String S2 = "Start GeeksforGeeks" ;
if (areSimilar(S1, S2))
System.out.println( "True" );
else
System.out.println( "False" );
}
}
|
Python3
from collections import deque
def areSimilar(S1, S2):
S1 = [i for i in S1]
S2 = [i for i in S2]
N = len (S1)
M = len (S2)
if (N = = M):
if (S1 = = S2):
return True
return False
X, Y = deque(), deque()
S1.append( ' ' )
S2.append( ' ' )
temp = ""
for i in range (N + 1 ):
if (S1[i] = = ' ' ):
X.append(temp)
temp = ""
else :
temp + = S1[i]
for i in range (M + 1 ):
if (S2[i] = = ' ' ):
Y.append(temp)
temp = ""
else :
temp + = S2[i]
while ( len (X) > 0 and
len (Y) > 0 and X[ 0 ] = = Y[ 0 ]):
X.popleft()
Y.popleft()
while ( len (X) > 0 and len (Y) > 0 and
X[ - 1 ] = = Y[ - 1 ]):
X.pop()
Y.pop()
if ( len (X) = = 0 or len (Y) = = 0 ):
return True
return False
if __name__ = = '__main__' :
S1 = "Start practicing on GeeksforGeeks"
S2 = "Start GeeksforGeeks"
if (areSimilar(S1, S2)):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
using System.Collections.Generic;
class Program
{
static bool AreSimilar( string S1, string S2)
{
int N = S1.Length;
int M = S2.Length;
if (N == M)
{
if (S1 == S2)
return true ;
return false ;
}
Queue< string > X = new Queue< string >();
Queue< string > Y = new Queue< string >();
S1 += ' ' ;
S2 += ' ' ;
string temp = "" ;
for ( int i = 0; i < N + 1; i++)
{
if (S1[i] == ' ' )
{
X.Enqueue(temp);
temp = "" ;
}
else
{
temp += S1[i];
}
}
for ( int i = 0; i < M + 1; i++)
{
if (S2[i] == ' ' )
{
Y.Enqueue(temp);
temp = "" ;
}
else
{
temp += S2[i];
}
}
while (X.Count > 0 && Y.Count > 0
&& X.Peek() == Y.Peek())
{
X.Dequeue();
Y.Dequeue();
}
while (X.Count > 0 && Y.Count > 0
&& X.ToArray()[X.Count - 1] == Y.ToArray()[Y.Count - 1])
{
X.Dequeue();
Y.Dequeue();
}
if (X.Count == 0 || Y.Count == 0)
return true ;
return false ;
}
static void Main( string [] args)
{
string S1 = "Start practicing on GeeksforGeeks" ;
string S2 = "Start GeeksforGeeks" ;
if (AreSimilar(S1, S2))
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
Console.ReadLine();
}
}
|
Javascript
function areSimilar(S1, S2) {
S1 = S1.split( '' );
S2 = S2.split( '' );
const N = S1.length;
const M = S2.length;
if (N === M) {
if (S1.join( '' ) === S2.join( '' )) {
return true ;
}
return false ;
}
const X = [],
Y = [];
S1.push( ' ' );
S2.push( ' ' );
let temp = "" ;
for (let i = 0; i < N + 1; i++) {
if (S1[i] === ' ' ) {
X.push(temp);
temp = "" ;
} else {
temp += S1[i];
}
}
for (let i = 0; i < M + 1; i++) {
if (S2[i] === ' ' ) {
Y.push(temp);
temp = "" ;
} else {
temp += S2[i];
}
}
while (X.length > 0 && Y.length > 0 && X[0] === Y[0]) {
X.shift();
Y.shift();
}
while (X.length > 0 && Y.length > 0 && X[X.length - 1] === Y[Y.length - 1]) {
X.pop();
Y.pop();
}
if (X.length === 0 || Y.length === 0) {
return true ;
}
return false ;
}
const S1 = "Start practicing on GeeksforGeeks" ;
const S2 = "Start GeeksforGeeks" ;
if (areSimilar(S1, S2)) {
console.log( "True" );
} else {
console.log( "False" );
}
|
Time Complexity: O(N+M), where N and M are the sizes of S1 and S2 respectively.
Auxiliary Space: O(N+M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...