Thursday, May 19, 2022
HomeSoftware DevelopmentTest if subsequences shaped by given characters are similar for Q queries

Test if subsequences shaped by given characters are similar for Q queries


  

import java.io.*;

import java.util.*;

  

public class SubsequenceQueries {

  

    

    

    public static ArrayList<Boolean>

    isPossible(int N, ArrayList<String> arr, int Q,

               String queries[])

    {

        

        remaining int numElements = 26;

  

        

        

        ArrayList<int[][]> parts

            = new ArrayList<>();

        for (int i = 0; i < N; i++) {

  

            

            

            parts.add(new int[arr.get(i).length()]

                                [numElements]);

            int[][] tmp = parts.get(i);

  

            

            

            for (int j = 0; j < arr.get(i).size();

                 j++) {

                for (int okay = 0; okay < numElements;

                     okay++) {

                    if (j != 0)

                        tmp[j][k] = tmp[j - 1][k];

                }

  

                

                

                tmp[j][arr.get(i).charAt(j) - 97]++;

            }

        }

  

        

        

        

        

        

        

        

        HashMap<Integer, Set<Integer> > requiredRemovals

            = new HashMap<>();

        for (int i = 0; i < numElements; i++)

            requiredRemovals.put(i, new HashSet<Integer>());

  

        

        

        for (int i = 0; i < numElements; i++) {

  

            

            

            

            for (int j = 1; j < N; j++) {

  

                

                

                

                

                String a = arr.get(j - 1);

                String b = arr.get(j);

  

                

                

                int[][] elements1

                    = parts.get(j - 1);

                int[][] elements2

                    = parts.get(j);

  

                int p1 = 0;

                int p2 = 0;

  

                

                

                

                

                

                

                

                

                

                

                if (elements1[a.length() - 1][i]

                    != elements2[b.length() - 1][i]) {

                    for (int key :

                         requiredRemovals.keySet())

                        requiredRemovals.get(key).add(i);

                }

                else {

                    

                    

                    

                    

                    

                    whereas (p1 < a.size()

                           && p2 < b.size()) {

  

                        

                        

                        whereas (p1 < a.size()

                               && a.charAt(p1)

                                          - 97

                                      != i) {

                            p1++;

                        }

  

                        

                        

                        whereas (p2 < b.size()

                               && b.charAt(p2)

                                          - 97

                                      != i) {

                            p2++;

                        }

  

                        

                        

                        

                        if (p1 < a.size()

                            && p2 < b.size()) {

  

                            

                            

                            for (int okay = 0;

                                 okay < numElements;

                                 okay++) {

                                if (elements1[p1][k]

                                    != elements2[p2][k])

                                    requiredRemovals.get(i)

                                        .add(okay);

                            }

                        }

                        p1++;

                        p2++;

                    }

                }

            }

        }

  

        ArrayList<Boolean> res

            = new ArrayList<Boolean>();

  

        

        for (int i = 0; i < Q; i++) {

  

            

            

            Set<Integer> union

                = new HashSet<Integer>();

  

            

            

            

            for (char c : queries[i].toCharArray())

                union.addAll(requiredRemovals.get(c - 97));

            boolean ans = true;

  

            

            

            for (char c : queries[i].toCharArray()) {

                if (union.accommodates(c - 97))

                    ans = false;

            }

  

            res.add(ans);

        }

        return res;

    }

  

    

    public static void primary(String[] args)

        throws IOException

    {

        int N = 3;

        ArrayList<String> arr

            = new ArrayList<String>();

        arr.add("accbad");

        arr.add("abcacd");

        arr.add("cacbda");

        int Q = 6;

        String queries[]

            = new String[6];

        queries[0] = "a";

        queries[1] = "ab";

        queries[2] = "advert";

        queries[3] = "bd";

        queries[4] = "bcd";

        queries[5] = "abd";

  

        

        ArrayList<Boolean> ans

            = isPossible(N, arr, Q, queries);

        for (boolean val : ans)

            System.out.println(val);

    }

}

RELATED ARTICLES

Most Popular

Recent Comments