当前位置: 首页 > article >正文

LeetCode100131. Make Three Strings Equal

文章目录

    • 一、题目
    • 二、题解

一、题目

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;
    }
};

http://www.kler.cn/a/135051.html

相关文章:

  • redis bind 127.0.0.1和bind 10.34.56.78的区别
  • MybatisPlus入门(十)MybatisPlus-逻辑删除和多记录操作
  • sql server 查看io资源使用
  • 除了 Postman,还有什么好用的 API 调试工具吗
  • 在 Ubuntu 上安装 `.deb` 软件包有几种方法
  • 天才的懈怠 : 平衡二叉树
  • OSCP系列靶场-Esay-DC-1
  • 在 Qt 框架中,有许多内置的信号可用于不同的类和对象\triggered
  • 数据结构【DS】数组
  • IDEA常用插件合集
  • 产业区块链生态日:你的故事,我们在等待 | 征集帖
  • 软文推广如何实现效果?媒介盒子为你支招
  • 选择java商城开发商需要注意哪些方面?
  • Web前端—小兔鲜儿电商网站底部设计及网站中间过渡部分设计
  • Vue 路由缓存 防止路由切换数据丢失 路由的生命周期
  • 虾皮台湾站点如何选品
  • 关于代码混淆,看这篇就够了
  • NX二次开发UF_CAM_ask_f_s_db_object 函数介绍
  • redis+python 建立免费http-ip代理池;验证+留接口
  • IC卡操作软件支持PN532
  • python 集合(set)
  • 基于 FFmpeg 的跨平台视频播放器简明教程(十一):一种简易播放器的架构介绍
  • 如何解决swagger-editor在线接口调试时的跨域问题
  • 海外IP代理如何助力跨境电商?
  • 海外媒体发稿:出口贸易媒体发稿16个超实用技巧-华媒舍
  • 系列九、JUC强大的辅助类