Programming Geek
Rated 4.1/5 based on 446 reviews

Minimum Shelf : Problem #1 ( Aspirations 2020 )

This is the first problem from Aspirations 2020 (2012) college round. The objective was to count the minimum shelf required.
Download the problem archive here (1a - 1d).

The input file as given in the contest was:

package test.minimumshelf;


public class Shelf {
    public int shelfCount(String packet)
    {
        return 0;
    }
}


My solution in JAVA
package test.minimumshelf;
 
import java.util.ArrayList;
 
public class Shelf {
 
    ArrayList<character> packetList = new ArrayList<character>();
    ArrayList<string> shelfList = new ArrayList<string>();
    ArrayList<character> carList = new ArrayList<character>();
 
    public int shelfCount(String packet) {
        int ret = -1;
       
        boolean flag = false;
        if (packet == null) {
            return ret;
        }
         
        String string = packet.substring(0);
        flag = checkInput(string);
        if (flag == false) {
            return ret;
        } else {
            return processResult(packet);
 
        }
 
    }
 
    public boolean checkInput(String str) {
        boolean flag = false;
        if (str == null) {
            return flag;
        } else if (str.equals("")) {
            return flag;
        } else {
            str = str.replaceAll("[ABCDEFGHIJKLMENOPQRSTUVWXYZ]", "");
            str.trim();
            if (str.length() > 0) {
                return flag;
            } else {
                flag = true;
            }
 
        }
        return flag;
 
    }
 
    private int processResult(String str) {
        for (int i = str.length() - 1; i >= 0; i--) {
            packetList.add(str.charAt(i));
        }
        for (int i = 65; i <= 90; i++) {
            carList.add((char) i);
        }
        // System.out.println("packetlist : "+packetList.toString());
        return loadCars();
 
    }
 
    private int loadCars() {
        int count = 1;
 
        while (carList.size() > 0) {
            char car = carList.get(0);
            for (int i = 0; i < shelfList.size(); i++) {
                String str = shelfList.get(i);
                if (str != null && str.matches("(.*)" + car + "(.*)")) {
                    str = "";
 
                }
            }
            int size = packetList.size();
            for (int i = 0; i < size; i++) {
 
                char packet = packetList.get(i);
 
                 
                if (packetList.contains(car)) {
                     
                    if (car == packet) {
                        packetList.remove(i);
                        i--;
                        size--;
                    } else {
                        boolean flag = true;
                        for (int j = 0; j < shelfList.size(); j++) {
 
                            String str = shelfList.get(j);
                            if (str != null && str.matches("(.*)" + packet + "(.*)")) {
                                str += packet;
                                flag = false;
 
                                packetList.remove(0);
                                i--;
                                size--;
 
                            } else if (str.equals("") || str.length() == 0) {
 
                                str += packet;
                                flag = false;
                                packetList.remove(i);
                                i--;
                                size--;
                            }
 
                        }
                        if (flag) {
                             
                            String str = packet + "";
                            str.trim();
                            count++;
                            shelfList.add(str);
                            packetList.remove(i);
                            i--;
                            size--;
                        }
 
                    }
                } else {
                    break;
                }
 
            }
            carList.remove(0);
             
        }
        return count;
    }
 
    public static void main(String[] args) {
        Shelf shelf = new Shelf();
        //  System.out.println("shelf count : "+ shelf.shelfCount("ZYXZYXZYXZYX"));
        System.out.println("shelf count : " + shelf.shelfCount("CCCCCABBBBBBBBBAAAAAAVAAAAACCCCCCDDDDDDDDDDD"));
 
    }
}