Concepts Used
Strings
Difficulty Level
Medium
Problem Statement (Simplified):
Given two string
SandTcontaining only-and+. Two-together can form a single+. If it is possible to obtainTfrom ‘S’ then printYESelseNO.
Test Case
Input:
4
-+--+
-+++
--------
-+--+-
--
---
+++
+++
Output:
YES
YES
NO
YES
Explanation:
Case-1:
Here we can see if we convert '--' starting at index 2 to '+', string S becomes string T. So YES string S can become string T.
Case-2:
Here we can see, if we convert '--' starting at index 1 to '+', also a string '--' starting at index 5 is converted to '+', string S becomes string T.So YES string S can become string T.
Case-3:
Here we can see if we convert '--' starting at index 2 to '+', string S becomes string T.
Case-4:
Here we can see, String S is smaller than string T. So it can never become string T.
Solving Approach :
- If the length of
Tis larger thanS, it is impossible to obtainTfromS. So we set flag value toFalse. - Otherwise, we iterate over both strings, during iteration we can get three cases :
- If both
SandThave the same characters at the current position, we continue the iteration. - If
Scontains+andTcontains-at the same positions, that means we cannot achieveTfromS, so we end iterations here and set flag value toFalse. - If
Scontains-andTcontains+at the same position, we check if next position contains-or not inS, if yes we move forward and increase theSposition by 1 as both-count as a single+, otherwise we get out of iterations and set flag value toFalse.
- If both
- After we have iterated, we check if the final position of
SandTare equal or not. If the iteration was completed and both ended at the same point that meansTcan be achieved bySso we set flag valueTrue. - We finally check the flag value, if it is
Falsewe print "YES", else "No".
Example
- Lets assume String
Sto be'--------'and stringTto be'-+--+-'. - Here
Sis larger in length thanT, so we can move further. - If we dry run the approach given above, we can check if they can become same or not. We start from starting point of both strings, using two pointers,
i=0andj=0. Now, we iterate both strings one by one.
Step-1:
Here, we see both characters at both indexes i and j are '-', so we move further.
Step-2:
Here, we see character at index i is '-' and character at index j is '+', so we check if next character to i is '-' or not. In this case, it is '-', so we can convert these two '-' to a '+'. We move index i by one more step too because two '-' are consumed to form a '+'.
Step-3:
Here, we see both characters at both indexes i and j are '-', so we move further.
Step-4:
Here, we see both characters at both indexes i and j are '-', so we move further.
Step-5:
Here, we see character at index i is '-' and character at index j is '+', so we check if next character to i is '-' or not. In this case, it is '-', so we can convert these two '-' to a '+'. We move index i by one more step too because two '-' are consumed to form a '+'.
Step-6:
Here, we see both characters at both indexes i and j are '-', so we move further.
- After
step-6, both of our strings are traversed, soYES, stringScan become stringT.
Solutions
#include <stdio.h>
int main()
{
int test;
scanf("%d",&test);
while(test--){
char s[10000001],t[10000001];
scanf("%s%s",s,t);
int diff = 0;
int flag = 0;
if((strlen(s)==0 && strlen(t)==0) || (strlen(s) < strlen(t)))
flag = 0;
else{
for(int i=0; (i-diff)<strlen(t); i++){
if(t[i-diff] == '+' && s[i] == '-' && s[i+1] == '-'){
i++;
diff++;
}else if((t[i-diff] == '+' && s[i] == '+') || (t[i-diff] == '-' && s[i] == '-') ){
flag = 0;
}
else{
flag = 1;
break;
}
}
}
if((strlen(s)-diff) != strlen(t)){
flag = 1;
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int test;
cin>>test;
while(test--){
string s,t;
cin>>s>>t;
int diff = 0;
bool flag = false;
if((s.length()==0 && t.length()==0) || (s.length() < t.length()))
flag = false;
else{
for(int i=0; (i-diff)<t.length(); i++){
if(t[i-diff] == '+' && s[i] == '-' && s[i+1] == '-'){
i++;
diff++;
}else if((t[i-diff] == '+' && s[i] == '+') || (t[i-diff] == '-' && s[i] == '-') ){
flag = false;
}
else{
flag = true;
break;
}
}
}
if((s.length()-diff) != t.length()){
flag = true;
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
Scanner sc= new Scanner(System.in);
int test_cases;
test_cases = sc.nextInt();
while(test_cases != 0){
String s = sc.next();
String t = sc.next();
int diff = 0;
int flag = 0;
if((s.length()==0 && t.length()==0) || (s.length() < t.length())){
flag = 0;
}
else{
for(int i=0; (i-diff)<t.length(); i++){
if(t.charAt(i-diff) == '+' && s.charAt(i) == '-'&& s.charAt(i+1) == '-' ){
i++;
diff++;
}else if((t.charAt(i-diff) == '+' && s.charAt(i) == '+') || (t.charAt(i-diff) == '-' && s.charAt(i) == '-') ){
flag = 0;
}
else{
flag = 1;
break;
}
}
}
if((s.length()-diff) != t.length()){
flag = 1;
}
if(flag == 1)
System.out.println("NO");
else
System.out.println("YES");
test_cases--;
}
}
}
for _ in range(int(input())):
s = input()
t = input()
diff = 0
flag = False
if len(s) == 0 and len(t) == 0 or len(s) < len(t):
flag = False
else:
for i in range(len(t) + diff):
if t[i - diff] == "+" and s[i] == "-" and s[i] == "-":
i += 1
diff += 1
elif (t[i - diff] == "+" and s[i] == "+") or (t[i - diff] == "-" and s[i] == "-"):
flag = False
else:
flag = True
break
if len(s) - diff != len(t):
flag = True
if flag:
print("NO")
else:
print("YES")
Space Complexity of this approach would be O(1).
This article tried to discuss Strings. Hope this blog helps you understand and solve the problem. To practice more problems on Strings you can check out .






