String字符串的完美度

题目详情:

我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同,

而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度是一样的。

 

现在给定一个字符串,输出它的最大可能的完美度。

例如:dad,你可以将26分配给d,25分配给a,这样整个字符串最大可能的完美度为77。

import java.util.*;
/**
 * @author xueji
 */
public class Main {
    /**
     * @param s
     * @return
     */
    public static int perfect(String s) {
        String str = s;
        // 字符的匹配值
        int x = 26;
        // 返回值
        int j = 0;
        while (!"".equals(str)) {
            int num = 0;
            String temp = str;
            str = str.replaceAll(str.substring(0, 1), "");
            num = temp.length() - str.length();
            j = j + x * num;
            x--;
        }
        return j;
    }
    //test
    public static void main(String args[]) {
        System.out.println(perfect("ddzzzz"));
    }
}

 

posted @ 2013-07-13 14:21  storm king  阅读(285)  评论(0编辑  收藏  举报