Re: coding an anagram function



Unless I'm mistaken, the efficiency of Alan's idea can be further
enhanced by...

1. Exiting the inner loop after the condition is first satisfied, since
continuing is a waste of time after the common character has been
located and removed.

2. Exiting the outer loop if a complete run through the inner loop did
not satisfy the condition at all, then a letter in the first string was
not found in the second string and the two are not anagrams.

Public Function ISANAGRAM(TEXT1 As String, TEXT2 As String) As Boolean
Application.Volatile
TEXT1 = UCase(Replace(TEXT1, " ", ""))
TEXT2 = UCase(Replace(TEXT2, " ", ""))
If Len(TEXT1) <> Len(TEXT2) Then
ISANAGRAM = False
Exit Function
End If
Dim I As Long
Dim J As Long
Dim K As Long
For I = 1 To Len(TEXT1)
K = Len(TEXT2)
For J = 1 To Len(TEXT2)
If Mid(TEXT2, J, 1) = Mid(TEXT1, I, 1) Then
Let TEXT2 = Left(TEXT2, J - 1) & Right(TEXT2, Len(TEXT2) - J)
Exit For
End If
Next J
If K = Len(TEXT2) Then
ISANAGRAM = False
Exit Function
End If
Next I
ISANAGRAM = True
End Function

Ken Johnson

.



Relevant Pages

  • Re: Type Error in For Each Worksheet Loop
    ... Dim wb As Workbook ... > and moves to the next worksheet with no error. ... > the inner loop but fails when it hits the 'Next ws'. ... > Dim wsName As String ...
    (microsoft.public.excel.programming)
  • Re: Manage groups of WS
    ... the inner loop "J" is handled in a sub-process), but I shall hang on to yours ... Select Case intSheetsIndex ... Dim vSheets As Variant ...
    (microsoft.public.excel.programming)
  • Re: Programmers unpaid overtime.
    ... > that most programs read multi-GB files into one enormous string, ... > call Split on that string, perhaps multiple times, within loops, ... > a string concatenation within its inner loop. ... > Micro$haft's typical QuickSort implementations? ...
    (comp.programming)
  • Re: problem with calling subprogram in a nested for-loop
    ... The outer loop works but the inner loop runs only one time ... are assigning a string to $ppm_number, but using it as a number later. ... # 5) generate pmm_G by cropping the source image ... chop $sourcefile_name; ...
    (perl.beginners)
  • Re: Do while loop error....
    ... the inner loop was highlighted (compile error), ... Sub MapPrice() ... Dim selection_v, selection_p As Range ...
    (microsoft.public.excel.programming)