473,669 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

words within a word

19 New Member
Hi all. I've been given an assignment to construct an algorithm that can find all words that occur within a given word. For example:

Given furthermore, solutions could be
form,
the,
further,
more,
theme
...and so on.

I thought about using the STL next_permutatio n() function, but it only finds permutation with the same amount of letters. Searching the dictionary for correct words isn't hard, I just can't start to figure out how to come up with all of the words. Any suggestions?
Sep 9 '08 #1
6 3315
newb16
687 Contributor
something like this
Expand|Select|Wrap|Line Numbers
  1. foreach( word in dictionary)
  2. {
  3.  foreach ( letter in [A..Z] )
  4.  {
  5.   if( letter_count(word, letter)>letter_count(original_word, letter) )
  6.    next word;
  7.  }
  8.  print word;
  9.  
ie if each letter occurs the same or less times in dictionay word that original word, it can be constructed from the letters of original word.
Calculation of letter_count(or iginal_word,let ter) probably should be moved outside the loop because is has the same value for any word_in_diction ary.
Sep 10 '08 #2
toefraz
19 New Member
I looked at that algorithm, but it requires looking at each word in the dictionary, and then seeing if it can be made from the given word, and is a very slow algorithm (takes about 15seconds on my core 2 duo 2ghz!) I know the faster way would be to find all combinations of the given word and matching them against the dictionary because then you wouldn't have to check every single dictionary entry. I just can't figure out how to do the combinatorial algebra to divine each combination.
Sep 12 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
Try one on paper, for instance try finding the all the combinations available from 5 letters (A, B, C, D, E).

Do it by the number of combinations for each result length so find the number of combinations of length 1, then length 2 and 3, 4 and 5.

Look at how you did it and then try to duplicate it on your computer as an algorithm.

Be aware that if you have 2 letters the same in the initial word you risk producing some duplicate results. That is if the word is "free" and you are picking 2 letter combinations then there are 2 ways of picking both "fe" and "re".

You could also try look up the maths for combinations which is binomial coefficients.
Sep 12 '08 #4
arnaudk
424 Contributor
Once you have the various permutations, you'll need to check if they're actual words. For an efficient way of doing this, check out this thread.
Sep 12 '08 #5
donbock
2,426 Recognized Expert Top Contributor
I looked at that algorithm, but it requires looking at each word in the dictionary, and then seeing if it can be made from the given word, and is a very slow algorithm (takes about 15seconds on my core 2 duo 2ghz!) I know the faster way would be to find all combinations of the given word and matching them against the dictionary because then you wouldn't have to check every single dictionary entry. I just can't figure out how to do the combinatorial algebra to divine each combination.
You may be disappointed by that strategy. Take your example: "furthermor e". Your strategy requires you to first compose a list of all the 1-letter sequences, 2-letter sequences, ... and finally the 11-letter sequences that can be formed from those 11 letters and then check each against your dictionary to see if it is a word.

Notice that I said "sequence" rather than your term "combinatio n". That's because "combinatio n" is a technical term in this context. You should look up the terms "permutatio n" and "combinatio n" to see which is appropriate for your problem. If you're not already familiar with it, you should also look up the "factorial" operator.

For an 11-letter word, there are 108,303,111 different sequences to compare against your dictionary! Odds are that's a lot more terms than are in your dictionary. This number comes from trusting the combination/permutation function in Excel.

A lot of times the best way to start a coding problem is to NOT code ... but think about the strategies you might use and then find ways to compare them.

Cheers,
donbock
Sep 12 '08 #6
donbock
2,426 Recognized Expert Top Contributor
something like this
Expand|Select|Wrap|Line Numbers
  1. foreach( word in dictionary)
  2. {
  3.  foreach ( letter in [A..Z] )
  4.  {
  5.   if( letter_count(word, letter)>letter_count(original_word, letter) )
  6.    next word;
  7.  }
  8.  print word;
  9.  
ie if each letter occurs the same or less times in dictionay word that original word, it can be constructed from the letters of original word.
Calculation of letter_count(or iginal_word,let ter) probably should be moved outside the loop because is has the same value for any word_in_diction ary.
Another approach would be to sort the letters in the original word and then, for each dictionary word, compare the sorted original word against the sorted dictionary word. This is similar to Knuth's anagram algorithm in "Sorting and Searching". Notice that unlike Knuth, you are not seeking equality in this comparison. Presorting the dictionary would be a very good tactic if several input words were to be checked.

Cheers,
donbock
Sep 12 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4317
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str' which may contain new lines and will contain html code, IN this string any "words" that begin with an underscore I want to replace with a given word. A word here being a group of chars preceded by a space or null (start of line) and closed by a...
31
2939
by: Brynn | last post by:
I want to thank the person that sent the nasty messages to my contact.asp test page ... you have inspired me !!! I have now added a bad_words filter both to my JSValidation script (going up on my site soon) and my contact.asp tool. I would have forgotten about it if it were not for you. I TRULY appreciate your help ... and hope that you helping me eats you up inside ... ROFL.
12
2185
by: teoryn | last post by:
I've been spending today learning python and as an exercise I've ported a program I wrote in java that unscrambles a word. Before describing the problem, here's the code: *--beginning of file--* #!/usr/bin/python # Filename: unscram.py def sort_string(word): '''Returns word in lowercase sorted alphabetically'''
1
4372
by: none | last post by:
Hello there, I have a table with many text and varchar fields and I would like to get all distinct words from these filelds. For example: table Pet: id int(10) unsigned legende text
8
2376
by: Peter Bungart | last post by:
I have a text field in Access (XP) that lists the occurrences of various plants, which are delimited within the field by commas (e.g., cactus, mesquite, yucca, cliffrose). Is there a way to tabulate the occurrence of each type of plant within the field, essentially allowing me to consider the text as data which can be analyzed across a geographic area? Would Word or Excel be a better tool for this? I hope my question is clear--its a little...
7
43592
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring). Is there something equivalent in C#, preferably built-in (assumed better performance), or sample code? Thanks in advance.
4
1653
by: MN | last post by:
Hello all - I'm really at wits end on this as I'm not having much success with locating a function or other options I have. I'm working on a message board for a website and I'm needing to check the length of the words that users put in. For example, when someone puts in "yesssssssssssss!", I put together a small function that omits all of the s's as this is wreaking havoc on the layout of tables within the site. But I don't have a...
5
5035
by: vonclausowitz | last post by:
Repost from an VB group. Hi All, I'm looking for a way to search for multiple words in a database. There is however one but. The words have to be within a certain range of each other. For example I would like to find records which contain the word MALE and within for example 10 words of it the word BROWN.
4
2080
by: sumedh..... | last post by:
In a compiler there are 36bits for a word and to store a character 8bits are needed. In this to store a character two words appended. Then for storing k characters string,how many words are needed?
7
2173
by: Hvid Hat | last post by:
Hello How should I go about marking certain words in a text? I've got a list of words: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="Mark.xsl"?> <Words> <Word> <Acronym>XML</Acronym>
0
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8896
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.