Skip to main content

Similar expressions

Given two expressions in the form of strings. The task is to compare them and check if they are similar. Expressions consist of lowercase alphabets, '+', '-' and  '( )'.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two lines. And each line contains an expression.
Output
For each test case, print in a new line "YES"  if the expressions are similar else print "NO".
Constraints:
1<=T<=100
3<=|Expression length|<=100
Example:
Input:
2
-(a+b+c)
-a-b-c

a-b-(c-d)
a-b-c-d
Output:
YES
NO

//JAVA CODE
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    static boolean isequal(String s1,String s2){
       return s1.equals(s2);
    }
    static char sign_change(int positive,int negative,int parentheses,char a){
        if(negative >0 && parentheses>0){
            if(a=='-')
                return '+';
            else                return '-';
        }
        else {
            return a;
        }
    }
    static String remove_parenthesis(String s1){
        String s="";
        int positive =0;int negative=0;int parentheses=0;int flag=0;
        for(int i=0;i<s1.length();i++){
            if(s1.charAt(i)=='-' && parentheses==0){
                negative=1;
                if(flag==0)
                    s+=s1.charAt(i);
                flag=1;
            }
            else if(s1.charAt(i)=='+' && parentheses==0){
                positive=1;
                if(flag==0)
                    s+=s1.charAt(i);
                    flag=1;
            }
            else if(Character.isLetter(s1.charAt(i)) && parentheses==0){
                s+=s1.charAt(i);
                flag=0;
                negative=0;positive=0;
            }
            else if(s1.charAt(i)=='(')
                parentheses++;
            else if(s1.charAt(i)==')')
                parentheses--;
            else if(parentheses>0 && s1.charAt(i)=='+'){
                s+=sign_change(positive,negative,parentheses,s1.charAt(i));
            }
            else if(parentheses>0 && s1.charAt(i)=='-'){
                s+=sign_change(positive,negative,parentheses,s1.charAt(i));
            }
            else if(parentheses>0 && Character.isLetter(s1.charAt(i))){
                s+=s1.charAt(i);
            }
        }
        return s;
    }
    static String checking(String k){
        int flag=0; int count=0; String f="";
        char[]c= k.toCharArray();
        ArrayList<Character> ml= new ArrayList<Character>();
        for(int i=0;i<c.length;i++)
            ml.add(c[i]);
        for(int i=0;i<ml.size();i++){
            if(Character.isLetter(ml.get(i))) {
                count++;
                flag++;
            }
            else {
                flag= 0;
                count++;
            }
            if(flag==2){
                ml.add(count-1,'+');
                count=0;
                flag=0;
            }
        }
        for(int i=0;i<ml.size();i++)
            f +=ml.get(i);
        return f;
    }
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String s1=sc.next();
       String s2=sc.next();
       String s3=remove_parenthesis(s1);
       String s4=checking(s3);
       if(isequal(s4,s2))
        System.out.println("yes");
       else        System.out.println("no");
    }
}

Comments

Popular posts from this blog

zoho taxi question (3rd Round)

Taxi application Design a Call taxi booking application -There are n number of taxi’s. For simplicity, assume 4. But it should work for any number of taxi’s. -The are 6 points(A,B,C,D,E,F) -All the points are in a straight line, and each point is 15kms away from the adjacent points. -It takes 60 mins to travel from one point to another -Each taxi charges Rs.100 minimum for the first 5 kilometers and Rs.10 for the subsequent kilometers. -For simplicity, time can be entered as absolute time. Eg: 9hrs, 15hrs etc. -All taxi’s are initially stationed at A. -When a customer books a Taxi, a free taxi at that point is allocated -If no free taxi is available at that point, a free taxi at the nearest point is allocated. -If two taxi’s are free at the same point, one with lower earning is allocated -Note that the taxi only charges the customer from the pickup point to the drop point. Not the distance it travels from an adjacent point to pickup the customer. -If no taxi is free a...

Inser 0 after Consecutive (K times) Of 1 is Found

Example: Input: Number of bits: 12 Bits: 1 0 1 1 0 1 1 0 1 1 1 1 Consecutive K: 2 Output: 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 //JAVA CODE import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System. in ); int n = sc.nextInt(); int k=sc.nextInt(); int []a = new int [n]; int temp= 0 ; //Inputs for ( int i= 0 ;i<n;i++) a[i]=sc.nextInt(); //print for ( int i= 0 ;i<n;i++){ System. out .print(a[i]+ " " ); if (a[i]== 1 ){ temp++; if (temp==k) { System. out .print( "0 " ); temp= 0 ; } } else temp= 0 ; } } }

QUESTION You’re given an even number n. If n=4, you have to print the following pattern : 4444 4334 4334 4444 If n=6, then the pattern should be like this : 666666 655556 654456 654456 655556 666666

// JAVA CODE import java.util.*; import java.lang.*; import java.io.*; class zohoquestion{     public static void main(String args[]) {         Scanner sc= new Scanner(System.in);         int n=sc.nextInt();int limit=0;         int s=n;         int a[][]= new int[n][n];         int l=0;int r=n-1;         while(limit<n){             for(int i=l;i<=r;i++){                 for(int j=l;j<=r;j++)                     if(i==l || i==r || j==l || j==r)                     a[i][j]=n;             }             l++;r--;n--;limit++;         }         for(int i=0;i<s;i++){ ...