| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Nov 2016 10:41 AM |
| one way would be to put the characters of the strings in abc order and check if their the same, def. more efficient than getting all permutations |
|
|
| Report Abuse |
|
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Nov 2016 10:44 AM |
;) im magician look
is this your card |
|
|
| Report Abuse |
|
|
sayhisam1
|
  |
| Joined: 25 Nov 2009 |
| Total Posts: 2092 |
|
|
| 09 Nov 2016 11:31 AM |
Another way is to start at each letter of the first string, go through the other string, check if letter is there, and then remove the letter from the array if it is.
|
|
|
| Report Abuse |
|
|
sayhisam1
|
  |
| Joined: 25 Nov 2009 |
| Total Posts: 2092 |
|
|
| 09 Nov 2016 11:35 AM |
or just check the number of occurrences of each letter.
|
|
|
| Report Abuse |
|
|
|
| 09 Nov 2016 11:39 AM |
| I really like ##### solution actually. It might be better to save you writing out ## ######### use string.byte and sort of size order ################ // Private Drive - Residents Only |
|
|
| Report Abuse |
|
|
|
| 09 Nov 2016 11:53 AM |
here u go
function anagram(s1,s2) local matched = false local string1=(s1):lower():gsub("%s+","") local string2=(s2):lower():gsub("%s+","") for i=1,#string1 do local _,m1 = string2:gsub(string1:sub(i,i),"") local _,m2 = string1:gsub(string1:sub(i,i),"") matched=(m1==m2) if not matched then break end end return matched end
print(anagram("anagram","nag a ram")) print(anagram("cool","no")) |
|
|
| Report Abuse |
|
|
|
| 09 Nov 2016 11:56 AM |
| it just checks if the amount of each letter is consistent |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
| |
|
| |
|