LeetCode100131. Make Three Strings Equal
最佳答案 问答题库628位专家为你答疑解惑
文章目录
- 一、题目
- 二、题解
一、题目
You are given three strings s1, s2, and s3. You have to perform the following operation on these three strings as many times as you want.
In one operation you can choose one of these three strings such that its length is at least 2 and delete the rightmost character of it.
Return the minimum number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return -1.
Example 1:
Input: s1 = “abc”, s2 = “abb”, s3 = “ab”
Output: 2
Explanation: Performing operations on s1 and s2 once will lead to three equal strings.
It can be shown that there is no way to make them equal with less than two operations.
Example 2:
Input: s1 = “dac”, s2 = “bac”, s3 = “cac”
Output: -1
Explanation: Because the leftmost letters of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1.
Constraints:
1 <= s1.length, s2.length, s3.length <= 100
s1, s2 and s3 consist only of lowercase English letters.
二、题解
求字符串的最长公共前缀(LCP)
class Solution {
public:int findMinimumOperations(string s1, string s2, string s3) {int n1 = s1.length(),n2 = s2.length(),n3 = s3.length();int n = min(min(n1,n2),n3);int index = 0;for(int i = 0;i < n;i++){if(s1[i] == s2[i] && s2[i] == s3[i]) index++;else break;}if(index == 0) return -1;return n1 + n2 + n3 - 3 * index;}
};
99%的人还看了
相似问题
- 【洛谷 B2003】输出第二个整数 题解(顺序结构+输入输出)
- 20天拿下华为OD笔试之【模拟】2023B-数大雁【欧弟算法】全网注释最详细分类最全的华为OD真题题解
- Centos(Linux)服务器安装Dotnet8 及 常见问题解决
- 【蓝桥杯选拔赛真题23】C++计算24 第十二届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
- [github配置] 远程访问仓库以及问题解决
- 电子学会C/C++编程等级考试2022年06月(一级)真题解析
- [github初学者教程] 分支管理-以及问题解决
- Flume的安装部署及常见问题解决
- 电子学会C/C++编程等级考试2022年03月(一级)真题解析
- 2023年咸阳市《网络建设与运维》赛题解析------四、安全配置
猜你感兴趣
版权申明
本文"LeetCode100131. Make Three Strings Equal":http://eshow365.cn/6-40262-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 【C/PTA】数组进阶练习(三)
- 下一篇: 【Python仿真】基于EKF的传感器融合定位