当前位置:网站首页>Interview question 17.11. word distance ●●
Interview question 17.11. word distance ●●
2022-07-25 22:37:00 【keep_ fan】
Interview questions 17.11. Word distance ●●
describe
There's a huge text file with words , Given any two different words , Find the shortest distance between the two words in this file ( Number of words separated ).
Advanced questions :
If the search process is repeated many times in this file , And each time I look for a different word , Can you optimize this ?
Example
Input :words = [“I”,“am”,“a”,“student”,“from”,“a”,“university”,“in”,“a”,“city”], word1 = “a”, word2 = “student”
Output :1
Answer key
1. Double pointer
Create two pointers , In the process of traversing the string array , The two pointers point to the traversed range , Corresponding to the word Maximum subscript idx1 and idx2 ( That is, when the corresponding word changes the new pointer position ), Calculate and update the interval distance every time .
- Time complexity :O(n), among nn It's an array words The length of . You need to traverse the array for one calculation word1 and word2 The shortest distance , The time to update the subscript and the shortest distance is O(1). Here, the length of the string is regarded as a constant .
- Spatial complexity :O(1).

class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
int idx1 = -1, idx2 = -1;
int ans = INT_MAX;
for(int i = 0; i < words.size(); ++i){
if(words[i] == word1 || words[i] == word2){
if(words[i] == word1){
// Update pointer
idx1 = i;
}else{
idx2 = i;
}
if(idx1 >= 0 && idx2 >= 0){
// Update distance
ans = min(ans, abs(idx1 - idx2));
if(ans == 1) return 1;
}
}
}
return ans;
}
};
Advanced questions :
If the search process is repeated many times in this file , And each time I look for a different word ,
You can maintain a Hashtable Record each word Subscript list .
Traverse the file once , Get All subscripts of each word appearing in the file .
When looking for words , Just get a list of subscripts for two words , Use Double pointer traverses two subscript linked lists , You can get the shortest distance between two words .
边栏推荐
猜你喜欢

About vscode usage+ Solutions to the problem of tab failure

软件测试 pytest pytest的命名规则 用例的前后置 conftest.py 定制allure报告 @pytest.mark.parametrize()装饰器作数据驱动

Wechat official account application development (I)

数据质量:数据治理的核心

Basic principle of torque motor control

Binder原理
![[training Day12] be go! [dynamic programming] [mathematics]](/img/63/689c17a0aae22ba25600b136178bf6.png)
[training Day12] be go! [dynamic programming] [mathematics]

【集训DAY11】Calc【数学】

MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV

贴片微型滚珠振动开关的结构原理
随机推荐
Perform Jieba word segmentation on the required content and output EXCEL documents according to word frequency
【集训DAY13】Out race【数学】【动态规划】
Why should we launch getaverse?
IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
MapGIS格式转ArcGIS方法
Share two music playing addresses
torchvision
Simulated Xiaomi mall head home page
Wkid in ArcGIS
Short circuit and &, short circuit or |, logic and &, logic or |; Conditional operator
ML-Numpy
Solve several common problems
冯诺依曼体系结构
Two methods of printing strings in reverse order in C language
xss-收集常用的代码
【集训DAY15】好名字【hash】
【集训DAY15】油漆道路【最小生成树】
【集训DAY13】Backpack【动态规划】【贪心】
【集训DAY15】简单计算【树状数组】【数学】
Five constraints and three paradigms