-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
46 lines (45 loc) · 1.45 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Linq;
namespace RearrangeWordsInASentence
{
class Program
{
static void Main(string[] args)
{
var text = "Leetcode is cool";
Console.WriteLine(ArrangeWords_HashMap(text));
}
static string ArrangeWords_OrderBy(string text)
{
var words = text.Split(' ');
var res = "";
words[0] = words[0].ToLower();
words = words.OrderBy(x => x.Length).ToArray();
res = string.Join(' ', words);
return char.ToUpper(res[0]) + res.Substring(1);
}
static string ArrangeWords_HashMap(string text)
{
var words = text.Split(' ');
words[0] = words[0].ToLower();
var longest = words.Max(word => word.Length);
var records = new List<string>[longest + 1];
var res = "";
foreach (var word in words)
{
if (records[word.Length] == null)
records[word.Length] = new List<string> { word };
else
records[word.Length].Add(word);
}
foreach (var record in records)
{
if (record == null)
continue;
res += string.Join(' ', record) + " ";
}
return char.ToUpper(res[0]) + res.Substring(1, res.Length - 2);
}
}
}