Skip to content

Commit bd99401

Browse files
committed
Dec 3, 2018
1 parent 7b718be commit bd99401

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

709__easy__to-lower-case.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
3+
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
4+
5+
Example 1:
6+
7+
Input: "Hello"
8+
Output: "hello"
9+
Example 2:
10+
11+
Input: "here"
12+
Output: "here"
13+
Example 3:
14+
15+
Input: "LOVELY"
16+
Output: "lovely"
17+
18+
*/
19+
20+
/**
21+
*
22+
* O(n) time
23+
* O(n) space
24+
* Runtime: 48 ms, faster than 100.00%
25+
*/
26+
/**
27+
* @param {string} str
28+
* @return {string}
29+
*/
30+
var toLowerCase = function(str) {
31+
if (!str) return str;
32+
// Char Code A-Z => 65 - 90, a-z 97 - 122
33+
// A -> a need + 32 to char code of A
34+
let res = "";
35+
for (let i = 0; i < str.length; i++) {
36+
let cCode = str[i].charCodeAt(0);
37+
if (65 <= cCode && cCode <= 90) {
38+
res += String.fromCharCode(cCode + 32);
39+
} else {
40+
res += str[i];
41+
}
42+
}
43+
return res;
44+
};

760__easy__find-anagram-mappings.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
3+
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.
4+
5+
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.
6+
7+
These lists A and B may contain duplicates. If there are multiple answers, output any of them.
8+
9+
For example, given
10+
11+
A = [12, 28, 46, 32, 50]
12+
B = [50, 12, 32, 46, 28]
13+
We should return
14+
[1, 4, 3, 2, 0]
15+
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.
16+
Note:
17+
18+
A, B have equal lengths in range [1, 100].
19+
A[i], B[i] are integers in range [0, 10^5]
20+
21+
*/
22+
/**
23+
*
24+
* O(n) time
25+
* O(n) space
26+
*
27+
* Runtime: 52 ms, faster than 100.00%
28+
*/
29+
/**
30+
* @param {number[]} A
31+
* @param {number[]} B
32+
* @return {number[]}
33+
*/
34+
var anagramMappings = function(A, B) {
35+
if (!A || !B) return null;
36+
37+
let map = {}; // {} is faster than Map()
38+
for (let i = 0; i < B.length; i++) {
39+
if (!map[B[i]]) map[B[i]] = i;
40+
}
41+
42+
let res = new Array(A.length);
43+
for (let i = 0; i < A.length; i++) {
44+
res[i] = map[A[i]];
45+
}
46+
47+
return res;
48+
};

0 commit comments

Comments
 (0)