Problem description

The following is a description of the problem.

English

We are building a word processor and we would like to implement a “word-wrap” functionality.

Given a list of words followed by a maximum number of characters in a line, return a collection of strings where each string element represents a line that contains as many words as possible, With the words in each line being concatenated with a single ‘-‘ but easier to see for testing). The length of each string must not exceed the maximum character length per line.

Your function should take in the maximum characters per line and return a data structure representing all lines in the indicated max length.

Chinese

We want to build a string handler that will process strings and arguments given input.

We will first define an array of strings in which each element is stored as a word, and we will give an argument of type integer. Your method will evaluate the two arguments entered above, add a character “-” between each word to distinguish it, and the length of each element of the new array or List will not be longer than the given string length.

If your newly generated element ends with a dash, you need to remove the dash.

The sample

An example of this problem is provided below for your reference.

Input parameter 1

Input parameter 2

The output

Words1 = [” The “, “day”, “began”, “as”, “, “, “as”, “The”, “night”, “abruptly,” “lighted”, “with” and “brilliant”. “Flame”]

13

[” The-Day-Began, “” As-Still-As,” “The-Night,” “abruptly,” “Lighted – With,” “Brilliant,” “Flame”]

Words1 = [” The “, “day”, “began”, “as”, “, “, “as”, “The”, “night”, “abruptly,” “lighted”, “with” and “brilliant”. “Flame”]

20

[ “The-day-began-as”, “still-as-the-night”, “abruptly-lighted”, “with-brilliant-flame” ]

Words2 = [” Hello “]

5

[ “Hello” ]

Words3 = [” Hello “, “world”]

5

[ “Hello”, “world” ]

Words4 = [” Well “, “Hello”, “world”]

5

[ “Well”, “Hello”, “world” ]

Words5 = [” Hello “, “HelloWorld”, “Hello”, “Hello”]

20

[” Hello – the HelloWorld “, “Hello – Hello”]

The above examples are for testing, where words1 through 5 are the defined variable names, so don’t worry too much.

Ideas review and source code

This topic can be quite difficult, especially if there are no development tools to compile it.

This question is one of indeed.com’s online interview tests. The company’s online interview test uses an assessment tool provided by a third-party company. The interview may be conducted by someone who does not know much about technology, or someone who knows a lot about technology. We do not know the specific situation.

But during the interview, they only pay attention to the output of the process and the result of the interview. As for your thoughts or your thoughts, the interviewer may not pay much attention to them and may not listen to your explanation. Most of the time, you are talking to yourself.

The overall feeling is that the interview is less interactive and more about you talking to the screen.

Initially, my idea was to first iterate through the array, and when I got the first element, I added a bar to the end of the element, then compared it to the length, and if the length exceeded the given length, I removed the bar and pressed it into the list to return.

If the length is less than the returned length, then take out the next element, and at the same time add bar at the end of the judgment, and then determine the treatment of bar.

The main problem with this problem is how to deal with the bars, sometimes the bars are at the end, sometimes the bars are at the beginning, you have to read them one by one.

In subsequent tests, I found that the horizontal bars were always poorly handled, resulting in a failure to fully pass the final test, the second line of the test case above.

Idea 2

Because time was limited for this topic, and we weren’t able to use StringUtils for some quick string processing, I didn’t complete all the tests in the allotted time. After the interview, I thought about the questions carefully and found that there are other ways to operate.

I used the following ideas and finished modifying the code.

First we need to turn the input array into a long string, separated by bars between words. For example, [” Hello “, “world”] will become the string: hello-world.

After doing the above, we need to use a while loop to do it.

The while loop first reads that the entire string length is less than a given length, and then interrupts the loop by returning directly.

The next step is to truncate the string from the beginning to the given length to obtain the substring, and then determine the substring. If the substring ends in a bar, delete the bar and press into the array to be returned, and then update the string to be processed to the remaining string after the truncation.

If according to the given length after cutting, you get the final one character is not rung, so we know you truncation on the words, get the substring, find the last rung, and then get the index ID, after get the index ID to truncate the need to deal with according to the index ID string.

Then delete the last bar and press it into the list to return.

What you might encounter in the rest of the string is that you are currently going to start with a slash, so you also need to remove the leading and ending dashes in the rest of the string.

The above processing continues until the length of the string to be processed is less than the given length and the loop is interrupted.

The figure above is the returned result after testing the algorithm in thought 2 above. It can be seen from the result that it meets the expectation of required output.

code

Visit GitHub: github.com/cwiki-us-do…

www.ossez.com/t/a-word-wr…