zceolrj / Community

a demo for nodejs and angularjs
0 stars 0 forks source link

wordgame #3

Open zceolrj opened 8 years ago

zceolrj commented 8 years ago

`public class WordGame { public static void main(String[] args) { String str = "xfircvscxggbwkf";

    int result = who(str);

    System.out.println(result);
}

public static int who(String str)
{
    Map<String, Integer> map = new HashMap<String, Integer>();

    int result = check(str, map);

    return result;
}

public static int check(String str, Map<String, Integer> map)
{
    if(str.length() == 2)
    {
        return 1;
    }

    if(map.containsKey(str))
    {
        return map.get(str);
    }

    if(str.length() == 3)
    {
        char c0 = str.charAt(0);
        char c1 = str.charAt(1);
        char c2 = str.charAt(2);

        if(c0 >= c1 && c1 >= c2)
        {
            map.put(str, 0);

            return 0;
        }
        else
        {
            map.put(str, 1);

            return 1;
        }
    }

    for(int i=0;i<str.length();i++)
    {
        String newStr = str.substring(0, i) + str.substring(i + 1);

        if(judge(newStr))
        {
            map.put(newStr, 1);

            return 1;
        }
    }

    for(int i=0;i<str.length();i++)
    {
        String newStr = str.substring(0, i) + str.substring(i + 1);

        int result = check(newStr, map);

        if(result == 0)
        {
            map.put(str, 1);

            return 1;
        }
    }

    map.put(str, 0);

    return 0;
}

public static boolean judge(String str)
{
    for(int i=1;i<str.length();i++)
    {
        char cur = str.charAt(i);
        char pre = str.charAt(i - 1);

        if(pre >= cur)
        {
            return false;
        }
    }

    return true;
}

}`