-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcp-057-breaking-up-text.linq
77 lines (62 loc) · 2.04 KB
/
dcp-057-breaking-up-text.linq
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<Query Kind="Program" />
// This problem was asked by Amazon.
//
// Given a string s and an integer k, break up the string into
// multiple texts such that each text has a length of k or less.
// You must break it up so that words don't break across lines.
// If there's no way to break the text up, then return null.
//
// You can assume that there are no spaces at the ends of the
// string and that there is exactly one space between each word.
//
// For example, given the string "the quick brown fox jumps over
// the lazy dog" and k = 10, you should return: ["the quick",
// "brown fox", "jumps over", "the lazy", "dog"]. No string in
// the list has a length of more than 10.
// UPDATE.
// + "each line has to have the maximum possible amount of words"
void Main()
{
// may be statement misses an optimization restriction that
// we must do such split in a fashion that minimizes amount of chunks?
// will the greedy algorithm work there?
// it might give suboptimal solution though
// got a response from founders -- they said that it's just about each line
// having maximum possible amount of words
// seems fishy... what if to maximize one line content I have to sacrifice
// another line len...
// e.g. len = 8
// ########
// aaa aa
// aaaa aaa <--- we can move "aaa" to the next line
// a <--- what is more optimal breakdown?...
// aaaaaaa
// aaa aaaa
// aa
// anyways, let's solve by simple greed algorithm
Split("aaa aa aaaa aaa a aaaaaaa aaa aaaa aa", 8).Dump();
Split("the quick brown fox jumps over the lazy dog", 10).Dump();
}
string[] Split(string text, int k)
{
string[] words = text.Split(' ');
List<string> lines = new List<string>();
string line = string.Empty;
foreach (string word in words)
{
if (word.Length > k)
return null; // unable to split!
if (line.Length + word.Length + 1 > k)
{
// flush!
lines.Add(line);
line = string.Empty;
}
if (line.Length != 0)
line += " ";
line += word;
}
if (!string.IsNullOrEmpty(line))
lines.Add(line);
return lines.ToArray();
}