我正在VB中制作一个小小的字谜解决游戏,我想验证给定的单词是否适合给出的字母。例如,字符串eliter
不应与letter
匹配,因为t
仅在上述字符串中出现一次。我已经检查确定提交是否是一个单词,但理想情况下,检查以确保它符合给定的字母将包含在该语句中。
Public Class Form1
Public Function GenerateRandomString(ByRef iLength As Integer) As String
Dim rdm As New Random()
Dim allowChrs() As Char = "ABCDEFGHIJKLOMNOPQRSTUVWXYZ".ToCharArray()
Dim sResult As String = ""
For i As Integer = 0 To iLength - 1
sResult += allowChrs(rdm.Next(0, allowChrs.Length))
Next
Return sResult
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
countdown.Enabled = True
lbl_countdown.Text = "10"
lbl_anagram.Text = GenerateRandomString(9)
End Sub
Private Sub countdown_Tick(sender As Object, e As EventArgs) Handles countdown.Tick
lbl_countdown.Text -= 1
If lbl_countdown.Text < 1 Then
countdown.Enabled = False
MsgBox("End of game")
Dim wordList As HashSet(Of String) = New HashSet(Of String)(File.ReadAllLines("words_alpha.txt"))
If Not wordList.Contains(TextBox1.Text.ToString()) Then
MsgBox("Not found")
Else
MsgBox("Found")
End If
End If
End Sub
End Class
请注意,这需要在VB中,最好不需要额外的库等。
另请注意,您不必使用所有字母,但不能使用超过可用的字母!
提前致谢!
答案 0 :(得分:1)
您只需要按字母顺序对字符串进行排序并比较排序后的字符串。当字符排序时,两个anagram将具有相同的字符串。
Sub Main()
Dim s As String = "betjwepfw"
Console.WriteLine(s)
Console.WriteLine(New String(s.OrderBy(Function(c) c).ToArray()))
Console.ReadLine()
End Sub