XYZ String

An XYZ string is a string that starts with XYZ and ends with XYZ.

You wil be given a string check whether given string can be converted into XYZ or not by shuffling the give string.

If we can get XYZ string by reshuffling print "YES" else Print "NO". 

Example:

Input:  xhzzyhxyp
Output: YES

Approach

Java

public class XYZString {
    public static void main(String args[]) throws Exception {

        char[] input = "xhzzyhxyp".toLowerCase().toCharArray();
        int c_x = 0;
        int c_y = 0;
        int c_z = 0;
        for (int i = 0; i < input.length; i++) {
            if (input[i] == 'x')
                c_x++;
            if (input[i] == 'y')
                c_y++;
            if (input[i] == 'z')
                c_z++;
        }
        if (c_x > 1 && c_y > 1 && c_z > 1)
            System.out.println("YES");
        else
            System.out.println("NO");
    }

}


No comments:

Post a Comment