Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Linq;
- using System.Collections.Generic;
- class Solution {
- public static string[] getFrequencies(string textString) {
- SortedDictionary<string, int> words = new SortedDictionary<string, int>();
- StringBuilder curr = new StringBuilder();
- for (int i = 0; i < textString.Length; i++) {
- if (!Char.IsLetter(textString[i])) {
- if (curr.Length != 0) {
- string s = curr.ToString();
- if (!words.ContainsKey(s)) {
- words.Add(s, 1);
- } else {
- words[s]++;
- }
- curr.Clear();
- }
- } else {
- curr.Append(char.ToLower(textString[i]));
- }
- }
- if (curr.Length != 0) {
- string s = curr.ToString();
- if (!words.ContainsKey(s)) {
- words.Add(s, 1);
- } else {
- words[s]++;
- }
- }
- string[] ret = new string[words.Count];
- int k = 0;
- foreach (string word in words.Keys) {
- ret[k] = word + " " + words[word];
- k++;
- }
- return ret;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string textString = "FirstWord, sECONDwORD, A B A";
- string[] output = Solution.getFrequencies(textString);
- string[] expected = new string[]{"a 2", "b 1", "firstword 1", "secondword 1"};
- Console.WriteLine("Your output: [{0}]", string.Join(", ", output));
- Console.WriteLine("Expected output: [{0}]", string.Join(", ", expected));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement