Last active
April 16, 2024 04:15
-
-
Save josephmcasey/730ad5463aaece052adebb2fcdcafe20 to your computer and use it in GitHub Desktop.
LeetCode #760 - Find Anagram Mappings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def anagramMappings(self, A, B): | |
D = {x: i for i, x in enumerate(B)} | |
return [D[x] for x in A] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
what if numbers are repated?
?