One word for several words

LeetCode link

first thought

  • create a set to maintain all the words
  • split each word from index i (i from 0 to end), if the first part of it is in the set, and split the second recursively.
  • remember each word should be split at least once.

solution

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

public class Solution {

public List<String> findAllConcatenatedWordsInADict(String[] words) {

Set<String> set = new HashSet<>();

for (String word: words) {

if (word.length() > 0) {

set.add(word);

}

}

List<String> results = new ArrayList<>();

for (String word: words) {

if (word.length() > 0) {

this.helper(word, set, 0, results);

}

}

return results;

}

private void helper(String word, Set<String> set, int start, List<String> results) {

if (start == word.length()) {

results.add(word);

}

for (int i = start + 1; i <= word.length(); i++) {

String first = word.substring(start, i);

if (set.contains(first) && !first.equals(word)) {

this.helper(word, set, i, results);

}

}

}

}

problem

has duplicated output

reason

a valid word may be added to the results for more than once because the different split during recursion.(e.g input: [a,b,ab,aa,aab] so ‘aab’ is split to ‘a ab’ and ‘aa b’ are both ok)


modification

change the results from List to Set

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

public class Solution {

public List<String> findAllConcatenatedWordsInADict(String[] words) {

Set<String> set = new HashSet<>();

for (String word: words) {

if (word.length() > 0) {

set.add(word);

}

}

Set<String> results = new HashSet<>();

for (String word: words) {

if (word.length() > 0) {

this.helper(word, set, 0, results);

}

}

List<String> result = new ArrayList<>();

result.addAll(results);

return result;

}

private void helper(String word, Set<String> set, int start, Set<String> results) {

if (start == word.length()) {

results.add(word);

}

for (int i = start + 1; i <= word.length(); i++) {

String first = word.substring(start, i);

if (set.contains(first) && !first.equals(word)) {

this.helper(word, set, i, results);

}

}

}

}

problem

TLE

reason

the same as the previous problem, check one word for several times


modification

if a word is valid, then jump out of the recursion and continue to check the next one.And this can also fix the first duplicate problem.

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

public class Solution {

public List<String> findAllConcatenatedWordsInADict(String[] words) {

Set<String> set = new HashSet<>();

for (String word: words) {

if (word.length() > 0) {

set.add(word);

}

}

List<String> results = new ArrayList<>();

for (String word: words) {

if (word.length() > 0 && this.helper(word, set, 0)) {

results.add(word);

}

}

return results;

}

private boolean helper(String word, Set<String> set, int start) {

if (start == word.length()) {

return true;

}

for (int i = start + 1; i <= word.length(); i++) {

String first = word.substring(start, i);

if (set.contains(first) && !first.equals(word)) {

if (this.helper(word, set, i)) {

return true;

}

}

}

return false;

}

}

.

OneWord

There are some rules for joining two different words into one, but they do not cover all cases

AREAS OF UNCERTAINTY ABOUT JOINING WORDS TOGETHER

Is it correct to write bath tub, or should it be the single word bathtub? Is every day a correct spelling, or everyday? Uncertainties like this are widespread in English, even among proficient users. They are made worse by the fact that in some cases both spellings are correct, but mean different things.

Are there any guidelines for resolving such uncertainties? It seems that in some cases there are and in some there are not. I wish here to indicate some of these guidelines. They mostly involve combinations that can make either one word or two, depending on meaning or grammar.

.

ORDINARY COMPOUNDS

Ordinary compounds are the area with the fewest guidelines. They include words like coursework, which I like to write as a single word but my Microsoft Word spellchecker tells me should be two. As a linguist, I usually disregard computer advice about language (see 68. How Computers Get Grammar Wrong), but the question of why ordinary compound words give especial problems is interesting. First, these words need to be defined.

One can think of a compound as two or more words joined together. Linguists, though, like to speak of joined roots or stems rather than words, partly because the joining into a compound stops them being words (a few are not even words by themselves, e.g. horti- in horticulture).

Another problem with “joined words” is that some, such as fearless, are not considered compounds at all. The -less ending is called not a “root” but an “affix”, a meaningful word part added to a root to modify its meaning. Most affixes (some named suffixes, e.g. -less, -ness, -tion, -ly, -ing; some prefixes, e.g. -un-, in-, mis-, pre-) cannot be separate words, but a few like -less can (see 106. Word-Like Suffixes and 146. Some Important Prefix Types). Thus, words like fearless, unhappy and international are not compounds because they have fewer than two roots. Other compounds are swimsuit, homework and eavesdrop.

Suggestions for recognising a compound are not always very helpful. The frequency of words occurring together is no guide because it ignores the fact that many frequent combinations are not compounds (e.g. town hall and open air). The grammatical classes of the words and the closeness of the link between them are sometimes mentioned, but are unreliable. The age of a combination is also suggested, the claim being that compounds originate as two separate words, and gradually evolve through constant use first into hyphenated expressions (like fire-eater or speed-read – see 223. Uses of Hyphens), and eventually into compounds. However, some quite recent words are already compounds, such as bitmap in computing.

Much more useful is the way compounds are pronounced. Single English words generally contain one syllable that is pronounced more strongly than the others (see 125. Stress and Emphasis). This means compounds should have just one strong syllable, while non-compounds should have more. The rule applies fairly universally (see 243. Pronunciation Secrets, #3). For example, home is the only strong syllable in homework, but one of two in home rule. I write coursework as one word because course- is stronger than work.

The only problem with this approach is that you have to know pronunciations before you start, which is not always the case if English is not your mother tongue. The only other resort is a dictionary or spellcheck!

.

NOUNS DERIVED FROM PHRASAL VERBS

Happily, some compound words have some other helpful features. Most are words whose roots, if written as two words, are also correct but have different meaning and grammar, so that the meaning indicates the spelling or vice versa. A particularly large category of such words is illustrated by the compound noun giveaway (= “obvious clue”). If its two roots are written separately as give away, they become a “phrasal” verb – a combination of a simple English verb (give) with a small adverb (away) – meaning “unintentionally reveal” (see 244. Special Uses of GIVE, #12).

There are many other nouns that can become phrasal verbs, e.g. takeover, takeaway, makeup, cutoff, breakout, setdown, pickup, washout, login and stopover. In writing there is always a need to remember that, if the two “words” are going to act as a verb, they must be spelled separately, but if they are going to act as a noun, they must be written together.

.

OTHER CHOICES THAT DEPEND ON WORD CLASS

In the examples above, it is the choice between noun and verb uses that determines the spelling. Other grammatical choices can have this effect too. The two alternative spellings mentioned earlier, every day and everyday, are an example. The first (with ev- and day said equally strongly) acts in sentences like a noun or adverb, the second (with ev- the strongest) like an adjective. Compare: 

(a) NOUN: Every day is different.

(b) ADVERB: Dentists recommend cleaning your teeth every day.

(c) ADJECTIVE: Everyday necessities are expensive. 

In (a), every day is noun-like because it is the subject of the verb is (for details of subjects, see 12. Singular and Plural Verb Choices). In (b), the same words act like an adverb, because they give more information about a verb (cleaning) and could easily be replaced by a more familiar adverb like regularly or thoroughly (see 120. Six Things to Know about Adverbs). In (c), the single word everyday appears before a noun (necessities), giving information about it just as any adjective might (see 109. Placing an Adjective after its Noun). It is easily replaced by a more recognizable adjective like regular or dailyFor more about every, see 169. “All”, “Each” and “Every”.

Another example of a noun/adverb contrast is any more (as in …cannot pay any more) versus anymore (…cannot pay anymore). In the first, any more is the object of pay and means “more than this amount”, while in the second anymore is not the object of pay (we have to understand something like money instead), and has the adverb meaning “for a longer time”.

A further adverb/adjective contrast is on board versus onboard. I once saw an aeroplane advertisement wrongly saying *available onboard – using an adjective to do an adverb job. The adverb on board is needed because it “describes” an adjective (available). The adjective form cannot be used because there is no noun to describe (see 6. Adjectives with no Noun 1). A correct adjective use would be onboard availability.

Slightly different is alright versus all right. The single word is either an adjective meaning “acceptable” or “undamaged”, as in The system is alright, or an adverb meaning “acceptably”, as in The system works alright. The two words all right, on the other hand, are only an adjective, different in meaning from the adjective alright: they mean “100% correct”. Thus, Your answers are all right means that there are no wrong answers, whereas Your answers are alright means that the answers are acceptable, without indicating how many are right.

Consider also upstairs and up stairs. The single word could be either an adjective (the upstairs room) or an adverb (go upstairs) or a noun (the upstairs). It refers essentially to “the floor above”, without necessarily implying the presence of stairs at all – one could, for example, go upstairs in a lift (see 154. Lone Prepositions after BE). The separated words, by contrast, act only like an adverb and do mean literally “by using stairs” (see 218. Tricky Word Contrasts 8, #3).

The pair may be and maybe illustrates a verb and adverb use:

(d) VERB: Food prices may be higher.

(e) ADVERB: Food prices are maybe higher.

In (e), the verb is are. The adverb maybe, which modifies its meaning, could be replaced by perhaps or possibly. Indeed, in formal writing it should be so replaced because maybe is conversational (see 108. Formal and Informal Words).

My final example is some times and sometimes, noun and adverb:

(f) NOUN: Some times are harder than others.

(g) ADVERB: Sometimes life is harder than at other times. 

Again, replacement is a useful separation strategy. The noun times, the subject of are in (f), can be replaced by a more familiar noun like days without radically altering the sentence, while the adverb sometimes in (g) corresponds to occasionally, the subject of is being the noun life.

.

USES INVOLVING “some”, “any”, “every” AND “no”

The words some, any, every and no generally do not make compounds, but can go before practically any noun to make a “noun phrase”. In a few cases, however, this trend is broken and these words must combine with the word after them to form a compound. Occasionally there is even a choice between using one word or two, depending on meaning.

The compulsory some compounds are somehow, somewhere and somewhat; the any compounds are anyhow and anywhere, while every and no make everywhere and nowhere. There is a simple observation that may help these compounds to be remembered: the part after some/any/every/no is not a noun, as is usually required, but a question word instead. The rule is thus that if a combination starting with some, any, every or no lacks a noun, a single word must be written.

The combinations that can be one word or two depending on meaning are someone, somebody, something, sometime, sometimes, anyone, anybody, anything, anyway (Americans might add anytime and anyplace), everyone, everybody, everything, everyday, no-one, nobody and nothing. The endings in these words (-one, -body, -thing, -way, -time, -place and –day) are noun-like and mean the same as question words (who? what/which? how? when? and where? – see 185. Noun Synonyms of Question Words).

Some (tentative) meaning differences associated with these alternative spellings are as follows: 

SOME TIME = “an amount of time”

Please give me some time.

SOMETIME (adj.) = “past; old; erstwhile”

I met a sometime colleague

.

SOMETHING = “an object whose exact nature is unimportant”.

SOME THING = “a nasty creature whose exact nature is unknown” (see 260. Formal Written Uses of “Thing”, #2).

Some thing was lurking in the water.

.

ANYONE/ANYBODY = “one or more people; it is unimportant who”

Anyone can come = Whoever wants to come is welcome; Choose anyone = Choose whoever you want – one or more people.

ANY ONE = “any single person/thing out of a group of possibilities”.

Any one can come = Only one person/thing (freely chosen) can come; Choose any one = Choose whoever/whichever you want, but only one.

ANY BODY = “any single body belonging to a living or dead creature”.

Any body is suitable = I will accept whatever body is available.

.

ANYTHING = “whatever (non-human) is conceivable/possible, without limit”.

Bring anything you like = There is no limit in what you can bring; Anything can happen = There is no limit on possible happenings.

ANY THING = “any single non-human entity in a set”.

Choose any thing = Freely choose one of the things in front of you.

.

EVERYONE/EVERYBODY = “all people” (see 169. “All”, “Each” and “Every” and 211.General Words for People).

Everyone/Everybody is welcome.

EVERY ONE = “all members of a previously-mentioned group of at least three things (not people)”.

Diamonds are popular. Every one sells easily.

EVERY BODY = “all individual bodies without exceptions”.

.

EVERYTHING = “all things/aspects/ideas”.

Everything is clear.

EVERY THING = “all individual objects, emphasising lack of exceptions”.

Every thing on display was a gift.

.

NO-ONE/NOBODY = “no people”

No-one/Nobody came.

NO ONE = “not a single” (+ noun)

No one answer is right.

NO BODY = “no individual body”.

.

NOTHING = “zero”.

Nothing is impossible.

NO THING = “no individual object”. 

There are other problem combinations besides those discussed here; hopefully these examples will make them easier to deal with.

You may have said it best in your question: in words. After all, that’s how several website tutorials describe it:

This is the line underneath «Pay to the order of» where you write out, in words, the dollar amount of the check.

Amount of check in words — Enter the amount of the check in words. Start writing at the far left side of the line. Follow the dollar amount by the word «and,» then write the amount of cents over the number 100. Draw a line from the end of the 100 to the end of the line.

…the different parts of the check (date, name and address of account holder, receiver of check or payee, amount in numerals, amount written in words, memo line, signature, account information)

Writing the amount of a check in words.

The amount written in words.

The amount in words is written on this line.

Amount of check: This should be written in words.

Term: written amount
Definition: amount of check written in words

Понравилась статья? Поделить с друзьями:
  • One word for set of rules
  • One word for set of clothes
  • One word for self made
  • One word for second thought
  • One word for school life