第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

n 個(gè)字符串中的常用字符

n 個(gè)字符串中的常用字符

慕慕森 2022-06-15 10:12:48
我正在嘗試制作一個(gè)函數(shù)來(lái)打印給定 n 個(gè)字符串中常見(jiàn)的字符數(shù)。(注意字符可以多次使用)我正在努力對(duì) n 個(gè)字符串執(zhí)行此操作但是我為 2 個(gè)字符串執(zhí)行了此操作,而沒(méi)有任何字符重復(fù)多次。我已經(jīng)發(fā)布了我的代碼。public class CommonChars {    public static void main(String[] args) {        String str1 = "abcd";        String str2 = "bcde";        StringBuffer sb = new StringBuffer();        // get unique chars from both the strings        str1 = uniqueChar(str1);        str2 = uniqueChar(str2);        int count = 0;        int str1Len = str1.length();        int str2Len = str2.length();        for (int i = 0; i < str1Len; i++) {            for (int j = 0; j < str2Len; j++) {                // found match stop the loop                if (str1.charAt(i) == str2.charAt(j)) {                    count++;                    sb.append(str1.charAt(i));                    break;                }            }        }           System.out.println("Common Chars Count : " + count + "\nCommon Chars :" +         sb.toString());    }    public static String uniqueChar(String inputString) {        String outputstr="",temp="";        for(int i=0;i<inputstr.length();i++) {            if(temp.indexOf(inputstr.charAt(i))<0) {                temp+=inputstr.charAt(i);            }        }        System.out.println("completed");        return temp;    }}3 abcaabcbdbgc3他們可能有機(jī)會(huì)在一個(gè)字符串中多次出現(xiàn)相同的字符,并且您不應(yīng)該消除這些字符而是檢查否。他們?cè)谄渌址兄貜?fù)的次數(shù)。例如 3 abacd aaxyz aatre輸出應(yīng)該是 2如果我在java中得到解決方案會(huì)更好
查看完整描述

4 回答

?
天涯盡頭無(wú)女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

您必須將所有Strings 轉(zhuǎn)換為Setof Characters 并保留第一個(gè)中的所有內(nèi)容。下面的解決方案有很多可以優(yōu)化的地方,但你應(yīng)該理解一般的想法。


import java.util.Arrays;

import java.util.Collection;

import java.util.Collections;

import java.util.HashSet;

import java.util.List;

import java.util.Set;


public class Main {


    public static void main(String[] args) {


        List<String> input = Arrays.asList("jonas", "ton", "bonny");


        System.out.println(findCommonCharsFor(input));

    }


    public static Collection<Character> findCommonCharsFor(List<String> strings) {

        if (strings == null || strings.isEmpty()) {

            return Collections.emptyList();

        }


        Set<Character> commonChars = convertStringToSetOfChars(strings.get(0));

        strings.stream().skip(1).forEach(s -> commonChars.retainAll(convertStringToSetOfChars(s)));


        return commonChars;

    }


    private static Set<Character> convertStringToSetOfChars(String string) {

        if (string == null || string.isEmpty()) {

            return Collections.emptySet();

        }


        Set<Character> set = new HashSet<>(string.length() + 10);

        for (char c : string.toCharArray()) {

            set.add(c);

        }


        return set;

    }

}

上面的代碼打印:


[n, o]


查看完整回答
反對(duì) 回復(fù) 2022-06-15
?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

好吧,如果一個(gè)人去散列:


public static int uniqueChars(String first, String second) {

    boolean[] hash = new boolean[26]; 

    int count = 0;

    //reduce first string to unique letters

    for (char c : first.toLowerCase().toCharArray()) {

        hash[c - 'a'] = true;

    }

    //reduce to unique letters in both strings

    for(char c : second.toLowerCase().toCharArray()){

        if(hash[c - 'a']){

            count++; 

            hash[c - 'a'] = false;

        }

    }

    return count;

}

這是使用 bucketsort,它給出了 n+m 的復(fù)雜度,但需要 26 個(gè)桶(“散列”數(shù)組)。Imo 在復(fù)雜性方面不能做得更好,因?yàn)槟枰辽俨榭疵總€(gè)字母一次,總和為 n + m。


Insitu 你能得到的最好的結(jié)果是在 O(n log(n) ) 范圍內(nèi)的某個(gè)地方。


你的方法是在 O(n2) 的聯(lián)盟中的某個(gè)地方


插件:如果您需要將字符作為字符串(本質(zhì)上與上面相同,計(jì)數(shù)是返回的字符串的長(zhǎng)度):


public static String uniqueChars(String first, String second) {

    boolean[] hash = new boolean[26]; 

    StringBuilder sb = new StringBuilder();

    for (char c : first.toLowerCase().toCharArray()) {

        hash[c - 'a'] = true;

    }

    for(char c : second.toLowerCase().toCharArray()){

        if(hash[c - 'a']){

            sb.append(c);

            hash[c - 'a'] = false;

        }

    }

    return sb.toString();

}


查看完整回答
反對(duì) 回復(fù) 2022-06-15
?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

獲取每個(gè)字符串的字符列表:


List<Character> chars1 = s1.chars()    // list of chars for first string

            .mapToObj(c -> (char) c)

            .collect(Collectors.toList());


List<Character> chars2 = s2.chars()    // list of chars for second string

            .mapToObj(c -> (char) c)

            .collect(Collectors.toList());

然后使用retainAll方法:


chars1.retainAll(chars2);  // retain in chars1 only the chars that are contained in the chars2 also

System.out.println(chars1.size());

如果您想獲得唯一字符的數(shù)量,只需使用Collectors.toSet()而不是toList()


查看完整回答
反對(duì) 回復(fù) 2022-06-15
?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

public static String getCommonCharacters(String... words) {

    if (words == null || words.length == 0)

        return "";


    Set<Character> unique = words[0].chars().mapToObj(ch -> (char)ch).collect(Collectors.toCollection(TreeSet::new));


    for (String word : words)

        unique.retainAll(word.chars().mapToObj(ch -> (char)ch).collect(Collectors.toSet()));


    return unique.stream().map(String::valueOf).collect(Collectors.joining());

}

另一個(gè)不創(chuàng)建臨時(shí)變量Set并使用Character.


public static String getCommonCharacters(String... words) {

    if (words == null || words.length == 0)

        return "";


    int[] arr = new int[26];

    boolean[] tmp = new boolean[26];


    for (String word : words) {

        Arrays.fill(tmp, false);


        for (int i = 0; i < word.length(); i++) {

            int pos = Character.toLowerCase(word.charAt(i)) - 'a';


            if (tmp[pos])

                continue;


            tmp[pos] = true;

            arr[pos]++;

        }

    }


    StringBuilder buf = new StringBuilder(26);


    for (int i = 0; i < arr.length; i++)

        if (arr[i] == words.length)

            buf.append((char)('a' + i));


    return buf.toString();

}

演示


System.out.println(getCommonCharacters("abcd", "bcde"));  // bcd


查看完整回答
反對(duì) 回復(fù) 2022-06-15
  • 4 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)