Skip to content

Commit d7c14ad

Browse files
authored
Added task 1585.
1 parent 661d2e8 commit d7c14ad

File tree

3 files changed

+118
-0
lines changed
  • src
    • main/java/g1501_1600/s1585_check_if_string_is_transformable_with_substring_sort_operations
    • test/java/g1501_1600/s1585_check_if_string_is_transformable_with_substring_sort_operations

3 files changed

+118
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g1501_1600.s1585_check_if_string_is_transformable_with_substring_sort_operations;
2+
3+
// #Hard #String #Sorting #Greedy #2022_04_11_Time_20_ms_(97.22%)_Space_54.4_MB_(72.22%)
4+
5+
public class Solution {
6+
public boolean isTransformable(String s, String t) {
7+
int n = s.length();
8+
if (n != t.length()) {
9+
return false;
10+
}
11+
int[] cnt = new int[10];
12+
for (int i = 0; i < n; i++) {
13+
cnt[s.charAt(i) - '0']++;
14+
}
15+
for (int i = 0; i < n; i++) {
16+
cnt[t.charAt(i) - '0']--;
17+
}
18+
for (int i = 0; i < 10; i++) {
19+
if (cnt[i] != 0) {
20+
return false;
21+
}
22+
}
23+
int[] sCnt = new int[10];
24+
int[] tCnt = new int[10];
25+
for (int i = 0; i < n; i++) {
26+
int sAsci = s.charAt(i) - '0';
27+
int tAsci = t.charAt(i) - '0';
28+
sCnt[sAsci]++;
29+
if (tCnt[sAsci] >= sCnt[sAsci] || (sAsci == tAsci && tCnt[sAsci] + 1 >= sCnt[sAsci])) {
30+
int rem = 0;
31+
for (int j = 0; j < sAsci; j++) {
32+
if (sCnt[j] - tCnt[j] > 0) {
33+
rem++;
34+
}
35+
}
36+
if (rem > 0) {
37+
return false;
38+
}
39+
}
40+
if (sCnt[tAsci] >= tCnt[tAsci] + 1) {
41+
int rem = 0;
42+
for (int j = tAsci; j < 10; j++) {
43+
if (tCnt[j] - sCnt[j] > 0) {
44+
rem++;
45+
}
46+
}
47+
if (rem > 0) {
48+
return false;
49+
}
50+
}
51+
tCnt[tAsci]++;
52+
}
53+
return true;
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1585\. Check If String Is Transformable With Substring Sort Operations
2+
3+
Hard
4+
5+
Given two strings `s` and `t`, transform string `s` into string `t` using the following operation any number of times:
6+
7+
* Choose a **non-empty** substring in `s` and sort it in place so the characters are in **ascending order**.
8+
* For example, applying the operation on the underlined substring in `"14234"` results in `"12344"`.
9+
10+
Return `true` if _it is possible to transform `s` into `t`_. Otherwise, return `false`.
11+
12+
A **substring** is a contiguous sequence of characters within a string.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "84532", t = "34852"
17+
18+
**Output:** true
19+
20+
**Explanation:** You can transform s into t using the following sort operations: "84532" (from index 2 to 3) -> "84352" "84352" (from index 0 to 2) -> "34852"
21+
22+
**Example 2:**
23+
24+
**Input:** s = "34521", t = "23415"
25+
26+
**Output:** true
27+
28+
**Explanation:** You can transform s into t using the following sort operations: "34521" -> "23451" "23451" -> "23415"
29+
30+
**Example 3:**
31+
32+
**Input:** s = "12345", t = "12435"
33+
34+
**Output:** false
35+
36+
**Constraints:**
37+
38+
* `s.length == t.length`
39+
* <code>1 <= s.length <= 10<sup>5</sup></code>
40+
* `s` and `t` consist of only digits.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1501_1600.s1585_check_if_string_is_transformable_with_substring_sort_operations;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void isTransformable() {
11+
assertThat(new Solution().isTransformable("84532", "34852"), equalTo(true));
12+
}
13+
14+
@Test
15+
void isTransformable2() {
16+
assertThat(new Solution().isTransformable("34521", "23415"), equalTo(true));
17+
}
18+
19+
@Test
20+
void isTransformable3() {
21+
assertThat(new Solution().isTransformable("12345", "12435"), equalTo(false));
22+
}
23+
}

0 commit comments

Comments
 (0)