87:扰乱字符串

This commit is contained in:
huangge1199 2021-04-16 13:29:21 +08:00
parent 7fecd74df0
commit 17eea862e0
3 changed files with 191 additions and 2 deletions

View File

@ -0,0 +1,134 @@
//使用下面描述的算法可以扰乱字符串 s 得到字符串 t
//
// 如果字符串的长度为 1 算法停止
// 如果字符串的长度 > 1 执行下述步骤
//
// 在一个随机下标处将字符串分割成两个非空的子字符串如果已知字符串 s 则可以将其分成两个子字符串 x y 且满足 s = x + y
// 随机 决定是要交换两个子字符串还是要保持这两个子字符串的顺序不变在执行这一步骤之后s 可能是 s = x + y 或者 s = y + x
//
// x y 这两个子字符串上继续从步骤 1 开始递归执行此算法
//
//
//
//
// 给你两个 长度相等 的字符串 s1 s2判断 s2 是否是 s1 的扰乱字符串如果是返回 true 否则返回 false
//
//
//
// 示例 1
//
//
//输入s1 = "great", s2 = "rgeat"
//输出true
//解释s1 上可能发生的一种情形是
//"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串
//"gr/eat" --> "gr/eat" // 随机决定保持这两个子字符串的顺序不变
//"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法两个子字符串分别在随机下标处进行一轮分割
//"g/r / e/at" --> "r/g / e/at" // 随机决定第一组交换两个子字符串第二组保持这两个子字符串的顺序不变
//"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法 "at" 分割得到 "a/t"
//"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定保持这两个子字符串的顺序不变
//算法终止结果字符串和 s2 相同都是 "rgeat"
//这是一种能够扰乱 s1 得到 s2 的情形可以认为 s2 s1 的扰乱字符串返回 true
//
//
// 示例 2
//
//
//输入s1 = "abcde", s2 = "caebd"
//输出false
//
//
// 示例 3
//
//
//输入s1 = "a", s2 = "a"
//输出true
//
//
//
//
// 提示
//
//
// s1.length == s2.length
// 1 <= s1.length <= 30
// s1 s2 由小写英文字母组成
//
// Related Topics 字符串 动态规划
// 👍 229 👎 0
package leetcode.editor.cn;
import java.util.HashMap;
import java.util.Map;
//87:扰乱字符串
public class ScrambleString {
public static void main(String[] args) {
//测试代码
Solution solution = new ScrambleString().new Solution();
System.out.println(solution.isScramble("abca", "caba"));//true
System.out.println(solution.isScramble("abcdbdacbdac", "bdacabcdbdac"));//true
System.out.println(solution.isScramble("great", "rgeat"));//true
System.out.println(solution.isScramble("abcde", "caebd"));//false
System.out.println(solution.isScramble("abc", "acb"));//true
System.out.println(solution.isScramble("abc", "bca"));//true
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
Map<String, Map<String, Boolean>> map = new HashMap<>();
public boolean isScramble(String s1, String s2) {
if (map.containsKey(s1) && map.get(s1).containsKey(s2)) {
return map.get(s1).get(s2);
}
if (s1.equals(s2)) {
return true;
}
if (!charSame(s1, s2)) {
return false;
}
int length = s1.length();
if (map.containsKey(s1) && map.get(s1).containsKey(s2)) {
return map.get(s1).get(s2);
}
Map<String, Boolean> temp;
for (int i = 1; i < length; i++) {
if (isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(s1.substring(i), s2.substring(i))
|| isScramble(s1.substring(0, i), s2.substring(length - i)) && isScramble(s1.substring(i), s2.substring(0, length - i))) {
temp = map.containsKey(s1) ? map.get(s1) : new HashMap<>(1);
temp.put(s2, true);
map.put(s1, temp);
return true;
}
}
temp = map.containsKey(s1) ? map.get(s1) : new HashMap<>(1);
temp.put(s2, false);
map.put(s1, temp);
return false;
}
private boolean charSame(String s1, String s2) {
int[] ch = new int[26];
int num = 0;
for (int i = 0; i < s1.length(); i++) {
int ch1 = s1.charAt(i) - 'a';
if (ch[ch1] >= 0) {
num++;
}
ch[ch1]++;
int ch2 = s2.charAt(i) - 'a';
if (ch[ch2] > 0) {
num--;
}
ch[ch2]--;
}
return num == 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,56 @@
使用下面描述的算法可以扰乱字符串 <code>s</code> 得到字符串 <code>t</code>
<ol>
<li>如果字符串的长度为 1 ,算法停止</li>
<li>如果字符串的长度 > 1 ,执行下述步骤:
<ul>
<li>在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 <code>s</code> ,则可以将其分成两个子字符串 <code>x</code><code>y</code> ,且满足 <code>s = x + y</code></li>
<li><strong>随机</strong> 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,<code>s</code> 可能是 <code>s = x + y</code> 或者 <code>s = y + x</code></li>
<li><code>x</code><code>y</code> 这两个子字符串上继续从步骤 1 开始递归执行此算法。</li>
</ul>
</li>
</ol>
<p>给你两个 <strong>长度相等</strong> 的字符串 <code>s1</code><em> </em>和 <code>s2</code>,判断 <code>s2</code><em> </em>是否是 <code>s1</code><em> </em>的扰乱字符串。如果是,返回 <code>true</code> ;否则,返回 <code>false</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s1 = "great", s2 = "rgeat"
<strong>输出:</strong>true
<strong>解释:</strong>s1 上可能发生的一种情形是:
"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串
"gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
"g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
算法终止,结果字符串和 s2 相同,都是 "rgeat"
这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s1 = "abcde", s2 = "caebd"
<strong>输出:</strong>false
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>s1 = "a", s2 = "a"
<strong>输出:</strong>true
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>s1.length == s2.length</code></li>
<li><code>1 <= s1.length <= 30</code></li>
<li><code>s1</code><code>s2</code> 由小写英文字母组成</li>
</ul>
<div><div>Related Topics</div><div><li>字符串</li><li>动态规划</li></div></div>\n<div><li>👍 229</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long