Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 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.         Dictionary<string, int> words = new Dictionary<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.         List<string> w = new List<string>();
  35.         foreach (string s in words.Keys) {
  36.             w.Add(s);
  37.         }
  38.         w.Sort();
  39.  
  40.         string[] ret = new string[words.Count];
  41.         for (int i = 0; i < w.Count; i++) {
  42.             ret[i] = w[i] + " " + words[w[i]];
  43.         }
  44.  
  45.         return ret;
  46.     }
  47. }
  48.  
  49. class Program
  50. {
  51.     static void Main(string[] args)
  52.     {
  53.         string textString = "FirstWord, sECONDwORD, A B A";
  54.         string[] output = Solution.getFrequencies(textString);
  55.         string[] expected = new string[]{"a 2", "b 1", "firstword 1", "secondword 1"};
  56.  
  57.         Console.WriteLine("Your output: [{0}]", string.Join(", ", output));
  58.         Console.WriteLine("Expected output: [{0}]", string.Join(", ", expected));
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement