Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. class Solution {
  7.     public static string[] getFrequencies(string textString) {
  8.         SortedList<string, int> words = new SortedList<string, int>();
  9.         StringBuilder curr = new StringBuilder();
  10.         for (int i = 0; i < textString.Length; i++) {
  11.             if (!Char.IsLetter(textString[i])) {
  12.                 if (curr.Length != 0) {
  13.                     string s = curr.ToString();
  14.                     if (!words.ContainsKey(s)) {
  15.                         words.Add(s, 1);
  16.                     } else {
  17.                         words[s]++;
  18.                     }
  19.                     curr.Clear();
  20.                 }
  21.             } else {
  22.                 curr.Append(char.ToLower(textString[i]));
  23.             }
  24.         }
  25.         if (curr.Length != 0) {
  26.             string s = curr.ToString();
  27.             if (!words.ContainsKey(s)) {
  28.                 words.Add(s, 1);
  29.             } else {
  30.                 words[s]++;
  31.             }
  32.         }
  33.  
  34.         string[] ret = new string[words.Count];
  35.         int k = 0;
  36.         foreach (string word in words.Keys) {
  37.             ret[k] = word + " " + words[word];
  38.             k++;
  39.         }
  40.  
  41.         return ret;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement