Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,923 questions

51,856 answers

573 users

How to check if a string can split into 4 distinct substrings in VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Function canSplitInto4DistinctSubstrings(ByVal str As String) As Boolean
        Dim size As Integer = str.Length

        If size < 4 Then
            Return False
        End If

        For i As Integer = 1 To size - 2 - 1
            For j As Integer = i + 1 To size - 1 - 1
                For k As Integer = j + 1 To size - 1
                    Dim part1 As String = str.Substring(0, i)
                    Dim part2 As String = str.Substring(i, j - i)
                    Dim part3 As String = str.Substring(j, k - j)
                    Dim part4 As String = str.Substring(k, size - k)

                    If part1.Length > 0 AndAlso part2.Length > 0 AndAlso part3.Length > 0 AndAlso part4.Length > 0 Then
                        Dim unique_parts As HashSet(Of String) = New HashSet(Of String)()
                        unique_parts.Add(part1)
                        unique_parts.Add(part2)
                        unique_parts.Add(part3)
                        unique_parts.Add(part4)

                        If unique_parts.Count = 4 Then
                            Console.WriteLine(part1 & " " & part2 & " " & part3 & " " & part4)
                            Return True
                        End If
                    End If
                Next
            Next
        Next

        Return False
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim str As String = "AlbusDumbledore"

        If canSplitInto4DistinctSubstrings(str) Then
            Console.Write("yes")
        Else
            Console.Write("no")
        End If
    End Sub
End Class




' run:
'
' A l b usDumbledore
' yes
'

 



answered Feb 15, 2024 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Function canSplitInto4DistinctSubstrings(ByVal str As String) As Boolean
        Dim size As Integer = str.Length

        If size < 4 Then
            Return False
        End If

        For i As Integer = 2 To size - 2 - 1
            For j As Integer = i + 2 To size - 1 - 1
                For k As Integer = j + 2 To size - 1
                    Dim part1 As String = str.Substring(0, i)
                    Dim part2 As String = str.Substring(i, j - i)
                    Dim part3 As String = str.Substring(j, k - j)
                    Dim part4 As String = str.Substring(k, size - k)

                    If part1.Length > 0 AndAlso part2.Length > 0 AndAlso part3.Length > 0 AndAlso part4.Length > 0 Then
                        Dim unique_parts As HashSet(Of String) = New HashSet(Of String)()
                        unique_parts.Add(part1)
                        unique_parts.Add(part2)
                        unique_parts.Add(part3)
                        unique_parts.Add(part4)

                        If unique_parts.Count = 4 Then
                            Console.WriteLine(part1 & " " & part2 & " " & part3 & " " & part4)
                            Return True
                        End If
                    End If
                Next
            Next
        Next

        Return False
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim str As String = "AlbusDumbledore"

        If canSplitInto4DistinctSubstrings(str) Then
            Console.Write("yes")
        Else
            Console.Write("no")
        End If
    End Sub
End Class



' run:
'
' Al bu sD umbledore
' yes
'

 



answered Feb 15, 2024 by avibootz
...