Page tree
Skip to end of metadata
Go to start of metadata
10'th Weeks Test
package works.tripod;

public class IsBeautifulString {

    static final Object[][] testData = {
            {"bbbaacdafe", true}
            , {"aabbb", false}
            , {"bbc", false}
            , {"bbbaa", false}
            , {"abcdefghijklmnopqrstuvwxyzz", false}
            , {"abcdefghijklmnopqrstuvwxyz", true}
            , {"abcdefghijklmnopqrstuvwxyzqwertuiopasdfghjklxcvbnm", true}
            , {"fyudhrygiuhdfeis", false}
            , {"zaa", false}
            , {"zyy", false}
    };

    public static void main(String[] args) {
        IsBeautifulString instance = new IsBeautifulString();
        for(Object[] testDataItem : testData ){
            String inputString = (String) testDataItem[0];
            boolean expect = (boolean) testDataItem[1];
            System.out.println(inputString + " - " + instance.isBeautifulString(inputString) + " / expect :: " +expect);
        }
    }

    boolean isBeautifulString(String inputString) {
        int[] counts = new int['z' - 'a' + 1]; // a to z  26 count slot.

        // fill zero
        for (int countIdx = 0; countIdx < counts.length; countIdx++) {
            counts[countIdx] = 0;
        }

        for (int idx = 0; idx < inputString.length(); idx++) {
            counts[inputString.charAt(idx) - 'a'] += 1;
        }

        for (int countIdx = 1; countIdx < counts.length; countIdx++) {
            if(counts[countIdx-1] < counts[countIdx]){
                return false;
            }
        }
        return true;
    }
}
  • No labels