Could you optimize the below code?: (String anagrams)

load(
local!str1: "abca",
local!str2: "ABAC",
local!splitstr1: apply(
fn!index(
lower(local!str1),
_,
""
),
enumerate(
len(
local!str1
)
) + 1
),
local!splitstr2: apply(
fn!index(
lower(local!str2),
_,
""
),
enumerate(
len(
local!str2
)
) + 1
),
local!checkData: and(
a!forEach(
items: local!splitstr2,
expression: contains(
local!splitstr1,
fv!item
)
)
),
if(
local!checkData,
"Anagram",
"Naah!!"
)
)

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Associate Developer

    How about this?

    load(
    local!str1: "abca",
    local!str2: "ABAC",

    local!unicode1:code(lower(local!str1)),
    local!unicode2:code(lower(local!str2)),


    if(
    contains(local!unicode1,local!unicode2),
    "Anagram",
    "Naah!!"
    )
    )

  • I believe, for an anagram, the letters should not only match but appear exactly the same number of times in both the words in different order. Check this out.:

    load(
      local!str1: "abca",
      local!str2: "abA",
      
      local!unicode1:code(lower(local!str1)),
      local!uniqueUnicode1: union(local!unicode1,local!unicode1),
    local!unicode2:code(lower(local!str2)),
    local!uniqueUnicode2:union(local!unicode2,local!unicode2),
    
    local!str1Details:
    
    a!forEach(local!uniqueUnicode1, 
    {character:fv!item,times:count(wherecontains(fv!item,local!unicode1))}),
    
    local!str2Details:a!forEach(local!uniqueUnicode2, 
    {character:fv!item,times:count(wherecontains(fv!item,local!unicode2))}),
    
    local!isMatch:a!forEach(local!str1Details,
        
      fv!item.times = index(local!str2Details.times, wherecontains(fv!item.character,local!str2Details.character),{})
         
      ),
      if(contains(toboolean(local!isMatch),false()),"Not an anagram","anagram")
    )

  • 0
    Certified Associate Developer
    in reply to ankitab0001

    Thanks for correcting the solution Ankita, I wasn't aware about Anagram functionality and my assumption was its just the character match that is required.

Reply Children
No Data