Skip to content

Commit ee17ea1

Browse files
author
rpanjrath
committed
Strings: Distance between words. Hint: Kadanes Algorithm.
1 parent 2845d37 commit ee17ea1

File tree

2 files changed

+85
-19
lines changed

2 files changed

+85
-19
lines changed

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,62 +193,65 @@ Strings
193193
1. Check string palindrome.
194194
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/checkpalindrome/CheckPalindrome.java
195195

196-
2. Find common prefix for a given list of strings.
196+
2. Find minimum distance between two words (order preserved) in a big string. (Kadane's Algo)
197+
Link:
198+
199+
3. Find common prefix for a given list of strings.
197200
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/findcommonprefix/CommonPrefix.java
198201

199-
3. Find duplicate strings in list of strings.
202+
4. Find duplicate strings in list of strings.
200203
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/findduplicates/FindDuplicates.java
201204

202-
4. Find latest version problem.
205+
5. Find latest version problem.
203206
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/findlatestversion/FindLatestVersion.java
204207

205-
5. Find Longest Common Subsequence.
208+
6. Find Longest Common Subsequence.
206209
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/findlongestcommonsubsequence/LCS.java
207210

208-
6. Find Longest Palindrome in a string.
211+
7. Find Longest Palindrome in a string.
209212
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/findlongestpalindrome/FindLongestPalindrome.java
210213

211-
7. Print permutations of all characters in a string.
214+
8. Print permutations of all characters in a string.
212215
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/findpermutations/FindPermutations.java
213216

214-
8. Find total number of palindromes in a String.
217+
9. Find total number of palindromes in a String.
215218
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/findtotalpalindromes/FindTotalNoOfPalindromes.java
216219

217-
9. Find first non-repeated character.
220+
10. Find first non-repeated character.
218221
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/firstnonrepeatedchar/FirstNonRepeatedChar.java
219222

220-
10. Given two (dictionary) words as Strings, determine if they are isomorphic.
223+
11. Given two (dictionary) words as Strings, determine if they are isomorphic.
221224
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/isomorphicstrings/Isomorphic.java
222225

223-
11. Given s string, find max size of a sub-string, in which no duplicate chars present.
226+
12. Given s string, find max size of a sub-string, in which no duplicate chars present. (Kadane's Algo)
224227
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/longestsubstringnorepeatedchar/LongestSubstringUnrepeatedChar.java
225228

226-
12. Find min substring that contains all the char of target string.
229+
13. Find min substring that contains all the char of target string.
227230
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/minimumsubstringcontains/MinimumSubstring.java
228231

229-
13. Print diamonds on basis of input size.
232+
14. Print diamonds on basis of input size.
230233
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/printdiamonds/PrintDiamonds.java
231234

232-
14. Identify all the 'n' (n will be input) letter-long sequences that occur more than once in any given input string.
235+
15. Identify all the 'n' (n will be input) letter-long sequences that occur more than once in any given input string.
233236
OR Find repeating sequence of specified length in given DNA chromosome sequence.
234237
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/repeatingstringsofspecifiedlength/RepeatingStringOfSpecificLength.java
235238

236-
15. Reverse a string.
239+
16. Reverse a string.
237240
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/reversestring/ReverseString.java
238241

239-
16. Find all dictionary words in a given string.
242+
17. Find all dictionary words in a given string.
240243
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/segmentstringfindalldictwords/StringSegmentation.java
241244

242-
17. Return true if stringA is subsequence of stringB.
245+
18. Return true if stringA is subsequence of stringB.
243246
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/stringsubsequencecheck/CheckStringSubsequence.java
244247

245-
18. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring.
248+
19. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring.
246249
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/substringcheckforrotatedstring/RotatedStringSubStringCheck.java
247250

248-
19. Check if a string has all unique characters.
251+
20. Check if a string has all unique characters.
249252
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/uniquecharscheck/CheckUniqueCharacters.java
250253

251-
20. Count word occurrence in a large text file.
254+
21. Count word occurrence in a large text file.
252255
Link: https://github.com/techpanja/interviewproblems/blob/master/src/strings/wordcountinlargefile/WordOccurencesInLargeFile.java
253256

254257
Threads
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package strings.distancebetweenwords;
2+
3+
import java.util.StringTokenizer;
4+
5+
/**
6+
* Find minimum distance between two words (order preserved) in a big string.
7+
* For e.g 1. "hello how are you" - distance between "hello" and "you" is 3.
8+
* e.g 2. "hello how are hello you" - distance is 1
9+
* e.g 3. "you are hello" - distance is -1. Order of "hello" and "you" should be preserved.
10+
* e.g 4. "hello how are hello" - distance is -1 since "you" didnt occur even once.
11+
*
12+
* Hint: Kadane's algorithm
13+
*
14+
* Created by techpanja
15+
* Created on 1/28/14 3:12 PM.
16+
*/
17+
public class DistanceBetweenWords {
18+
19+
private DistanceBetweenWords() {
20+
21+
}
22+
23+
/*
24+
* Assume words in inputBody are separated by space.
25+
* */
26+
public static int findDistanceBetweenWords(String inputBody, String pair1, String pair2) {
27+
if (inputBody.isEmpty() || pair1.isEmpty() || pair2.isEmpty()) {
28+
return -1;
29+
}
30+
if (pair1.equals(pair2)) {
31+
return 0;
32+
}
33+
StringTokenizer stringTokenizer = new StringTokenizer(inputBody, " ");
34+
int distance = 0, globalDistance = inputBody.length();
35+
boolean foundPair1 = false;
36+
String token = "";
37+
while (stringTokenizer.hasMoreTokens()) {
38+
token = stringTokenizer.nextToken();
39+
if (token.equals(pair1)) {
40+
distance = 0;
41+
foundPair1 = true;
42+
}
43+
if (foundPair1) {
44+
distance++;
45+
}
46+
if (token.equals(pair2) && foundPair1) {
47+
globalDistance = Math.min(distance, globalDistance);
48+
}
49+
}
50+
if (globalDistance == inputBody.length()) {
51+
return -1;
52+
} else {
53+
return globalDistance - 1;
54+
}
55+
}
56+
57+
public static void main(String[] args) {
58+
System.out.println(findDistanceBetweenWords("hello how are you hello you", "hello", "you"));
59+
System.out.println(findDistanceBetweenWords("hello how are you hello", "hello", "you"));
60+
System.out.println(findDistanceBetweenWords("hello how are", "hello", "you"));
61+
System.out.println(findDistanceBetweenWords("you are how hello", "hello", "you"));
62+
}
63+
}

0 commit comments

Comments
 (0)