Skip to content

Commit 9701eb7

Browse files
add 791
1 parent 4a99b45 commit 9701eb7

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | O(n+m) | O(1) | |Medium|
2526
|788|[Rotated Digits](https://leetcode.com/problems/rotated-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_788.java) | O(n*m) | O(1) | |Easy|
2627
|784|[Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_784.java) | O(n*2^n) | O(n*2^n) | |Easy|
2728
|783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_783.java) | O(n) | O(h) | |Easy|
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 791. Custom Sort String
8+
9+
S and T are strings composed of lowercase letters. In S, no letter occurs more than once.
10+
11+
S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.
12+
13+
Return any permutation of T (as a string) that satisfies this property.
14+
15+
Example :
16+
Input:
17+
S = "cba"
18+
T = "abcd"
19+
Output: "cbad"
20+
Explanation:
21+
"a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a".
22+
Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.
23+
24+
Note:
25+
26+
S has length at most 26, and no character is repeated in S.
27+
T has length at most 200.
28+
S and T consist of lowercase letters only.
29+
30+
*/
31+
public class _791 {
32+
public static class Solution1 {
33+
public String customSortString(String S, String T) {
34+
Map<Character, Integer> map = new HashMap<>();
35+
for (char c : T.toCharArray()) {
36+
map.put(c, map.getOrDefault(c, 0) + 1);
37+
}
38+
StringBuilder sb = new StringBuilder();
39+
for (char c : S.toCharArray()) {
40+
if (map.containsKey(c)) {
41+
int count = map.get(c);
42+
while (count-- > 0) {
43+
sb.append(c);
44+
}
45+
map.remove(c);
46+
}
47+
}
48+
for (char c : map.keySet()) {
49+
int count = map.get(c);
50+
while (count-- > 0) {
51+
sb.append(c);
52+
}
53+
}
54+
return sb.toString();
55+
}
56+
}
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._791;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _791Test {
10+
private static _791.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _791.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("cbad", solution1.customSortString("cba", "abcd"));
20+
}
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy