VB.NETで文字列内の最初の非繰り返し文字を検索する

私はVB.NETで文字列の最初の非反復文字を見つけるための関数を書く必要があります。下のコードは大丈夫ですか?

Module Module2

Sub Main()
    ' Unit test
    ' Pass string as argument.
    Console.WriteLine(nonRepeat("BBEEXEE")

End Sub

Function nonRepeat(ByVal aString As String) As String

    Dim repeated As Integer = 0

    For i = 0 To aString.Length-1

        repeated = 0

        For j = 0 To aString.Length-1

            ' If inner and outer For loops are on the same index then
            ' inner For loop moves to next index and compares character 
            ' with outer For loop character.
            ' If characters are equal then set repeated = 1 and Exit inner For loop.
            ' Otherwise, continue to find repeating character
            ' If reached end of string without finding repeating character
            ' then non-repeating character has been found and is returned.

            If ((i <> j) AndAlso (aString(i) = aString(j))) Then

                ' Found repeating character 
                repeated = 1

                Exit For

            End If

        Next

        If (repeated = 0) Then

            ' Found first non-repeating character
            Return aString(i)

        End If
    Next

    Return ("No Non-Reapeating character!")

End Function

End Module
0

3 答え

linqのビットがこれよりずっと短くなる可能性があります。

Imports System.Linq

Module Module1

  Sub Main()
    Console.WriteLine(FirstCharacterToNotRepeat(Nothing))
    Console.WriteLine(FirstCharacterToNotRepeat(""))
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEXEE"))
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEEE"))
    Console.WriteLine(FirstCharacterToNotRepeat("XBBEEEE"))
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEEEX"))
    Console.WriteLine(FirstCharacterToNotRepeat("BBEEXEEACEED"))
    Console.ReadLine()
  End Sub

  Private Function FirstCharacterToNotRepeat(ByVal input As String) As String
    If String.IsNullOrEmpty(input) Then Return String.Empty
    Return (input.GroupBy(Function(x) x).Where(Function(x) x.Count = 1).Select(Function(x) x.First))(0)
  End Function
End Module
1
追加された
+1ラムダをうまく使う!
追加された 著者 Wade73,

いいえ、ループで処理するデータが不足するため、コードで例外がスローされます。ループは.Length -1で終了する必要があります。さもなければ、それは働くはずです。

ただし、効率を高め、エッジケースを処理することもできます。

    ' Don't bother checking if the string is empty
    If Not String.IsNullOrEmpty(asString) Then
        ' If the string is only a single character, just return it
        If asString.Length = 1 Then
            Return asString
        End If

        ' Create a collection that records the number of occurences for each character
        Dim cCharacterCounts As New System.Collections.Generic.Dictionary(Of Char, Integer)

        For Each cCharacter As Char In asString
            If cCharacterCounts.ContainsKey(cCharacter) Then
                ' If the character exists, increment its count
                cCharacterCounts(cCharacter) += 1
            Else
                ' Otherwise record the character as a new entry, initializing the count to 1
                cCharacterCounts.Add(cCharacter, 1)
            End If
        Next

        ' Now find the first character which only has a single count. This will be the first non-repeating value.
        For Each cCharacter As Char In cCharacterCounts.Keys
            If cCharacterCounts(cCharacter) > 1 Then
                Return cCharacter.ToString()
            End If
        Next
    End If

    ' Handle the case in which there is no non-repeating character
    Return String.Empty
1
追加された
[OK]を、コードを更新しました。他のすべては大丈夫ですか?
追加された 著者 Bruno,
はい、そうです。私は答えにオプションのデザインを追加しました。
追加された 著者 competent_tech,
Dim str As String = "BBEEXEE"
    For Each ch As Char In str
        If str.Contains(ch & ch) = False Then
            'first non-repeating
            MsgBox("First non-repeating character is: " & ch)
            Exit For
        End If
    Next
0
追加された