使三个字符串相等
难度:
标签:
题目描述
You are given three strings: s1
, s2
, and s3
. In one operation you can choose one of these strings and delete its rightmost character. Note that you cannot completely empty a string.
Return the minimum number of operations required to make the strings equal. If it is impossible to make them equal, return -1
.
Example 1:
Input: s1 = "abc", s2 = "abb", s3 = "ab"
Output: 2
Explanation: Deleting the rightmost character from both s1
and s2
will result in three equal strings.
Example 2:
Input: s1 = "dac", s2 = "bac", s3 = "cac"
Output: -1
Explanation: Since the first letters of s1
and s2
differ, they cannot be made equal.
Constraints:
1 <= s1.length, s2.length, s3.length <= 100
s1
,s2
ands3
consist only of lowercase English letters.
代码结果
运行时间: 27 ms, 内存: 16.1 MB
// 思路:使用Java Stream来处理公共后缀的查找和删除操作次数的计算。
// 主要思路与普通Java版本一致,只是通过流的方式进行数据处理。
import java.util.stream.IntStream;
public class SolutionStream {
public int minOperations(String s1, String s2, String s3) {
int minLength = IntStream.of(s1.length(), s2.length(), s3.length()).min().orElse(0);
long commonSuffixLength = IntStream.range(0, minLength)
.mapToObj(i -> new int[]{s1.charAt(s1.length() - 1 - i), s2.charAt(s2.length() - 1 - i), s3.charAt(s3.length() - 1 - i)})
.takeWhile(chars -> chars[0] == chars[1] && chars[1] == chars[2])
.count();
int totalRemovals = (s1.length() - (int) commonSuffixLength) +
(s2.length() - (int) commonSuffixLength) +
(s3.length() - (int) commonSuffixLength);
return totalRemovals == 0 ? -1 : totalRemovals;
}
}
解释
方法:
此题解通过检查三个字符串s1, s2, s3的公共前缀来解决问题。首先,通过一个循环比较所有字符串在同一位置的字符是否相同,如果相同,则继续检查下一个字符;如果不同,或任一字符串的长度被耗尽,则停止循环。循环结束后,如果存在公共前缀(即i>0),则计算将三个字符串剪切到公共前缀长度所需的操作次数,这等于三个字符串的总长度减去三倍的公共前缀长度。如果没有公共前缀(i == 0),则直接返回-1,表示无法通过剪切使三个字符串相等。
时间复杂度:
O(n)
空间复杂度:
O(1)
代码细节讲解
🦆
在题解中,你是如何处理每个字符串长度不等但可能存在公共前缀的情况?
▷🦆
为什么当公共前缀长度为0时直接返回-1,而不考虑其他可能的操作来使三个字符串相等?
▷🦆
题解中提到的操作次数计算公式是怎么得出的,能详细解释一下吗?
▷🦆
如果在某一步中,两个字符串的字符相同,但第三个字符串的字符不同,这种情况下算法如何处理?
▷