comparing to string array

Hello, I have a large string array with many four letter words called "words". I also have a matrix of char that is has been scrambled called "scrambled"(it is a block of letters). I am attempting to sort all possible combinations of the char matrix and compare them to my list to see if there any possible combinations of four letters that match a word in my list. I have tried a couple of things that did not work. I am new to matlab so I am having trouble with the process. Below are examples of the variables I am working with. Any advice is greatly appreciated. Thanks in advance!
words = ["make" "hope" "list" "home"]
scrambled =
kame
stit
abnaa

7 Comments

Did you intend scrambled to be a 3x4 character array, like this?
scrambled = ['kame';
'stit';
'abna'];
Can letters be chosen from anywhere in the array to be "unscrambled", or only from a single row?
abnaa is not 4 letters?
Yes! Sorry it should be only 4 letters per row. I selected 3 random four letter words from my large string array of four letter words then I scrambled them and put them in the char matrix.
The user is supposed to look at the block and find any four letter word. The user can choose letters from anywhere in the block to derive his four letter word.
After the user makes a selection my code will do the following by use of fucntions:
1. verify() This function will verify that the users selection is actually in my large string array of 4 letter words.
2. replace() If verify results true then my program will remove the users selection from the block of letters and put the remaing block of letters into a variable so that I can display it again to the user to look for another 4 letter word.
3. verify2() This function will take the remaining block of letters and compare them to my string array of 4 letter words to see if it is even possible for the user to continue. It would ruin the program if there were no more possible 4 letter words the user could find that matched my list and he/she just kept guessing.
I am looking for assistance and guidance on the "verify2()" function.
I am sorry for the lengthy post but it is hard for me to get effecient help by without putting things into context and only asking peice by peice.
Your algorithm is not efficient.
For any given word,
if all(ismember(thisword, available_letters))
then the word can be formed and you can quit checking at that point.
However you do need to modify this algorithm to account for multiple copies of the same letter being required, such as being able to figure out that you cannot form 'boss' if you only have one available s.
What would "thisword" and "available_letters" refer to? Is thisword referring to the remaining letters in the block?
can_form_a_word = false;
for thisword = words(:).'
if all(ismember(thisword{1}, letters_remaining_in_block))
can_form_a_word = true;
break;
end
end
if ~can_form_a_word
disp('No more word soup for you!')
end

Sign in to comment.

Answers (1)

ScS = string(scrambled);
[present, index] = ismember(ScS, words) ;
present will be a logical vector. Each location that is true will have a corresponding value in index that will reflect the position inside words that scrambled came from. For example if present(2) is true and index(2) is 7 then ScS(2) is words(7)

Categories

Tags

Asked:

on 22 Nov 2019

Edited:

on 24 Nov 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!