searching a jumbled string in a list of strings in C#
I am using .NET2.0
I want to search a jumbled string within a list of stringsstring[] wordList = new string[] { "java", "csharp", "fortran", "cobol", "pascal", "perl", "flash" };
string findText = "spclaa";
// do stuff
I want result to be "pascal" also if not found give "sorry, not Found"
Updated:
- this is not homework
- list is about 1000 words so its not ea开发者_运维百科sy to sort every word
Well, one way would be:
- Create a copy of the array
- Sort each string alphabetically
- Sort your search term alphabetically
- Find the index of the element in the copied array that matches the sorted search term
- If they match, return the corresponding element from the original (unsorted) array.
- If they don't match, keep searching.
It's worth noting that it's possible that two words will have the same character content and yet not be equal. For instance, "neo" and "one" have identical characters, but are clearly not the same word.
Update
As Paul suggested, this will perform better if you only sort strings that have the same number of characters as the search term.
Smells like homework
Sort the characters in findText. Sort the characters for each word in wordList and make a new list. Compare the sorted findText to each sorted word. If you get a match, look up the word at the same index in the original wordList.
(you could probably sort the word from wordList just before you compare it)
EDIT: If you really don't want to sort them,
Count how many of each letter there are in findText.
- Iterate through wordList
- For each word,
- if it is the same length as wordList
- copy the frequency table for findtext
- iterate through the word from wordList
- For each character found, decrement the number in the frequency table if not zero.
- If you match all letters and end up with all zeros you have a match
One method might be to sort the characters in each string and the comparison string and compare them that way.
java becomes aajv
csharp becomes achprs
pascal becomes aaclps
scplaa becomes aaclps
Compare the sorted strings for equality and return not found if no matches.
1.Filter the Array List like search string length equal to array list element
2.compare each character in array list element . If you found increment count and remove that character array list element.
3.If count is equal to search string than print array element otherwise element not found
- List out those strings whose length equals to given string's length.
- From those filtered list, check the sum of ASCII values of each string with the given string.
- third and final now match the characters from the filtered list,you will get the jumbled string.
精彩评论