其他分享
首页 > 其他分享> > 【6094. 公司命名】枚举

【6094. 公司命名】枚举

作者:互联网

import java.util.HashSet;
import java.util.Set;

class Solution {
    public long distinctNames(String[] ideas) {
        int[][] cnt = new int[26][26];
        boolean[][] flag = new boolean[ideas.length][26];
        Set<String> set = new HashSet<>();
        for (String idea : ideas) {
            set.add(idea);
        }

        for (int i = 0; i < ideas.length; i++) {
            int first = ideas[i].charAt(0) - 'a';
            String substr = ideas[i].substring(1);
            for (int j = 0; j < 26; j++) {
                String str = (char) (j + 'a') + substr;

                if (!set.contains(str)) {
                    flag[i][j] = true;
                    cnt[first][j]++;
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < ideas.length; i++) {
            for (int j = 0; j < 26; j++) {
                if( flag[i][j]){
                    int first = ideas[i].charAt(0)-'a';
                    ans += cnt[j][first];
                }
            }
        }
        return ans;
    }
}

 

标签:26,String,int,ideas,++,枚举,命名,6094,first
来源: https://www.cnblogs.com/fishcanfly/p/16368408.html