forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.5.cpp
35 lines (31 loc) · 780 Bytes
/
20.5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
const int kMaxInt = ~(1<<31);
int ShortestDist(string text[], int n, string word1, string word2){
int min = kMaxInt / 2;
int pos1 = -min;
int pos2 = -min;
for(int pos=0; pos<n; ++pos){
if(text[pos] == word1){
pos1 = pos;
int dist = pos1 - pos2;
if(dist < min)
min = dist;
}
else if(text[pos] == word2){
pos2 = pos;
int dist = pos2 - pos1;
if(dist < min)
min = dist;
}
}
return min;
}
int main(){
string text[] = {
"What","is","your","name","My","name","is","Hawstein"
};
int len = 8;
cout<<ShortestDist(text, len, "is", "name")<<endl;
return 0;
}