SHARE
TWEET

Untitled

a guest Oct 14th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         SortedDictionary<string, int> words = new SortedDictionary<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. }
  44.  
  45. class Program
  46. {
  47.     static void Main(string[] args)
  48.     {
  49.         string textString = "FirstWord, sECONDwORD, A B A";
  50.         string[] output = Solution.getFrequencies(textString);
  51.         string[] expected = new string[]{"a 2", "b 1", "firstword 1", "secondword 1"};
  52.  
  53.         Console.WriteLine("Your output: [{0}]", string.Join(", ", output));
  54.         Console.WriteLine("Expected output: [{0}]", string.Join(", ", expected));
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top