我有一句话,"十七",并且必须想出所有可能的写作方式。也就是说,它可能是Seventeen,sEvenTeeN等等。任何人都知道如何实现这一目标?
答案 0 :(得分:1)
试试这个:
Dim word = "seventeen"
Dim generate As Func(Of String, IEnumerable(Of String)) = Nothing
generate = Function(w) _
If(String.IsNullOrEmpty(word), _
{ String.Empty }, _
From h In _
{ _
w.Substring(0, 1).ToLowerInvariant(), _
w.Substring(0, 1).ToUpperInvariant() _
} _
From t In generate(w.Substring(1)) _
Select h & t)
Dim results = generate(word)
它产生了“十七”这个词的512个变体:
十七岁,十七岁,十七岁,十七岁,七十一岁,SeVenteen,sEVenteen,(删除),SevENTEEN,sEvENTEEN,SEVENTEEN,seVENTEEN,SeVENTEEN,sEVENTEEN,SEVENTEEN
为了让这个更容易理解,这是正常的功能版本:
Public Function Generate(word As String) As IEnumerable(Of String)
If String.IsNullOrEmpty(word)
Return { String.Empty }
Else
Return _
From h In _
{ _
word.Substring(0, 1).ToLowerInvariant(), _
word.Substring(0, 1).ToUpperInvariant() _
} _
From t In generate(word.Substring(1)) _
Select h & t
End If
End Function
答案 1 :(得分:0)
将文字分成单词。对每个word.tolower测试你的string.tolower。如果找到匹配项,则保存单词而不使用.tolower
答案 2 :(得分:0)
Enigmativity发布的答案显然更好,但这更容易理解:
Private Sub Btn_AllCases_Click(sender As Object, e As EventArgs) Handles Btn_AllCases.Click
Dim OriginalWord As String = "seventeen"
Dim CharsArr() As Char = OriginalWord.ToCharArray
Dim Combs As Integer = CInt(2 ^ CharsArr.Length)
Dim CombsArr(Combs - 1) As String
Dim ChangeInterval As Integer = Combs
For Chr As Integer = 0 To CharsArr.Length - 1
ChangeInterval = CInt(ChangeInterval / 2)
Dim UpperLower As Boolean = True
Dim ChangeCount As Integer = ChangeInterval
For Comb As Integer = 0 To Combs - 1
ChangeCount -= 1
UpperLower = If(ChangeCount = 0, Not UpperLower, UpperLower)
If UpperLower Then
CombsArr(Comb) &= CharsArr(Chr).ToString.ToUpper
Else
CombsArr(Comb) &= CharsArr(Chr).ToString.ToLower
End If
If ChangeCount = 0 Then ChangeCount = ChangeInterval
Next
Next
'Test: Put all words into an HashSet to check for No duplicates
Dim HashWords As New HashSet(Of String)
For W As Integer = 0 To CombsArr.Length - 1
HashWords.Add(CombsArr(W))
Next
MsgBox("Hashset.Count:" & HashWords.Count.ToString & vbCrLf &
"CombsArr.Length:" & CombsArr.Length.ToString)
End Sub
答案 3 :(得分:0)
这是我对可能的解决方案的看法。
它并不像genepos的解决方案那么快,并且远不及Enigmativity的答案那么快,但它与genepos的答案相当。这个函数获取字符串的长度并迭代超过2到字符数的幂,我想到每个可能的状态 - 大写或小写是二进制。
所以..外部循环使用循环索引创建一个二进制字符串,其数字与字符串中的字符数相同。 1对应于大写,0对应于小写。
然后内部循环遍历二进制字符串和原始字符串,并根据二进制字符串中包含的内容构建一个包含大写和小写字符的新字符串。内部循环生成的字符串将添加到列表中,依此类推,直到外部循环完成,并从函数返回最终列表。
我非常确定可以优化此代码,但至少可以获得所有排列的排序列表。
这里有..
Private Function GetAllCasePermutations(originString As String) As List(Of String)
Dim resultList As New List(Of String)
Dim originLength As Integer = originString.Length
originString = originString.ToLower
For i As Integer = 1 To CInt(2 ^ originLength)
Dim binaryIndexString As String = Convert.ToString(i, 2)
binaryIndexString=binaryIndexString.PadLeft(originLength,"0")
Dim resultString As New StringBuilder
For j As Integer = 1 To originLength
If Mid(binaryIndexString, j, 1) = 1 Then
resultString.Append(Mid(originString, j, 1).ToUpper)
Else
resultString.Append(Mid(originString, j, 1).ToLower)
End If
Next
resultList.Add(resultString.ToString)
Next
Return resultList
End Function