资讯专栏INFORMATION COLUMN

[LeetCode] 760. Find Anagram Mappings

caozhijian / 1623人阅读

Problem

Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order of the elements in A.

We want to find an index mapping P, from A to B. A mapping P[i] = j means the ith element in A appears in B at index j.

These lists A and B may contain duplicates. If there are multiple answers, output any of them.

For example, given

A = [12, 28, 46, 32, 50]
B = [50, 12, 32, 46, 28]
We should return
[1, 4, 3, 2, 0]
as P[0] = 1 because the 0th element of A appears at B[1], and P[1] = 4 because the 1st element of A appears at B[4], and so on.
Note:

A, B have equal lengths in range [1, 100].
A[i], B[i] are integers in range [0, 10^5].

Solution
class Solution {
    public int[] anagramMappings(int[] A, int[] B) {
        Map map = new HashMap<>();
        for (int i = 0; i < B.length; i++) {
            map.put(B[i], i);
        }
        for (int i = 0; i < A.length; i++) {
            A[i] = map.get(A[i]);
        }
        return A;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72319.html

相关文章

  • [LeetCode] 438. Find All Anagrams in a String [滑动窗

    Problem Given a string s and a non-empty string p, find all the start indices of ps anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be...

    muzhuyu 评论0 收藏0
  • [LeetCode]Find All Anagrams in a String

    摘要:解题思路,就是只顺序不同但个数相同的字符串,那我们就可以利用的思想来比较每个字符串中字符出现的个数是否相等。 Find All Anagrams in a StringGiven a string s and a non-empty string p, find all the start indices of ps anagrams in s. Strings consists of...

    niceforbear 评论0 收藏0
  • leetcode438. Find All Anagrams in a String

    摘要:题目要求思路和代码这是一个简单的双指针问题,即左指针指向可以作为起点的子数组下标,右指针则不停向右移动,将更多的元素囊括进来,从而确保该左右指针内的元素是目标数组的一个兄弟子数组即每个字母的个数均相等左指针记录每个字母出现的次数拷贝一个 题目要求 Given a string s and a non-empty string p, find all the start indices ...

    wangbinke 评论0 收藏0
  • [LeetCode] Group Anagram

    Problem Given an array of strings, group anagrams together. Example: Input: [eat, tea, tan, ate, nat, bat], Output: [ [ate,eat,tea], [nat,tan], [bat] ] Note: All inputs will be in lowercase.The ...

    kid143 评论0 收藏0
  • [Leetcode] Valid Anagram 验证变形词

    摘要:排序法复杂度时间空间思路因为变形词两个单词对应字母出现的次数都相同,所以如果将两个单词按字母顺序排序,肯定会变为一个字符串,如果字符串不相同,则不是变形词。 Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = a...

    justjavac 评论0 收藏0

发表评论

0条评论

caozhijian

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<