Guest User

Untitled

a guest
Jul 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. HashMap<Character,Integer> map = new HashMap<Character,Integer>();
  2. String s = "aasjjikkk";
  3. for(int i = 0; i < s.length(); i++){
  4. char c = s.charAt(i);
  5. Integer val = map.get(new Character(c));
  6. if(val != null){
  7. map.put(c, new Integer(val + 1));
  8. }else{
  9. map.put(c,1);
  10. }
  11. }
  12.  
  13. Multiset<Character> chars = HashMultiset.create();
  14. for (int i = 0; i < string.length(); i++) {
  15. chars.add(string.charat(i));
  16. }
  17.  
  18. public class Pair
  19. {
  20. private char letter;
  21. private int count;
  22. public Pair(char letter, int count)
  23. {
  24. this.letter = letter;
  25. this.count= count;
  26. }
  27. public char getLetter(){return key;}
  28. public int getCount(){return count;}
  29. }
  30.  
  31. public static Pair countCharFreq(String s)
  32. {
  33. String temp = s;
  34. java.util.List<Pair> list = new java.util.ArrayList<Pair>();
  35. while(temp.length() != 0)
  36. {
  37. list.add(new Pair(temp.charAt(0), countOccurrences(temp, temp.charAt(0))));
  38. temp.replaceAll("[" + temp.charAt(0) +"]","");
  39. }
  40. }
  41.  
  42. public static int countOccurrences(String s, char c)
  43. {
  44. int count = 0;
  45. for(int i = 0; i < s.length(); i++)
  46. {
  47. if(s.charAt(i) == c) count++;
  48. }
  49. return count;
  50. }
  51.  
  52. Hashtable<Character,Integer> table = new Hashtable<Character,Integer>();
  53. String str = "aasjjikkk";
  54. for( c in str ) {
  55. if( table.get(c) == null )
  56. table.put(c,1);
  57. else
  58. table.put(c,table.get(c) + 1);
  59. }
  60.  
  61. for( elem in table ) {
  62. println "elem:" + elem;
  63. }
Add Comment
Please, Sign In to add comment