开发者

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 strings

string[] 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:


Well, one way would be:

  1. Create a copy of the array
  2. Sort each string alphabetically
  3. Sort your search term alphabetically
  4. Find the index of the element in the copied array that matches the sorted search term
  5. If they match, return the corresponding element from the original (unsorted) array.
  6. 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


  1. List out those strings whose length equals to given string's length.
  2. From those filtered list, check the sum of ASCII values of each string with the given string.
  3. third and final now match the characters from the filtered list,you will get the jumbled string.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜