Valid letter heterotopic (Title No. 242)

The title

Given two strings s and t, write a function to determine whether t is an alphabetic allotopic of S.

Example 1:

Input: s = "anagram", t = "nagaram" Output: trueCopy the code

Example 2:

Input: s = "rat", t = "car" output: falseCopy the code

Note: You can assume that strings contain only lowercase letters.

Advanced: What if the input string contains Unicode characters? Can you adjust your solution to this situation?

link

Leetcode-cn.com/problems/va…

explain

This problem originally only wanted to use Map or object to operate, the idea is relatively simple, not to repeat here. The remaining method is to sort the two strings and then compare whether the sorting results are equal or not. This method is relatively simple, but not recommended because of poor performance.

Your own answer (object)

var isAnagram = function(s, t) { var obj = new Map() var sArr = s.split('') var tArr = t.split('') if (sArr.length ! == tArr.length) return false for (let index = 0; index < sArr.length; index++) { const item = sArr[index]; if (! obj.has(item)) { obj.set(item, 1) } else { obj.set(item, obj.get(item) + 1) } } for (let index = 0; index < tArr.length; index++) { const item = tArr[index]; if (! obj.has(item)) { return false } else { var value = obj.get(item) switch (value) { case 0: return false case 1: obj.delete(item) break default: obj.set(item, obj.get(item) - 1) break } } } if (obj.size === 0)return true return false };Copy the code

You can use either an object or a Map, but there’s a slight difference in performance

Your own answers (sorted twice)

var isAnagram = function(s, t) {
  return s.split('').sort().join('') === t.split('').sort().join('')
};
Copy the code

There’s nothing to be said for all of this. One line of code will do, and toString() will do.

Better all answers

var isAnagram = function(s, t) { return s.length === t.length && Array.from(Array.from(s).reduce((h, v, i) => (h.set(v, (h.get(v) || 0) + 1), h.set(t[i], (h.get(t[i]) || 0) - 1), h), new Map).values()).every(v => ! v) };Copy the code

This is reduce, it looks more upscale

Sum of two numbers (Item no. 1)

The title

Given an integer array nums and an integer target value target, find the two integers in the array and the target values and return their array subscripts.

You can assume that there is only one answer for each type of input. However, the same element in an array cannot be used twice.

You can return the answers in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9 output: [0,1]Copy the code

Example 2:

Input: nums = [3,2,4], target = 6Copy the code

Example 3:

Input: nums = [3,3], target = 6Copy the code

Tip:

  • 2 <= nums.length <= 103
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • There can only be one valid answer

link

Leetcode-cn.com/problems/tw…

explain

There’s nothing to explain. Just look at the code

My own answer

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
  var obj = new Map()
  for (let index = 0; index < nums.length; index++) {
    var item = nums[index];
    if (obj.has(item)) {
      return [obj.get(item), index]
    }
    obj.set(target - item, index)
  }
};
Copy the code





PS: To view previous articles and titles, click on the links below:

Here are the categories by date 👇

Front end Brush – Table of Contents (date category)

After some friends remind, the feeling should also be classified according to the question type here is classified according to the question type 👇

Front End Brush problem path – Table of Contents (Question type classification)

If you are interested, you can also check out my personal homepage at 👇

Here is RZ