Two words are anagrams if and only if they contain the exact same letters with the exact same frequency (for example, “name” and “mean” are anagrams, but “red” and “deer” are not).
Given two strings S1 and S2, which each only contain the lowercase letters a through z, write a program to determine if S1 and S2 are anagrams.
Rule: You can use any language you want. Best algorithm will win.
Module modAnagram
Public Sub Main(ByVal cmdArgs() As String)
'sort the strings to arrange the chars
Dim s1, S2 As String
s1 = SortString(cmdArgs(0))
S2 = SortString(cmdArgs(1))
Dim bAnagramTrue As Boolean
bAnagramTrue = True
If s1.Length = S2.Length Then
For nCounter As Integer = 0 To s1.Length - 1
If s1.Substring(nCounter, 1) <> S2.Substring(nCounter, 1) Then
bAnagramTrue = False
nCounter = s1.Length
End If
Next
Else
bAnagramTrue = False
End If
If bAnagramTrue Then
Console.WriteLine("The words " + cmdArgs(1) + " & " + cmdArgs(2)+ " are anagrams")
Else
Console.WriteLine("The words " + cmdArgs(1) + " & " + cmdArgs(2) + " are not anagrams")
End If
End Sub
Function SortString(S1 As String) As String
Dim chrArray() As Char
chrArray = S1.ToCharArray()
Array.Sort(chrArray)
'Console.WriteLine(New String(chrArray))
SortString = New String(chrArray)
End Function
End Module
My only suggest would be to pass in two character arrays to SortString by reference and then after sorting use them in your main loop for comparison. This way you won’t have to convert them to string again in SortString function and second you won’t have to use Substring in the main method. It will double the performance of this algorithm.
yo programming genuises, help me find a tool that converts PDF to WORD doc,
conditions: No online tools, need something to be downloaded on my machine. Paid for tools are welcome. Crackable versions even better… Torrent links, direct download links, all cool.