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].
class Solution { public int[] anagramMappings(int[] A, int[] B) { Mapmap = 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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72319.html
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...
摘要:解題思路,就是只順序不同但個數相同的字符串,那我們就可以利用的思想來比較每個字符串中字符出現的個數是否相等。 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...
摘要:題目要求思路和代碼這是一個簡單的雙指針問題,即左指針指向可以作為起點的子數組下標,右指針則不停向右移動,將更多的元素囊括進來,從而確保該左右指針內的元素是目標數組的一個兄弟子數組即每個字母的個數均相等左指針記錄每個字母出現的次數拷貝一個 題目要求 Given a string s and a non-empty string p, find all the start indices ...
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 ...
摘要:排序法復雜度時間空間思路因為變形詞兩個單詞對應字母出現的次數都相同,所以如果將兩個單詞按字母順序排序,肯定會變為一個字符串,如果字符串不相同,則不是變形詞。 Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = a...
閱讀 2993·2021-10-13 09:39
閱讀 2694·2021-09-27 13:34
閱讀 2031·2019-08-30 15:55
閱讀 3260·2019-08-30 15:43
閱讀 3631·2019-08-30 11:16
閱讀 1748·2019-08-26 18:28
閱讀 1285·2019-08-26 13:56
閱讀 915·2019-08-26 13:35