Pokemon Pikachu

Pikachu is a well-known character in the Pokemon anime series. Pikachu can speak, but only 3 syllables: “pi”, “ka”, and “chu”. Therefore Pikachu can only pronounce strings that can be created as a concatenation of one or more syllables he can pronounce. For example, he can pronounce the words “pikapi” and “pikachu”.
Examples
1)i/p=”pikapi”    Returns: “YES”   = >   “pikapi” = “pi” + “ka” + “pi”

2)i/p=”pipikachu”    Returns: “YES”    =>   “pipikachu” = “pi” + “pi” + “ka” + “chu”

3)i/p=”pikaqiu”      Returns: “NO”    => since ‘q’ does not appear in “pi”, “ka”, or “chu”.

4)i/p=”daynight”     Returns: “NO”

5)i/p=”piika”  Returns: “NO”

public class PikachuEasy {
    public String check(String word) {
        String ans = "YES";
        word = word.replace("pi", " ");
        word = word.replace("ka", " ");
        word = word.replace("chu", " ");
        if((word.trim()).length()>0) ans = "NO";
        System.out.println(ans);

        //simple approach
        //return word.matches("((pi)|(ka)|(chu))*") ? "YES": "NO";
        return ans;
    }

    public static void main(String[] args) {
        PikachuEasy pe = new PikachuEasy();
        pe.check("pikapi");
        pe.check("pipikachu");
        pe.check("pikaqiu");
        pe.check("topcoder");
        pe.check("piika");
        pe.check("chupikachupipichu");
        pe.check("pikapipachu");
        pe.check("pikapi");
        pe.check("pipikachu");
        pe.check("pikaqiu");
        pe.check("piika");
        pe.check("chupikachupipichu");
        pe.check("chuchuchuchupichuchupichuchucauchuchu");
        pe.check("chukakakapipipi");
        pe.check("kakachukachupichupikakapichukapichuka");
    }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: