Description
Software like Swype and SwiftKey lets smartphone users enter text by dragging their finger over the on-screen keyboard, rather than tapping on each letter.
You'll be given a string of characters representing the letters the user has dragged their finger over.
For example, if the user wants "rest", the string of input characters might be "resdft" or "resert".
Input
Given the following input strings, find all possible output words 5 characters or longer.
- qwertyuytresdftyuioknn
- gijakjthoijerjidsdfnokg
Output
Your program should find all possible words (5+ characters) that can be derived from the strings supplied.
Use http://norvig.com/ngrams/enable1.txt as your search dictionary.
The order of the output words doesn't matter.
- queen question
- gaeing garring gathering gating geeing gieing going goring
Notes/Hints
Assumptions about the input strings:
- QWERTY keyboard
- Lowercase a-z only, no whitespace or punctuation
- The first and last characters of the input string will always match the first and last characters of the desired output word
- Don't assume users take the most efficient path between letters
- Every letter of the output word will appear in the input string
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace swipe
{
class Program
{
static string path = "C:\\Users\\px06\\Documents\\Visual Studio 2015\\Projects\\swipe\\swipe\\words.txt";
static void Main(string[] args)
{
string[] input = { "cghhjkkllooiuytrrdfdftgyuiuytrfdsaazzseertyuioppoiuhgfcxxcfvghujiiuytrfddeews" };
string[] words = File.ReadAllLines(path);
var watch = System.Diagnostics.Stopwatch.StartNew();
foreach (string inp in input)
{
var acceptable = reduce(words, inp);
foreach (int i in acceptable)
{
if (check(words[i], inp))
{
Debug.WriteLine(words[i]);
}
}
}
watch.Stop();
Console.WriteLine("Run time(ms): " + watch.ElapsedMilliseconds);
Console.ReadLine();
}
static List<int> reduce(string[] list, string input)
{
List<int> acceptableList = new List<int>();
int count = 0;
foreach(string word in list)
{
if (word.Length >= 5 && input[0] == word[0] && input[input.Length - 1] == word[word.Length - 1])
{
acceptableList.Add(count);
}
count++;
}
return acceptableList;
}
static bool check(string word, string input)
{
int charcheck = 0;
foreach (char c in word)
{
charcheck = input.IndexOf(c, charcheck);
if (charcheck == -1)
{
return false;
}
}
return true;
}
}
}
qwertyuytresdftyuioknn
-> queen
, question
-> runtime: 2ms
gijakjthoijerjidsdfnokg
gaeing
garring
gathering
gating
geeing
gieing
going
goring
Run time(ms): 2
Trying a very complex string to see if I could find the word:
chlordiazepoxides
Input:
cghhjkkllooiuytrrdfdftgyuiuytrfdsaazzseertyuioppoiuhgfcxxcfvghujiiuytrfddeews
Output:
caches
cades
cafes
caffs
cages
caids
cairds
capes
caphs
capos
capotes
capouches
capuches
cards
cares
caress
carices
caries
carious
caroches
carps
carpus
carries
carroches
carrots
carrs
carryouts
cartes
cartouches
carts
carves
casefies
caseous
cases
casettes
cashes
cashews
cassettes
cassis
castes
castoffs
castors
casts
casus
catches
cates
catguts
catties
caucus
caves
cavies
cedes
cepes
cercis
cercus
ceres
ceriphs
cerites
ceros
cerous
certes
certifies
cetes
chads
chafes
chaffs
chairs
chaos
chapes
chaps
chards
chares
charges
chariots
charpoys
charros
charrs
chars
charts
chasers
chases
chassepots
chasses
chasseurs
chassis
chats
chaws
chayotes
chays
cheeps
cheerios
cheeros
cheers
chefs
cheroots
cherries
cherts
chess
cheths
chevies
chevres
chews
chias
chiaus
chichis
chics
chiders
chides
chiefs
chiggers
chigoes
chippies
chips
chiros
chirps
chirres
chirrs
chirrups
chits
chitters
chitties
chives
chivies
chivvies
chlorates
chlordiazepoxides
chlorides
chlorids
chlorites
chlorous
choices
choirs
choosers
chooses
chops
choragus
chordates
chords
choregus
chores
chorioids
chorizos
choroids
chorus
choruses
chorusses
choses
chotts
choughs
chousers
chouses
choushes
chows
christies
chrysophytes
chuddahs
chuddars
chudders
chufas
chuffs
chuggers
chugs
churches
churrs
chutes
chutists
ciders
cigars
circuits
circus
cires
cirrous
cirrus
cissies
cissoids
cists
cistus
citators
citers
cites
cities
citifies
citrates
citreous
citrous
citrus
citruses
civies
civvies
clachs
clades
clads
clags
claps
claries
clarifies
claroes
claros
clashes
clasps
class
classers
classes
classics
classifies
classis
clastics
clasts
claughts
claves
clavus
claws
clays
clefs
clefts
clepes
clergies
clerics
clerids
clerihews
clevis
clews
cliches
cliffs
clifts
clips
clitics
cloches
clods
cloggers
clogs
cloistress
cloots
clops
closeouts
closers
closes
closets
closures
clothes
cloths
clots
clotures
clouds
cloughs
clours
clouters
clouts
cloves
cloys
clozes
clues
clutches
clutters
coaches
coacts
coapts
coasts
coatees
coatis
coats
coaxes
coccids
coccus
codas
codders
codecs
coderives
coders
codes
codfishes
codgers
codices
codifies
codirects
codrives
coeds
coerces
coffeepots
coffees
coffers
coffrets
coffs
cogitates
cogitos
coiffes
coiffeurs
coiffures
coifs
coirs
coitus
coituses
cooches
cooees
cooers
cooeys
coofs
coops
coopts
cooters
cooties
coots
copes
copies
copihues
coppices
cordages
corders
cordgrass
cordgrasses
cordierites
cordites
cords
corduroys
corers
cores
corgis
corps
corpus
corrades
corrects
corridas
corridors
corries
corrodes
corrupts
corsacs
corsages
corsairs
corses
corsets
corteges
cortexes
cortices
corvees
corves
coryphees
cosecs
coses
cosets
coseys
coshes
cosies
cossets
costs
coteries
cotes
cotrustees
cottages
cottars
cottas
cotters
cottiers
cotypes
couches
cougars
coughs
coupes
coups
courages
courgettes
couriers
coursers
courses
courteous
courters
courtiers
courts
courtsides
courtyards
couters
couths
coutures
coves
coxes
coydogs
coyotes
coypous
coypus
cozes
cozeys
cozies
cozzes
crafts
crags
crapes
crappies
craps
crases
crashes
crasis
crass
cratches
crates
craves
craws
crazes
crazies
creches
creeds
creepies
creeps
crepes
cress
cretics
crews
criers
cries
cripes
crises
crisis
crisps
critics
critters
critturs
crocs
crocus
crofts
croppies
crops
crores
cross
crouches
crows
cruces
crudes
crudites
cruds
cruets
cruisers
cruises
cruors
crusades
cruses
crusets
crushes
crusts
crutches
cruxes
crypts
cuddies
cuffs
cuifs
cuirass
cuirasses
cuishes
cuisses
cupids
curacies
curaghs
curares
curaris
curassows
curates
curatives
curators
curches
curds
curers
cures
curets
curettes
curfews
curfs
curies
curios
curious
curites
currachs
curraghs
currieries
curriers
curries
currs
cursers
curses
cursives
cursors
curtseys
curtsies
curves
cuscus
cusecs
cuspides
cuspids
cuspis
cusps
cussers
cusses
cussos
custodes
custos
cutches
cutes
cuteys
cutgrass
cutgrasses
cuties
cutis
cutises
cutoffs
cutouts
cuttages
cutters
cutties
cutups
cyders
cypres
cypress
cystoids
cysts
Run time(ms): 5
I just wanted to see what I can improve on my code. I'm very new to C# so I'd like some suggestions on what I can do to either improve efficiency or improve it in general. For the most part it looks like it works well, but could I improve this any further? Is there anything else I could add to make it even faster?
Would anyone know the efficiency of this program? I have a feeling it's \$O(\log n)\$ but obviously that shouldn't be the case by looking at the structure.
I tried feeding in 1,000,000 randomly generated characters and here is the output:
zaikai
zamindari
zastrugi
zecchini
zingani
zingari
zombi
zucchini
Run time(ms): 2
I know that the runtime is significantly decreased by cutting down the dictionary by the first index of the word word[0] == dict[word[0]]
and then the last element word[last] == dict[word[last]]
, so what factors here would determine speed? The only ones I can think of are: size of dictionary and location of the first char (i.e. starting with a z
). Even the input size doesn't determine the runtime, so the complexity isn't increasing even though I am trying to find a match in check()
.
That being said, Visual Studio is using up to 27 MB of RAM while computing the 1M long string... but who really cares about space-complexity?
cg?h?h?j?k?k?l?l?o?o?i?u?y?t?...?s
and match that pattern with the word list. I doubt that this has any better performance than what you are doing now, so I just leave it as a comment here. \$\endgroup\$I'll add comments tomorrow
\$\endgroup\$