Skip to content

Instantly share code, notes, and snippets.

@josephmcasey
Last active April 16, 2024 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephmcasey/730ad5463aaece052adebb2fcdcafe20 to your computer and use it in GitHub Desktop.
Save josephmcasey/730ad5463aaece052adebb2fcdcafe20 to your computer and use it in GitHub Desktop.
LeetCode #760 - Find Anagram Mappings
int* anagramMappings(int* A, int ASize, int* B, int BSize, int* returnSize) {
int i,j=0;
returnSize=0;
int result=(int)malloc(sizeof(int)(ASize));
for(i=0;i<ASize;i++){
for(j=0;j<BSize;j++){
if((A+i)==(B+j)){
result[(*returnSize)++]=j;
break;
}
}
}
return result;
}
class Solution {
public:
vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
vector<int> res;
unordered_map<int,int> m;
int i = 0;
while(i < A.size())
m[B[i]] = i++;
i = 0;
while(res.size() < A.size())
res.push_back(m[A[res.size()]]);
return res;
}
};
public int[] AnagramMappings(int[] A, int[] B)
{
if(A == null || B == null || A.Length != B.Length)
return null;
Dictionary<int, int> BValues = new Dictionary<int, int>();
for(int i = 0 ; i < A.Length ; i ++)
AddOrUpdate(BValues, B[i], i);
int[] anagramMapping = new int[A.Length];
for(int i = 0 ; i < A.Length ; i++)
anagramMapping[i] = BValues[A[i]];
return anagramMapping;
}
private void AddOrUpdate(Dictionary<int, int> dictionary, int key, int val)
{
if(dictionary.ContainsKey(key))
dictionary[key] = val;
else
dictionary.Add(key, val);
}
func anagramMappings(A []int, B []int) []int {
var bMap map[int]int = make(map[int]int)
for index, num := range B {
bMap[num] = index
}
var res []int = make([] int, len(A))
for index, num := range A {
res[index] = bMap[num]
}
return res
}
class Solution {
public int[] anagramMappings(int[] A, int[] B) {
Map<Integer, Integer> D = new HashMap();
for (int i = 0; i < B.length; ++i)
D.put(B[i], i);
int[] ans = new int[A.length];
int t = 0;
for (int x: A)
ans[t++] = D.get(x);
return ans;
}
}
/**
* @param {number[]} A
* @param {number[]} B
* @return {number[]}
*/
function anagramMappings(A, B) {
if(A.length === 1 && B.length === 1)
{
return [ 0 ]
}
else
{
const map = new Map();
B.forEach( (item, index) => map.set(item, index))
return A.map( (item, index) => map.get(item))
}
}
class Solution {
fun anagramMappings(a: IntArray, b: IntArray): IntArray {
val bMap = HashMap<Int, MutableList<Int>>()
for ((index, bValue) in b.withIndex()) {
bMap.computeIfAbsent(bValue, { mutableListOf<Int>() } ).add(index)
}
val res = IntArray(a.size)
for ((index, aValue) in a.withIndex()) {
res.set(index, bMap[aValue]!!.removeAt(bMap[aValue]!!.size - 1))
}
return res
}
}
class Solution(object):
def anagramMappings(self, A, B):
D = {x: i for i, x in enumerate(B)}
return [D[x] for x in A]
def anagram_mappings(a, b)
b_map = Hash[b.collect.with_index { |val, i| [val, i] }]
a.map { |a_v| b_map[a_v]}
end
class Solution {
func anagramMappings(_ A: [Int], _ B: [Int]) -> [Int] {
var cached = [Int:Int]()
for (idx, val) in B.enumerated() {
cache[val] = idx
}
var mapped = Array<Int>(repeatElement(-1, count: A.count))
for (idx, val) in A.enumerated() {
mapped[idx] = cache[val] ?? -1
}
return mapped
}
}
@roshan-jha-23
Copy link

what if numbers are repated?
?

@D-Kumar19
Copy link

@roshan-jha-23 that constraint wasn't there which made it a little confusing but the numbers aren't duplicate here, Either, just store it inside a Set in a Map instead of an index. Like this:

HashMap<Integer, HashSet<Integer>> result= new HashMap<Integer, HashSet<Integer>>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment