From Wikipedia, the free encyclopedia
A word square is a type of acrostic. It consists of a set of words written out in a square grid, such that the same words can be read both horizontally and vertically. The number of words, which is equal to the number of letters in each word, is known as the «order» of the square. For example, this is an order 5 square:
H E A R T |
E M B E R |
A B U S E |
R E S I N |
T R E N D |
A popular puzzle dating well into ancient times, the word square is sometimes compared to the numerical magic square, though apart from the fact that both use square grids there is no real connection between the two.
Early history[edit]
Sator Square[edit]
The first-century Sator Square is a Latin word square, which the Encyclopedia Britannica called «the most familiar lettered square in the Western world».[2]
Its canonical form reads as follows:
S A T O R |
A R E P O |
T E N E T |
O P E R A |
R O T A S |
In addition to satisfying the basic properties of word squares, it is palindromic; it can be read as a 25-letter palindromic sentence (of an obscure meaning) and it is speculated that it includes several additional hidden words such as reference to the Christian Paternoster prayer, and hidden symbols such as the cross formed by the horizontal and vertical palindromic word «Tenet». The square became a powerful religious and magical symbol in medieval times, and despite over a century of considerable academic study, its origin and meaning are still a source of debate.[3][4]
Abramelin the Mage[edit]
If the «words» in a word square need not be true words, arbitrarily large squares of pronounceable combinations can be constructed. The following 12×12 array of letters appears in a Hebrew manuscript of The Book of the Sacred Magic of Abramelin the Mage of 1458, said to have been «given by God, and bequeathed by Abraham». An English edition appeared in 1898. This is square 7 of Chapter IX of the Third Book, which is full of incomplete and complete «squares».
I S I C H A D A M I O N |
S E R R A R E P I N T O |
I R A A S I M E L E I S |
C R A T I B A R I N S I |
H A S I N A S U O T I R |
A R I B A T I N T I R A |
D E M A S I C O A N O C |
A P E R U N O I B E M I |
M I L I O T A B U L E L |
I N E N T I N E L E L A |
O T I S I R O M E L I R |
N O S I R A C I L A R I |
No source or explanation is given for any of the «words», so this square does not meet the standards for legitimate word squares. Modern research indicates that a 12-square would be essentially impossible to construct from indexed words and phrases, even using a large number of languages. However, equally large English-language squares consisting of arbitrary phrases containing dictionary words are relatively easy to construct; they too are not considered true word squares, but they have been published in The Enigma and other puzzle magazines as «Something Different» squares.
Modern English squares[edit]
A specimen of the order-six square (or 6-square) was first published in English in 1859; the 7-square in 1877; the 8-square in 1884; and the 9-square in 1897.[5]
Here are examples of English word squares up to order eight:
A | N O | B I T | C A R D | H E A R T | G A R T E R | B R A V A D O | L A T E R A L S |
O N | I C E | A R E A | E M B E R | A V E R S E | R E N A M E D | A X O N E M A L | |
T E N | R E A R | A B U S E | R E C I T E | A N A L O G Y | T O E P L A T E | ||
D A R T | R E S I N | T R I B A L | V A L U E R S | E N P L A N E D | |||
T R E N D | E S T A T E | A M O E B A S | R E L A N D E D | ||||
R E E L E D | D E G R A D E | A M A N D I N E | |||||
O D Y S S E Y | L A T E E N E R | ||||||
S L E D D E R S |
The following is one of several «perfect» nine-squares in English (all words in major dictionaries, uncapitalized, and unpunctuated):[6]
A C H A L A S I A |
C R E N I D E N S |
H E X A N D R I C |
A N A B O L I T E |
L I N O L E N I N |
A D D L E H E A D |
S E R I N E T T E |
I N I T I A T O R |
A S C E N D E R S |
Order 10 squares[edit]
A 10-square is naturally much harder to find, and a «perfect» 10-square in English has been hunted since 1897.[5] It has been called the Holy Grail of logology.
Various methods have produced partial results to the 10-square problem:
- Tautonyms
Since 1921, 10-squares have been constructed from reduplicated words and phrases like «Alala! Alala!» (a reduplicated Greek interjection). Each such square contains five words appearing twice, which in effect constitutes four identical 5-squares. Darryl Francis and Dmitri Borgmann succeeded in using near-tautonyms (second- and third-order reduplication) to employ seven different entries by pairing «orangutang» with «urangutang» and «ranga-ranga» with «tanga-tanga», as follows:[7]
O R A N G U T A N G |
R A N G A R A N G A |
A N D O L A N D O L |
N G O T A N G O T A |
G A L A N G A L A N |
U R A N G U T A N G |
T A N G A T A N G A |
A N D O L A N D O L |
N G O T A N G O T A |
G A L A N G A L A N |
However, «word researchers have always regarded the tautonymic ten-square as an unsatisfactory solution to the problem.»[5]
- 80% solution
In 1976, Frank Rubin produced an incomplete ten-square containing two nonsense phrases at the top and eight dictionary words. If two words could be found containing the patterns «SCENOOTL» and «HYETNNHY», this would become a complete ten-square.
- Constructed vocabulary
From the 1970s, Jeff Grant had a long history of producing well-built squares; concentrating on the ten-square from 1982 to 1985, he produced the first three traditional ten-squares by relying on reasonable coinages such as «Sol Springs» (various extant people named Sol Spring) and «ses tunnels» (French for «its tunnels»). His continuing work produced one of the best of this genre, making use of «impolarity» (found on the Internet) and the plural of «Tony Nader» (found in the white pages), as well as words verified in more traditional references:
D I S T A L I S E D |
I M P O L A R I T Y |
S P I N A C I N E S |
T O N Y N A D E R S |
A L A N B R O W N E |
L A C A R O L I N A |
I R I D O L I N E S |
S I N E W I N E S S |
E T E R N N E S S E |
D Y S S E A S S E S |
- Personal names
By combining common first and last names and verifying the results in white-pages listings, Steve Root of Westboro, Massachusetts, was able to document the existence of all ten names below (total number of people found is listed after each line):
L E O W A D D E L L 1 |
E M M A N E E L E Y 1 |
O M A R G A L V A N 5 |
W A R R E N L I N D 9 |
A N G E L H A N N A 2 |
D E A N H O P P E R 10+ |
D E L L A P O O L E 3 |
E L V I N P O O L E 3 |
L E A N N E L L I S 3 |
L Y N D A R E E S E 5 |
- Geographic names
Around 2000, Rex Gooch of Letchworth, England, analyzed available wordlists and computing requirements and compiled one or two hundred specialized dictionaries and indexes to provide a reasonably strong vocabulary. The largest source was the United States Board on Geographic Names National Imagery and Mapping Agency. In Word Ways in August and November 2002, he published several squares found in this wordlist. The square below has been held by some word square experts as essentially solving the 10-square problem (Daily Mail, The Times), while others anticipate higher-quality 10-squares in the future.[5][8]
D E S C E N D A N T |
E C H E N E I D A E |
S H O R T C O A T S |
C E R B E R U L U S |
E N T E R O M E R E |
N E C R O L A T E R |
D I O U M A B A N A |
A D A L E T A B A T |
N A T U R E N A M E |
T E S S E R A T E D |
There are a few «imperfections»: «Echeneidae» is capitalized, «Dioumabana» and «Adaletabat» are places (in Guinea and Turkey respectively), and «nature-name» is hyphenated.
Many new large word squares and new species[clarification needed] have arisen recently. However, modern combinatorics has demonstrated why the 10-square has taken so long to find, and why 11-squares are extremely unlikely to be constructible using English words (even including transliterated place names). However, 11-squares are possible if words from a number of languages are allowed (Word Ways, August 2004 and May 2005).
Other languages[edit]
Word squares of various sizes have been constructed in numerous languages other than English, including perfect squares formed exclusively from uncapitalized dictionary words. The only perfect 10-squares published in any language to date have been constructed in Latin, and perfect 11-squares have been created in Latin as well.[9] Perfect 9-squares have been constructed in French,[10] while perfect squares of at least order 8 have been constructed in Italian and Spanish.[11] Polyglot 10-squares have also been constructed, each using words from several European languages.[12]
Vocabulary[edit]
It is possible to estimate the size of the vocabulary needed to construct word squares. For example, a 5-square can typically be constructed from as little as a 250-word vocabulary. For each step upwards, one needs roughly four times as many words. For a 9-square, one needs over 60,000 9-letter words, which is practically all of those in single very large dictionaries.
For large squares, the need for a large pool of words prevents one from limiting this set to «desirable» words (i.e. words that are unhyphenated, in common use, without contrived inflections, and uncapitalized), so any resulting word squares are expected to include some exotic words. The opposite problem occurs with small squares: a computer search produces millions of examples, most of which use at least one obscure word. In such cases finding a word square with «desirable» (as described above) words is performed by eliminating the more exotic words or by using a smaller dictionary with only common words. Smaller word squares, used for amusement, are expected to have simple solutions, especially if set as a task for children; but vocabulary in most eight-squares tests the knowledge of an educated adult.
Variant forms[edit]
Double word squares[edit]
Word squares that form different words across and down are known as «double word squares». Examples are:
T O O U R N B E E |
L A C K I R O N M E R E B A K E |
S C E N T C A N O E A R S O N R O U S E F L E E T |
A D M I T S D E A D E N S E R E N E O P I A T E R E N T E R B R E E D S |
The rows and columns of any double word square can be transposed to form another valid square. For example, the order 4 square above may also be written as:
L I M B
A R E A
C O R K
K N E E
Double word squares are somewhat more difficult to find than ordinary word squares, with the largest known fully legitimate English examples (dictionary words only) being of order 8. Puzzlers.org gives an order 8 example dating from 1953, but this contains six place names. Jeff Grant’s example in the February 1992 Word Ways is an improvement, having just two proper nouns («Aloisias», a plural of the personal name Aloisia, a feminine form of Aloysius, and «Thamnata», a Biblical place-name):
T R A T T L E D |
H E M E R I N E |
A P O T O M E S |
M E T A P O R E |
N A I L I N G S |
A L O I S I A S |
T E N T M A T E |
A S S E S S E D |
Diagonal word squares[edit]
Diagonal word squares are word squares in which the main diagonals are also words. There are four diagonals: top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, and bottom-left to top-right. In a Single Diagonal Square (same words reading across and down), these last two will need to be identical and palindromic because of symmetry. The 8-square is the largest found with all diagonals: 9-squares exist with some diagonals.
These are examples of diagonal double squares of order 4:
B A R N A R E A L I A R L A D Y |
S L A M T I L E E A T S P R O S |
T A N S A R E A L I O N L A N D |
Word rectangles[edit]
Word rectangles are based on the same idea as double word squares, but the horizontal and vertical words are of a different length. Here are 4×8 and 5×7 examples:
F R A C T U R E O U T L I N E D B L O O M I N G S E P T E T T E |
G L A S S E S R E L A P S E I M I T A T E S M E A R E D T A N N E R Y |
Again, the rows and columns can be transposed to form another valid rectangle. For example, a 4×8 rectangle can also be written as an 8×4 rectangle.
Higher dimensions[edit]
Word squares can be extended to the third and higher dimensions, such as the word cube and word tesseract below.[13]
K │I │N │G I │ D │ E │ A N │ E │ T │ S G│ A│ S│ H ────┼────┼────┼──── I │D │E │A D │ E │ A │ L E │ A │ R │ L A│ L│ L│ Y ────┼────┼────┼──── N │E │T │S E │ A │ R │ L T │ R │ I │ O S│ L│ O│ P ────┼────┼────┼──── G │A │S │H A │ L │ L │ Y S │ L │ O │ P H│ Y│ P│ E
ALA ROB TWO AEN TEU ARN RAA ARM EYE EAN IBA EAR SRI YAS RIE EAS OYE SAW SON AEA TST HAE ETH OII AMP REU SLE
Other forms[edit]
Numerous other shapes have been employed for word-packing under essentially similar rules. The National Puzzlers’ League maintains a full list of forms which have been attempted.
See also[edit]
- National Puzzlers’ League
- Sator Square
References[edit]
- ^ Ferguson, Everett (1999). Encyclopedia of Early Christianity (2nd ed.). Routledge. p. 1002. ISBN 978-0815333197. Retrieved 16 September 2022.
Rotas Sator (first century): Although the result is striking, the interpretation rests on the unlikely assumptions, and a non-Christian meaning is more probable.
- ^ «Sator square». Encyclopedia Britannica. Retrieved 17 September 2022.
- ^ Sheldon, Rose Mary (2003). «The Sator Rebus: An unsolved cryptogram?». Cryptologia. 27 (3): 233–287. doi:10.1080/0161-110391891919. S2CID 218542154. Retrieved 10 September 2022.
- ^ Griffiths, J. Gwyn (March 1971). «‘Arepo’ in the Magic ‘Sator’ Square». The Classical Review. New Series. 21 (1): 6–8. doi:10.1017/S0009840X00262999.
- ^ a b c d Eckler, A. Ross (2005). «A History of the Ten-Square». In Cipra, Barry Arthur; Demaine, Erik D.; Demaine, Martin L.; Rodgers, Tom (eds.). Tribute To A Mathemagician. A K Peters, Ltd. pp. 85–91. ISBN 978-1-56881-204-5. Retrieved 2008-08-25.
- ^ «Achalasia». Word Ways. August 2003.
- ^ Brandreth, Gyles (1986). Everyman’s Word Games. Book Club Associates. p. 90.
- ^ «Hunting the Ten-Square». Word Ways. May 2004.
- ^ Tentarelli, Eric (November 2020). «Large Word Squares in Latin». Word Ways. 53 (4).
- ^ Bartholdi, Laurent (1996). «Mots croisés mélanophobes» (PDF). Gazette des Mathématiciens (in French). 70.
- ^ Borgmann, Dmitri (1965). Language on Vacation. Charles Scribner’s Sons. p. 198.
- ^ Gooch, Rex (May 2004). «Hunting the Ten-Square». Word Ways. 37 (2).
- ^ Darryl Francis, ‘From Square to Hyperhypercube’, Word Ways: Vol. 4: Issue 3, Article 8, 1971
External links[edit]
- Word Square — Free to play double word squares
- Word Hash — Free to play word squares
- Stairsteps — Daily double word squares and rectangles — Free M-Th
A somewhat simplistic approach would be to form a multiset of all the first letters of each word, and then search for a word that contain the exact same letters and letter counts as the multiset of first letters. If a match is found, we remove the word from consideration. Next, we look for a word whose suffix (beginning at the second letter) matches the multiset of second letters of remaining words. If a match is found, we remove it, and then check third letters, and so forth.
Here’s a Python implementation that makes use of the collections.Counter class in place of a multiset.
from collections import Counter
def word_square(words):
words = list(words)
n = len(words)
assert all(len(w) == n for w in words)
result = []
for i in range(n):
letters = Counter(w[i] for w in words)
match = next((w for w in words if Counter(w[i:]) == letters), None)
if not match:
return None
words.remove(match)
result.append(match)
return result
This solution is, if I not mistaken, is O(n^3)
, which isn’t great, but it’s an improvement over O(n!)
.
wordsquares
Description
Thoughtbot coding challenge: given a dictionary of some kind, generate
word squares of size n.
Initial Approach
After some consideration, I decided to use the approach of filling the word square one word(row) at a time and testing to see if that word or (partial) combination of letters (a ‘word-stem’) is a valid possibility by checking the existing columns to see if there were a possible word match.
For example, for a 2-square (word square of size 2), given a dictionary containing the words ‘av’, ‘an’, ‘no’, we can determine that the word ‘av’ in the first row is invalid because there is no 2-letter word starting with ‘v’. Therefore, we don’t have to test any of words in line 2 and can skip to try the next word in row 1.
This started out as a straight-forward exercise, but I was unhappy with the original completion times, so I eventually coded several different strategies and refactored the program using the Strategy Pattern. At this point, there are three strategies, presented in the order that they were developed.
Strategies
- NaiveWordSquares — a ‘brute-force’ approach, which simply tries all the possible words, one at a time. ‘Possible’ in this case includes consideration as to whether a word added to a row results in acceptable words for each column. If the new row word results in an unacceptable column word, that new word is skipped. This strategy also includes memoization of words that might fit into a column, given what’s already in that column.
- BaseWordSquares. This is identical to the NaiveWordSquares strategy except that it uses a separate Square class to hold the word square, rather than a simple array. This Base strategy is rather slower: a 6-square generated via the Naive strategy takes about 571 seconds, wherea the same 6-square generated via this Base strategy takes about 652 seconds, a roughly 14% decrease in performance due to the use of the Square class. The tradeoff is that the Base version is clearer and made it easier to develop the Pivot strategy.
- PivotWordSquares — an approach which searches for ‘pefect’ word squares (i.e., the word in the ith row is also placed into the ith column). My research indicated that perfect word squares are quite common. In this strategy, each time I add a new word in a row ‘n’, I ‘pivot’ that word so that it also exists in column ‘n’. This strategy includes similar memoization as the BaseWordSquares strategy, but this time based on what is already in the rows because of the pivoting. Thus, if the algorithm has already selected ‘abacay’ for row 1 and ‘bacule’ for row 2, then rows 3 through 6 contain »ac’, ‘cu’, ‘al’, and ‘ye’, respectively, and the memoization limits words selected for each row to these prefixes. This occurs as each new word is added to a row.
Running the programs
There are two different programs. In either case, you must specify a strategy.
-
The first (word_squares) generates word squares up to a 6×6;
./word_squares [strategy]
-
The other (one_word_square) generates the desired size word square directly.
./one_word_square [size] [strategy]
If you wish to code your own runner, this will do the trick, in an executable file:
#!/usr/bin/env ruby
require_relative 'lib/word_squares'
@ws = WordSquares.new([word_list], [strategy]) # There's a word list called... wait for it: word_list
@word_square = @ws.generate([dimension])
Results
- First attempt simply used the basic approach with no optimization and generated a 5-square in 5650 seconds. The 6-square was still running (overnight) at 8 hours when I terminated it :=)
- Adding memoization of the known ‘bad’ word-stems improved the 5-square generation to 205 seconds.
- Additional enhancements yielded a 6-square generated in a little over 6 minutes.
- Final implementation of the Pivot strategy resulted in a 6-square in 7.1 seconds, and a 7-square in 80 seconds.
Size | Naive | Base | Pivot |
---|---|---|---|
5-square | 7 sec | 7 sec | 2 sec |
6-square | 571 sec | 652 sec | 7 sec |
7-square | >8 hours | >8 hours | 80 sec |
-
Algorithms and Data Structures: TheAlgorist.com
-
System Design: DistributedComputing.dev
-
Low Level Design: LowLevelDesign.io
-
Frontend Engineering: FrontendEngineering.io
Help Your Friends save 40% on our products
Problem Statement:
Given a set of words (without duplicates), find all word squares you can build from them.
A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).
For example, the word sequence
[«ball»,»area»,»lead»,»lady»]
forms a word square because each word reads the same both horizontally and vertically.
b a l l a r e a l e a d l a d y
Solution:
-
NOTE: I highly recommend going through the Backtracking chapters in the order they
are given in the Index page to get the most out of it and be able to build a rock-solid understanding.
Prerequisites:
- Trie
-
Backtracking
The solution of this problem has two major parts: (1)
Backtracking, (2)
Trie.
This problem at the beginning right after looking at the problem statement might seem like a pretty difficult problem. But
with systematic and organized step-by-step thinking and with sound knowledge of data structures (like
Trie)
we can easily conquer it by designing a simple algorithm.
It is very easy o understand that it an exhaustive search problem. Next comes the systematic thinking. How
would we actually build word squares from a given list of words ? We would try to build word squares
by trying to make each and every word the top-most word in a word square. For each of these partial
configuration we go on making progress to see if they extend to become a complete
word square. We discard a partial configuration when we see that none of the remaining
words would help us extend an existing partial solution solution to become a complete solution.
How do we know if a word is a good fit to extend a partial Solution to make some progress ?
If we have already built the first (R — 1) rows of a word square,
for the row R we can consider only those words which has the prefix equal to what substring
we have at column R right now.
For example: if we have given list of words = [«ball»,»area»,»lead»,»lady»]
and we have already built partial solution as follows:
ball area
Now we are looking what word to put in 3rd row. It needs to start with «le» since the 3rd column
has «le». So only those words are good candidate for 3rd row of the word square
which has «le» has prefix.
So, the performance of our solution depends on how efficiently we can find out all the words
with a specific prefix from the given
list of words. What data structure is specifically good for searching strings with a given prefix ?
TRIE.
So we would need a
Trie.
Once we have figured till here, we should be able to figure everything else out if we have a good understanding
of how Backtracking works and how to implement a Backtracking solution. Fortunately we have the backtracking template
that makes writing Backtracking code much easier.
Let’s look at the code below and everything would become crystal clear.
This is a Premium content.
Please subscribe to Algorithms course to access the code.
Don’t forget to take in-depth look at the other backtracking problems because that is what would make you comfortable with using
the backtracking template and master the art of Backtracking:
- Letter Case Permutation
- Power Set
- All Paths Between Two Nodes
- Word Search
- Sudoku
- N-Queens
- Word Square
- Generate Parentheses
Instructor:
If you have any feedback, please use this form:
https://thealgorists.com/Feedback.
Help Your Friends save 40% on our products
wave
Description
A word square is a type of acrostic, a word puzzle. In a word square you are given a grid with letters arranged that spell valid English language words when you read from left to right or from top to bottom, with the requirement that the words you spell in each column and row of the same number are the same word. For example, the first row and the first column spell the same word, the second row and second column do, too, and so on. The challenge is that in arranging those letters that you spell valid words that meet those requirements.
One variant is where you’re given an n*n grid and asked to place a set of letters inside to meet these rules. That’s today’s challenge: given the grid dimensions and a list of letters, can you produce a valid word square.
Via /u/Godspiral: http://norvig.com/ngrams/enable1.txt (an English-language dictionary you may wish to use)
Input Description
You’ll be given an integer telling you how many rows and columns (it’s a square) to use and then n2 letters to populate the grid with. Example:
4 eeeeddoonnnsssrv
Output Description
Your program should emit a valid word square with the letters placed to form valid English language words. Example:
rose
oven
send
ends
Challenge Input
4 aaccdeeeemmnnnoo
5 aaaeeeefhhmoonssrrrrttttw
5 aabbeeeeeeeehmosrrrruttvv
7 aaaaaaaaabbeeeeeeedddddggmmlloooonnssssrrrruvvyyy
Challenge Output
moan
once
acme
need
feast
earth
armor
stone
threw
heart
ember
above
revue
trees
bravado
renamed
analogy
valuers
amoebas
degrade
odyssey