Find Anagrams Mapping
Map ๋ชจ๋ฅธ๋ค๊ณ ๊ฒ๋จน์ง ๋ง๊ธฐ. Map์ผ๋ก ํธ๋ ๋ฌธ์ ์น๊ณ ๋ ์ฌ์ด ๋ฌธ์ !
Example Input : int[ ] A = {11,27,45,31,50}; int[ ] B = {50,11,31,45,27}; Output : [1 4 3 2 0]
์๋ฃ๊ตฌ์กฐ : Map ์ด์ฉ
์๊ณ ๋ฆฌ์ฆ 1. ๋ด์ ๊ทธ๋ฆ ์์ฑ : Map๊ณผ ์ ๋ต ๋ฐฐ์ด 2. Map์ B๋ฐฐ์ด์ ๋ด๋๋ค. (key,value) = (B[i],i). ์๋ํ๋ฉด ์ฐพ๊ณ ์ํ๋ ๊ฒ์ ์์๊ฐ์ด๊ณ , ์์๊ฐ์ด ๋ช๋ฒ์งธ ์ธ๋ฑ์ค์ธ์ง(value)๋ฅผ ์ป๊ธฐ ์ํด! 3. Map์์ A์ ์์๊ฐ ๋ช๋ฒ์งธ์ธ์ง๋ฅผ ์ ๋ต ๋ฐฐ์ด์ ๋ฃ๋๋ค. ์ ๋ต ๋ฐฐ์ด์ ๋ฆฌํดํ๋ค.
public static void main(String[] args) {
int[] A = {12, 28, 46, 32, 50};
int[] B = {50, 12, 32, 46, 28};
int [] result = anagramMappings(A, B);
print(result);
}
public static void print(int[] result) {
for(int i=0; i< result.length; i++) {
System.out.print(result[i]+" ");
}
}
public static int[] anagramMappings(int[] A, int[] B) {
int leng = A.length;
int[] res = new int[leng];
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0;i<leng;i++){
map.put(B[i],i);
}
for(int i=0;i<leng;i++){
res[i] = map.get(A[i]);
}
return res;
}
Last updated
Was this helpful?