LeetCode 3093. 最长公共后缀查询

1 题目描述

给你两个字符串数组 wordsContainerwordsQuery

对于每个 wordsQuery[i] ,你需要从 wordsContainer 中找到一个与 wordsQuery[i]最长公共后缀 的字符串。如果 wordsContainer 中有两个或者更多字符串有最长公共后缀,那么答案为长度 最短 的。如果有超过两个字符串有 相同 最短长度,那么答案为它们在 wordsContainer 中出现 更早 的一个。

请你返回一个整数数组 ans ,其中 ans[i]wordsContainer中与 wordsQuery[i]最长公共后缀 字符串的下标。

示例 1:

输入:wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]

输出:[1,1,1]

解释:

我们分别来看每一个 wordsQuery[i]

  • 对于 wordsQuery[0] = "cd"wordsContainer 中有最长公共后缀 "cd" 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。
  • 对于 wordsQuery[1] = "bcd"wordsContainer 中有最长公共后缀 "bcd" 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。
  • 对于 wordsQuery[2] = "xyz"wordsContainer 中没有字符串跟它有公共后缀,所以最长公共后缀为 "" ,下标为 0 ,1 和 2 的字符串都得到这一公共后缀。这些字符串中, 答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。

示例 2:

输入:wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]

输出:[2,0,2]

解释:

我们分别来看每一个 wordsQuery[i]

  • 对于 wordsQuery[0] = "gh"wordsContainer 中有最长公共后缀 "gh" 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 2 的字符串,因为它的长度为 6 ,是最短的字符串。
  • 对于 wordsQuery[1] = "acbfgh" ,只有下标为 0 的字符串有最长公共后缀 "fgh" 。所以尽管下标为 2 的字符串是最短的字符串,但答案是 0 。
  • 对于 wordsQuery[2] = "acbfegh"wordsContainer 中有最长公共后缀 "gh" 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 2 的字符串,因为它的长度为 6 ,是最短的字符串。

提示:

  • 1 <= wordsContainer.length, wordsQuery.length <= 104
  • 1 <= wordsContainer[i].length <= 5 * 103
  • 1 <= wordsQuery[i].length <= 5 * 103
  • wordsContainer[i] 只包含小写英文字母。
  • wordsQuery[i] 只包含小写英文字母。
  • wordsContainer[i].length 的和至多为 5 * 105
  • wordsQuery[i].length 的和至多为 5 * 105

2 解题思路

这题最容易想到的方法应该就是字典树了,当然如果只是这样的话我也没有必要写这篇题解。

这里要介绍的方法是通过二分搜索,在使用几乎最少的额外空间下完成这道题。

2.1 翻转字符串

首先,我们需要将这个问题从后缀匹配转换为前缀匹配问题,也就是先把所有的字符串都进行一次翻转,在此过程中用map记录他们的下标。

后面要用到二分搜索,因此还需要进行一次排序,转换为有序数组。

unordered_map<string, int> index{};

int num = 0;
for (auto &words : wordsContainer)
{
// 翻转字符串
reverse(words.begin(), words.end());
// 相同的字符串仅记录最早的下标
if (!index.count(words))
{
index[words] = num;
}
num++;
} sort(wordsContainer.begin(), wordsContainer.end());

2.2 二分搜索迭代

接下来我们就需要思考,该如何利用二分搜索来找到答案。

举个栗子,我们需要在words中寻找query对应的最大前缀。

如果我们直接以query为目标,在words中进行二分搜索,其结果肯定是不正确的。

换个角度思考,我们要找到是最长的前缀,那我们大可以不以query为目标,而是先用二分搜索,把所有b开头的字符串给找到。

操作过程就如上图所示,其中橙色方框为查找的范围红色方框则是搜索的结果,二者都是左闭右开区间

其中搜索上界可以使用lower_bound,下界则是使用upper_bound

可以看出,我们已经找到了开头为b的字符串,其范围是[1, 5),所以我们就可以将查找的范围更新为[1, 5)

而接下来的事情,想必你也能猜到了,那就是在此基础上继续查找第二个字符为c的字符串。

搜索范围[1, 5)缩小到了[2, 4),接着继续查找第三个字符为d的字符串。

这次的结果还是[2, 4),最后再查找第四个字符为b的字符串。

有意思的事情发生了,搜索的结果[4, 4)是一个空区间,这说明已经没有能够继续匹配的字符串。这就意味着上一个搜索的结果(也就是[2, 4))就已经是能够找到的最长公共前缀字符串的区间了。

具体的代码如下:

auto begin = wordsContainer.begin();
auto end = wordsContainer.end(); for (string prefix{}; query.size(); query.pop_back())
{
prefix.push_back(query.back()); compare cmp(prefix.size() - 1);
auto nBegin = lower_bound(begin, end, prefix, cmp);
auto nEnd = upper_bound(begin, end, prefix, cmp); // nBegin == nEnd表示已经达到最大前缀匹配
// 直接退出循环,在目前的[begin, end)范围中寻找最合适的结果
if (nBegin == nEnd)
{
break;
} begin = nBegin;
end = nEnd;
}

2.3 遍历区间

最后,我们就可以根据题目的要求,在最终的区间内找出长度最短且出现最早的字符串的下标,作为本次查询的结果。

int minSize = INT32_MAX;
int minIndex = INT32_MAX;
while (begin != end)
{
string &s = *begin++;
if (s.size() < minSize ||
s.size() == minSize && index[s] < minIndex)
{
minSize = s.size();
minIndex = index[s];
}
}
ans.push_back(minIndex);

2.4 比较函数

其实在二分搜索的那一节还有些问题没有解决,比如我们要如何实现搜索呢?

我们使用lower_boundupper_bound函数定位的上下界时,比较的并不是传入的字符串,而是比较其中特定位置的字符,并且这个位置会随需求变化。这里就可以通过定义仿函数,来将每次比较所需要的下标信息传入其中。

class compare
{
public:
compare(int index) : index(index) {}
int index;
bool operator()(const string &s1, const string &s2)
{
return /*返回比较的结果*/;
}
};

先别急着在函数中返回s1[index] < s2[index]的结果,让我们接着分析一下具体情况

首先,我们需要明确的是,比较函数中传入的两个参数,一个是待匹配的字符串words,另一个是当前前缀prefix

从之前的搜索过程可以看出index = prefix.size() - 1,所以prefix绝对不会出现访问越界的情况,words就不一定了

index小于s1.size()s2.size()时,我们可以直接返回s1[index] < s2[index]

但是当words.size() <= index时,我们就需要分两种情况来判断。

  1. s1 = wordss1[index]没有字符,视为最小值,因此s1[index] < s2[index] == true
  2. s2 = wordss2[index]没有字符,视为最小值,因此s1[index] < s2[index] == false

我们将上述三种情况整理一下,用以下代码表示:

bool operator()(const string &s1, const string &s2)
{
// if(s1.size() <= index) return true;
// if(s2.size() <= index) return false;
// return s1[index] < s2[index]; return s1.size() <= index ||
s2.size() > index && s1[index] < s2[index];
}

3 答案代码

最终代码如下:

class compare
{
public:
compare(int index) : index(index) {}
int index;
bool operator()(const string &s1, const string &s2)
{
// if(s1.size() <= index) return true;
// if(s2.size() <= index) return false;
// return s1[index] < s2[index]; return s1.size() <= index ||
s2.size() > index && s1[index] < s2[index];
}
}; vector<int> stringIndices(vector<string> &wordsContainer,
vector<string> &wordsQuery)
{
vector<int> ans{};
unordered_map<string, int> index{}; int num = 0;
for (auto &words : wordsContainer)
{
reverse(words.begin(), words.end());
// 相同的字符串仅记录最早的下标
if (!index.count(words))
{
index[words] = num;
}
num++;
}
sort(wordsContainer.begin(), wordsContainer.end()); for (auto &query : wordsQuery)
{
auto begin = wordsContainer.begin();
auto end = wordsContainer.end(); for (string prefix{}; query.size(); query.pop_back())
{
prefix.push_back(query.back()); compare cmp(prefix.size() - 1);
auto nBegin = lower_bound(begin, end, prefix, cmp);
auto nEnd = upper_bound(begin, end, prefix, cmp); // nBegin == nEnd表示已经达到最大前缀匹配
// 直接退出循环,在目前的[begin, end)范围中寻找最合适的结果
if (nBegin == nEnd)
{
break;
} begin = nBegin;
end = nEnd;
} int minSize = INT32_MAX;
int minIndex = INT32_MAX;
while (begin != end)
{
string &s = *begin++;
if (s.size() < minSize ||
s.size() == minSize && index[s] < minIndex)
{
minSize = s.size();
minIndex = index[s];
}
}
ans.push_back(minIndex);
} return ans;
}

本文发布于2024年3月27日

最后编辑于2024年3月27日

LeetCode 3093. 最长公共后缀查询 (二分法)的更多相关文章

  1. LeetCode:最长公共前缀【14】

    LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...

  2. python(leetcode)-14最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  3. 【LeetCode】最长公共前缀【二分】

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  4. LeetCode 14. 最长公共前缀(Longest Common Prefix)

    14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...

  5. Java实现 LeetCode 14 最长公共前缀

    14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...

  6. [LeetCode]14.最长公共前缀(Java)

    原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...

  7. LeetCode 7最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  8. leetcode 14 最长公共前缀

    描述: 给个字符串vector,求最长公共前缀. 解决: 直接取第一个字符串作为最长公共前缀,将其每个字符遍历过一次.设最长字符实际为k,共n个元素,则复杂度O(nk) string longestC ...

  9. 领扣(LeetCode)最长公共前缀 个人题解

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  10. LeetCode 14. 最长公共前缀(Longest Common Prefix)

    题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

随机推荐

  1. C++ 多线程的错误和如何避免(11)

    不要在对时间敏感的上下文中使用 .get() 先看下面的代码, #include "stdafx.h" #include <future> #include <i ...

  2. win32-StretchDIBits - PrintDlg

    使用StretchDIBits将位图数据传输到printer的dc中 #include <Windows.h> #include <algorithm> int main() ...

  3. Qt+QtWebApp开发笔记(六):http服务器html实现静态相对路径调用第三方js文件

    前言   前面做了一些交互,网页是直接通过html对response进行返回的,这里QtWebApp与传统的web服务器不同,传统的web服务器可以调用同级目录相对路径或者绝对路径下的js,而QtWe ...

  4. 【.Net Core】.Net Core 源码分析与深入理解 - 入口 Program.cs (一)

    研究原因:学习 .Net Core 两年有余,实际项目也使用了一年半,自己的技术已经到了瓶颈,需要有一个突破,我觉得首先研究架构师的设计思想,其次分析一下.Net Core的源码,这将会是一个很好的学 ...

  5. 【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.

    问题描述 PHP的Web Job,通过artisan来配置路径启动PHP任务,相关启动脚本如下: artisan_path = "d:\\home\\site\\wwwroot"; ...

  6. 【Azure 应用服务】用App Service部署运行 Vue.js 编写的项目,应该怎么部署运行呢?

    问题描述 用App Service部署运行 Vue.js 编写的项目,应该怎么部署运行呢? 问题解答 VUE通常是运行在客户端侧的JS框架. App Service 在这种场景中是以静态文件的形式提供 ...

  7. [前端] html和原生js实现鼠标拖动和触摸拖动以及点击后跟随鼠标移动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Java处理子父级菜单的方式二

    处理存在子父级关系的数据是写代码的过程中常见的操作,前面讲解过使用递归的方法来做, 可以参考这篇博客 https://www.cnblogs.com/yilangcode/p/16831867.htm ...

  9. 32位数字电位器AD5228使用及调试总结

    一 概念 什么是数字电位计? 数字电位器(Digital Potentiometer)亦称数控可编程电阻器,是一种代替传统机械电位器(模拟电位器)的新型CMOS数字.模拟混合信号处理的集成电路.数字电 ...

  10. Hibernate之Criteria

      1,Criteria Hibernate 设计了 CriteriaSpecification 作为 Criteria 的父接口,下面提供了 Criteria和DetachedCriteria . ...