Solution of Weekly Contest 170

[Easy] 1309. Decrypt String from Alphabet to Integer Mapping

Given a string s formed by digits ('0' - '9') and '#' . We want to map s to English lowercase characters as follows:

  • Characters ('a' to 'i') are represented by ('1' to '9') respectively.
  • Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.

Return the string formed after mapping. It’s guaranteed that a unique mapping will always exist.

Example

1
2
3
Input: s = "10#11#12"
Output: "jkab"
Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".

Solution

We only have to care about i + 2 and see if it is #, if so, we can get the number and then 'j' + (number - 10) , if not, we just use one character each time, and then 'a' + (number - 1).But the move would be different, it moves i + 3, i + 1 separately.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// time: O(n) space: O(n)
public String freqAlphabets(String s) {
// check the pos of i + 2 equals # or not
if (s == null || s.length() == 0) return "";
StringBuilder sb = new StringBuilder();
int index = 0;
while (index < s.length()) {
if (index + 2 < s.length() && s.charAt(index + 2) == '#') {
char ch = 'j';
ch += (Integer.parseInt(s.substring(index, index + 2)) - 10);
sb.append(String.valueOf(ch));
index += 3;
} else {
char ch = 'a';
ch += (Integer.parseInt(s.substring(index, index + 1)) - 1);
sb.append(String.valueOf(ch));
index++;
}
}
return sb.toString();
}

[Medium] 1310. XOR Queries of a Subarray

Given the array arr of positive integers and the array queries where queries[i] = [Li, Ri], for each query i compute the XOR of elements from Li to Ri (that is, arr[Li] xor arr[Li+1] xor ... xor arr[Ri] ). Return an array containing the result for the given queries.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
Output: [2,7,14,8]
Explanation:
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8

Solution

Use prefix strategy and we maintain the arr to store the prefix XOR value. Also, we have to use the a ^ b ^ b = a to solve this problem. For example, we have the arr [a, b, c, d, e, f]. And if we have to calculate [4,5] in [0, 5]. And we can get the prefix[4] = abcd and prefix[6] = abcdef and we get ef.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// time:O(n) space:O(n)
public int[] xorQueries(int[] arr, int[][] queries) {
int n = arr.length;
int[] xorPrefix = new int[n + 1];
for (int i = 1; i <= n; i++) {
xorPrefix[i] = xorPrefix[i - 1] ^ arr[i - 1];
}
int[] res = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int[] query = queries[i];
res[i] = xorPrefix[query[0]] ^ xorPrefix[query[1] + 1];
}
return res;
}

[Medium] 1311. Get Watched Videos by Your Friends

There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.

Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.

Example

1
2
3
4
5
6
7
8
9
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2

Solution

We add the id into the queue and see it’s friends and iterate k levels. At the end we get person we need and see the moveis they watched and sort it.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// time:O(nlogn) space:O(n)
public List<String> watchedVideosByFriends(List<List<String>> watchedVideos, int[][] friends, int id, int level) {
int n = friends.length;
boolean[] visited = new boolean[n];
visited[id] = true;
Queue<Integer> queue = new LinkedList<>();
queue.offer(id);
// 相当于每次替换当前层。
for (int i = 0; i < level; i++) {
Queue<Integer> newQ = new LinkedList<>();
for (int friend : queue) {
for (int adj : friends[friend]) {
if (!visited[adj]) {
visited[adj] = true;
newQ.offer(adj);
}
}
}
queue = newQ;
}

HashMap<String, Integer> map = new HashMap<>();
for (int person : queue) {
for (String video : watchedVideos.get(person)) {
map.put(video, map.getOrDefault(video, 0) + 1);
}
}
List<String> res = new ArrayList<>(map.keySet());
res.sort((a, b) -> {
int fa = map.get(a);
int fb = map.get(b);
if (fa != fb) {
return fa - fb;
} else {
return a.compareTo(b);
}
});
return res;
}

[Hard] 1312. Minimum Insertion Steps to Make a String Palindrome

Given a string s. In one step you can insert any character at any index of the string.

Return the minimum number of steps to make s palindrome.

A Palindrome String is one that reads the same backward as well as forward.

1
2
3
Input: s = "mbadm"
Output: 2
Explanation: String can be "mbdadbm" or "mdbabdm".

Solution

We set left as 0, right as n - 1 and compare these two, if it equals, we just need to focus on [left +1, right - 1] and if doesn’t we have to ways to make it palindrome, we can add char in left side or right side. So, we have to get min([left, right-1], [left + 1, right]) + 1. The following code uses top-down approach with memo.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// time:O(n^2) space:O(n^2)
public int minInsertions(String s) {
if (s == null || s.length() == 0) return 0;
int[][] memo = new int[s.length()][s.length()];
for (int[] arr : memo) {
Arrays.fill(arr, -1);
}
return helper(s, 0, s.length() - 1, memo);
}

public int helper(String s, int start, int end, int[][] memo) {
if (memo[start][end] != -1) return memo[start][end];
if (start >= end) return 0;
int res = 0;
if (s.charAt(start) == s.charAt(end)) {
res = helper(s, start + 1, end - 1, memo);
} else {
res = Math.min(helper(s, start, end - 1, memo), helper(s, start + 1, end, memo)) + 1;
}
memo[start][end] = res;
return memo[start][end];
}

Reference

Rank: 2742 / 6833. Solved 2/4 in 1:30:00.