2014-09-02 30 views
0

我有一段代碼會告訴我字符串中出現次數最多的字符。該cade如下:靜態字符串中字符數的計數

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
public class MaximumOccurringChar { 

    static final String TEST_CASE_1 = "Hello! Are you all fine? What are u doing today? Hey Guyz,Listen! I have a plan for today. Help!"; 
public static void main(String[] args) { 

     MaximumOccurringChar test = new MaximumOccurringChar(); 
     List<Character> result = test.maximumOccurringChars(TEST_CASE_1, true); 
     System.out.println(result); 
    } 


    public List<Character> maximumOccurringChars(String str) { 
     return maximumOccurringChars(str, false); 
    } 

    // set skipSpaces true if you want to skip spaces 
    public List<Character> maximumOccurringChars(String str, Boolean skipSpaces) { 
     Map<Character, Integer> map = new HashMap<>(); 
     List<Character> occurrences = new ArrayList<>(); 
     int maxOccurring = 0; 

     // creates map of all characters 
     for (int i = 0; i < str.length(); i++) { 
      char ch = str.charAt(i); 

      if (skipSpaces && ch == ' ')  // skips spaces if needed 
       continue; 

      if (map.containsKey(ch)) { 
       map.put(ch, map.get(ch) + 1); 
      } else { 
       map.put(ch, 1); 
      } 

      if (map.get(ch) > maxOccurring) { 
       maxOccurring = map.get(ch);   // saves max occurring 
      } 
     } 

     // finds all characters with maxOccurring and adds it to occurrences List 
     for (Map.Entry<Character, Integer> entry : map.entrySet()) { 
      if (entry.getValue() == maxOccurring) { 
       occurrences.add(entry.getKey()); 
      } 
     } 

     return occurrences; 
    } 
} 

但我無法弄清楚如何顯示他們的計數。例如,如果我放入「aasssddeefgt」,它說s發生次數最多,但它並不告訴我它發生了3次。誰能幫忙?

+1

你在你的'map'計數。需要從那裏帶走它們。爲什麼不只是返回另一個Map而不是List呢? – Thilo 2014-09-02 01:35:46

+0

刪除'List occurrences'並使用您的代碼,以便它不使用發生,只需使用地圖。你想要的值是在地圖 – 2014-09-02 01:37:00

+0

你可以請幫助通過修改代碼? – 2014-09-02 01:48:49

回答

0

我建議你創建一個方法來計算的char的出現在String開始,

static int countOccurences(String in, char ch) { 
    int count = 0; 
    if (in != null) { 
     for (char c : in.toCharArray()) { 
      if (ch == c) { 
       count++; 
      } 
     } 
    } 
    return count; 
} 

接下來,我想創建一個通用ComparableTuple POJO(像) -

static class Tuple<K, V extends Comparable<V>> implements 
     Comparable<Tuple<K, V>> { 
    private K ch; 
    private V count; 

    public Tuple(K ch, V count) { 
     this.ch = ch; 
     this.count = count; 
    } 

    @Override 
    public int compareTo(Tuple<K, V> o) { 
     return o.count.compareTo(this.count); 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (o instanceof Tuple) { 
      return this.ch.equals(((Tuple) o).ch); 
     } 
     return false; 
    } 

    @Override 
    public String toString() { 
     return String.format("'%s' = %d", ch.toString(), count); 
    } 
} 

然後我們可以創建一個Tuple<Character, Integer>List like,

static List<Tuple<Character, Integer>> getTuples(String in) { 
    List<Tuple<Character, Integer>> tuples = new ArrayList<>(); 
    // Only need to get each char count once. 
    Set<Character> processed = new TreeSet<>(); 
    // to skip space(s) 
    // processed.add(' '); // <-- add before the loop. 
    for (char ch : in.toCharArray()) { 
     if (processed.contains(ch)) { 
      continue; 
     } 
     processed.add(ch); 
     tuples.add(new Tuple<>(ch, countOccurences(in, ch))); 
    } 
    return tuples; 
} 

最後,我們可以從main()

public static void main(String[] args) { 
    final String TEST_CASE_1 = "Hello! Are you all fine? What are u doing " 
      + "today? Hey Guyz,Listen! I have a plan for today. Help!"; 
    List<Tuple<Character, Integer>> tuples = getTuples(TEST_CASE_1); 
    Collections.sort(tuples); 
    System.out.println(tuples); 
} 

輸出調用它(和Collections.sort()吧)(格式爲這個職位)

[' ' = 18, 'e' = 8, 'a' = 8, 'l' = 6, 'o' = 6, 'y' = 5, 'n' = 4, 't' = 4, 
'H' = 3, '!' = 3, 'r' = 3, 'u' = 3, 'i' = 3, 'd' = 3, 'f' = 2, '?' = 2, 
'h' = 2, 'p' = 2, 'A' = 1, 'W' = 1, 'g' = 1, 'G' = 1, 'z' = 1, ',' = 1, 
'L' = 1, 's' = 1, 'I' = 1, 'v' = 1, '.' = 1] 
0

分離出的地圖創建和MAX-次數字符進入不同的方法可能會清除問題。

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
public class MaximumOccurringChar { 
static final String TEST_CASE_1 = "Hello! Are you all fine? What are u doing today? Hey Guyz,Listen! I have a plan for today. Help!"; 

public static void main(String[] args) { 

    MaximumOccurringChar test = new MaximumOccurringChar(); 
    Map<Character, Integer> map = charMap(TEST_CASE_1, true); 
    int max = max(map); 
    for(Map.Entry<Character, Integer> e : map.entrySet()){ 
     if(e.getValue() == max) 
      System.out.println(e.getKey() + " " + max); 
    } 
} 

public static Map<Character, Integer> charMap(String s, boolean skipSpaces){ 
    Map<Character, Integer> map = new HashMap<>(); 
    for (int i = 0; i < str.length(); i++) { 
     char ch = str.charAt(i); 

     if (skipSpaces && ch == ' ')  // skips spaces if needed 
      continue; 

     if (map.containsKey(ch)) { 
      map.put(ch, map.get(ch) + 1); 
     } else { 
      map.put(ch, 1); 
     } 
    } 
    return map; 
} 

public static int max(Map<Character, Integer> m){ 
    int max = Integer.MIN_VALUE; 
    for(Integer i : m.values()){ 
     if(i > max) 
      max = i; 
    } 
    return max; 
} 

}