Post History
JavaScript (Node.js), 131 bytes Returns true and false g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c) f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).l...
Answer
#2: Post edited
# [JavaScript (Node.js)], 129 bytesReturns truthy and falsey values- ```javascript
- g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c)
- f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).length)<2&&x+y
- ```
```ADOBE ABODE -> 0 (proper anagram)TUPLE TULIP -> 2 (near-anagram)ABCDE DADBC -> 2 (two Ds)BAR BARN -> 1 (one extra letter)BARN BARREN -> false (too different)```[Try it online!][TIO-kl91jx22]- ## Explanation
- `g` computes a sort of set (Array?) difference between `t` and `s`, running through the characters of `s` and removing any matches in `t`, then returning the remaining characters in `t`.
`f` calls `g` to compute both `s-t` and `t-s` and checks that both are less than 2 (i.e. have only one letter difference either way). The third check, which gets returned, is the sum of the difference to check that there *is* a difference, which I needed to add in to make perfect anagrams return zero.[^1][^1]: Without the restriction on perfect anagrams being falsey, I could have removed all of the x and y logic to get a [116 char solution] (that also actually returns booleans!)- [JavaScript (Node.js)]: https://nodejs.org
[TIO-kl91jx22]: https://tio.run/##dYxBC4IwAIXv/opOutEa0bUmbOkhEBXRU3iQOZchLtoQ/fWmdqmw03u89/Hdi67Q/Fk/zK5VpRhHSYBGBnFyxRibHBIXzE3nuFJPv@A30E3TQDiu21L0UQU6iDjWj6bmAgxocMkewmmCVrWoZkNP5FJxI1ppbvB0sO3JIYFB@mvst8PIVatVI3CjJKiAQ1nk@Q7aONSLmO9AeLR@iDSLg4VIs@ASrxGUnd8Oj3rsvEYwmsz/FOGfO/HDT2J8AQ "JavaScript (Node.js) – Try It Online"- [116 char solution]: https://tio.run/##dYxBC4IwAIXv/opOusEa0bUmbOohEA3RU3iQOachTtyQ/PVmdijCTu/x3sd3L8ZC86Hpzb5TpZhnSYBGBnFywxibHBIXvJrOcaWGoOA1GJdpIhw3XSkecQVGiDjWfdtwASY0ueQA4TJBq1pVi0GuiVvRSVOfj7YtgUH6M8xcdVq1ArdKggo4lMV@4KCdQ/2YBQ6EJ@uHSLNruBJpFl6uWwRl3tvhU595WwSjyetfIvpzJ0H0TcxP "JavaScript (Node.js) – Try It Online"
- # [JavaScript (Node.js)], 131 bytes
- Returns true and false
- ```javascript
- g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c)
- f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).length)<2&&x+y
- ```
- [Try it online!][TIO-klb94gwz]
- ## Explanation
- `g` computes a sort of set (Array?) difference between `t` and `s`, running through the characters of `s` and removing any matches in `t`, then returning the remaining characters in `t`.
- `f` calls `g` to compute both `s-t` and `t-s` and checks that both are less than 2 (i.e. have only one letter difference either way). Then it checks the sum of the difference is non-zero to check that there *is* a difference, which I needed to add in to make perfect anagrams false.[^1]
- [^1]: Without the restriction on perfect anagrams being falsey, I could have removed all of the x and y logic to get a [116 char solution]
- [JavaScript (Node.js)]: https://nodejs.org
- [TIO-klb94gwz]: https://tio.run/##dYxBC4IwAIXv/YpOudEa0bUmbLlDIBqhp@ggcy5DNmlD9Neb2qXCTu/x3sf3yJrMimdZu402uex7RYBFDglyxRi7GyQ@GJu94cI8eSbuoBmmjghc6ly2cQEaiAS2dVUKCTrU@WQL4TDBRTGpRkNL1FRxJbVyd3jYrVaDQwGH7NfYrjt/2wujrakkrowCBfAoiwPuoaVHg5hxD8L94odI0nM4EUkans5zBGXHtyOgATvOEYxexn@I6M994dEn0b8A "JavaScript (Node.js) – Try It Online"
- [116 char solution]: https://tio.run/##dYxBC4IwAIXv/opOusEa0bUmbOohEA3RU3iQOachTtyQ/PVmdijCTu/x3sd3L8ZC86Hpzb5TpZhnSYBGBnFywxibHBIXvJrOcaWGoOA1GJdpIhw3XSkecQVGiDjWfdtwASY0ueQA4TJBq1pVi0GuiVvRSVOfj7YtgUH6M8xcdVq1ArdKggo4lMV@4KCdQ/2YBQ6EJ@uHSLNruBJpFl6uWwRl3tvhU595WwSjyetfIvpzJ0H0TcxP "JavaScript (Node.js) – Try It Online"
#1: Initial revision
# [JavaScript (Node.js)], 129 bytes Returns truthy and falsey values ```javascript g=(s,t,c=[...t])=>([...s].forEach(v=>(y=c.indexOf(v),c.splice(y,y>=0))),c) f=(s,t)=>(x=g(s,t).length)<2&&(y=g(t,s).length)<2&&x+y ``` ``` ADOBE ABODE -> 0 (proper anagram) TUPLE TULIP -> 2 (near-anagram) ABCDE DADBC -> 2 (two Ds) BAR BARN -> 1 (one extra letter) BARN BARREN -> false (too different) ``` [Try it online!][TIO-kl91jx22] ## Explanation `g` computes a sort of set (Array?) difference between `t` and `s`, running through the characters of `s` and removing any matches in `t`, then returning the remaining characters in `t`. `f` calls `g` to compute both `s-t` and `t-s` and checks that both are less than 2 (i.e. have only one letter difference either way). The third check, which gets returned, is the sum of the difference to check that there *is* a difference, which I needed to add in to make perfect anagrams return zero.[^1] [^1]: Without the restriction on perfect anagrams being falsey, I could have removed all of the x and y logic to get a [116 char solution] (that also actually returns booleans!) [JavaScript (Node.js)]: https://nodejs.org [TIO-kl91jx22]: https://tio.run/##dYxBC4IwAIXv/opOutEa0bUmbOkhEBXRU3iQOZchLtoQ/fWmdqmw03u89/Hdi67Q/Fk/zK5VpRhHSYBGBnFyxRibHBIXzE3nuFJPv@A30E3TQDiu21L0UQU6iDjWj6bmAgxocMkewmmCVrWoZkNP5FJxI1ppbvB0sO3JIYFB@mvst8PIVatVI3CjJKiAQ1nk@Q7aONSLmO9AeLR@iDSLg4VIs@ASrxGUnd8Oj3rsvEYwmsz/FOGfO/HDT2J8AQ "JavaScript (Node.js) – Try It Online" [116 char solution]: https://tio.run/##dYxBC4IwAIXv/opOusEa0bUmbOohEA3RU3iQOachTtyQ/PVmdijCTu/x3sd3L8ZC86Hpzb5TpZhnSYBGBnFywxibHBIXvJrOcaWGoOA1GJdpIhw3XSkecQVGiDjWfdtwASY0ueQA4TJBq1pVi0GuiVvRSVOfj7YtgUH6M8xcdVq1ArdKggo4lMV@4KCdQ/2YBQ6EJ@uHSLNruBJpFl6uWwRl3tvhU595WwSjyetfIvpzJ0H0TcxP "JavaScript (Node.js) – Try It Online"