• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

anagram string (1 Viewer)

fuad

Newcomer
Joined
Aug 9, 2020
Messages
4
Reaction score
2
Age
31
Location
israel
if i need to check if the array is anagram or if both array are anagram i just but each letter in a first array in hash with key that is the number of how much it in the index and check from the other array if the first index in the hash..

her i do'nt know from where to start cuz there is no hash map her so it make the solution difficult!
in my julia solution
this is the final code
Lua:
function anagrams(a,b)
    res = zeros(Int,length(a))
    for i=1:length(a)
        a1 = sort(collect(a))
        b1 = sort(collect(b))
        for j=1:min(length(a1),length(b1))
            if length(a1) <= length(b1)
                if a1[j] in b1
                    res +=1
                    j +=1
                else
                    j+=1
                end
            else
                if b1[j] in a1
                    res +=1
                    j +=1
                else
                    j+=1
                end
            end
        end
    end
    return res
end

a = ["teewaa" , "ategh" , "simplefo"]
b = ["atwe" , "atcgh" , "fore"]

println(anagrams(a,b))

any one know how to convert it to lua ..
 

fuad

Newcomer
Joined
Aug 9, 2020
Messages
4
Reaction score
2
Age
31
Location
israel
Lua:
function anagrams(a, b)
    a1 = {}
    for i = 1, #a do --3times
        for j=1,#a do --3,3,8
            t = string.sub(a,j,j) -- i try to do that,  but ,, how to complete
            if -- ?
                print(t)
            end
        end
    end
end

a = {"tee" , "waa" ,"simplefo"}
b = {"atwe" , "atcgh" , "fore"}

print(anagrams(a,b))
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
69
Reaction score
27
Location
Victoria BC
Website
winlua.net
Not sure what you're trying to do, but you can't use `string.sub` on "a" because it's a table. You can use `string.sub` on an element of "a":

Lua:
--I changed the name of your inputs
function anagrams(t1, t2)
    a = t1[1] --turn this assignment into a loop...
    for i = 1, #a do --3times
        for j=1,#a do --3,3,8
            t = string.sub(a,j,j)
                print(t)
        end
    end
end

a = {"tee" , "waa" ,"simplefo"}
b = {"atwe" , "atcgh" , "fore"}

print(anagrams(a,b))
 
Top