Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortest Distance Between Words #1685 #1692

Merged
merged 6 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ _add list here_
- [Ugly Number With Recursion](cp/UglyRecursion.java)
- [PDDI Using Recursion](cp/ArmStrongRecursion.java)
- [Subarray of an array with given sum in O(n) time](cp/Subarray.java)
- [Shortest Distance Between Words](cp/Shortest_Dist.java)

## Cryptography

Expand Down
86 changes: 86 additions & 0 deletions Java/cp/Shortest_Dist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Given a list of words and two words word1 and word2, find the shortest distance between those two words from the list.
*/
import java.util.*;
class Shortest_Dist {
static List < String > arr;
public static void main(String args[]) {
int t = 0;

Scanner sc = new Scanner(System.in);
System.out.println("Enter the list of words " + "\n" + "Words = ");
arr = new ArrayList <> ();

arr.add(sc.nextLine());
while (sc.hasNext()) {
arr.add(sc.nextLine());
if (sc.hasNextInt()) {
t = sc.nextInt();
break;
}

}

List < String > wd = new ArrayList(arr);
while (t > 0) {
String wd1 = sc.next();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prompt user to enter the words between which the distance is to be determined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atarax665 done.

String wd2 = sc.next();
System.out.println(" The shortest Distance between " + wd1 + " and " + wd2 + " is : " + shortest_dist(wd, wd1, wd2));
wd = new ArrayList(arr);

t--;
}

}
public static int shortest_dist(List < String > wd, String wd1, String wd2) {
int dist = -1;
List < String > wdcopy = new ArrayList(wd);
List < Integer > ind1 = new ArrayList <> ();
List < Integer > ind2 = new ArrayList <> ();

while (wd.contains(wd1)) {
ind1.add(wd.indexOf(wd1));
//
wd.add(wd.indexOf(wd1), "0");
wd.remove(wd.indexOf(wd1));

}

while (wdcopy.contains(wd2)) {
ind2.add(wdcopy.indexOf(wd2));

wdcopy.add(wdcopy.indexOf(wd2), "0");
wdcopy.remove(wdcopy.indexOf(wd2));
}

for (int i = 0; i < ind1.size(); i++) {
for (int j = 0; j < ind2.size(); j++) {
int d = (int) Math.abs(ind1.get(i) - ind2.get(j));
if (dist == -1)
dist = d;
else if ((dist != -1) && (d < dist))
dist = d;
}
}
return dist;

}
}
/*Sample Input And Output :
* Enter the list of words
Words =
practice
makes
perfect
coding
makes
2
practice
coding
The shortest Distance between practice and coding is : 3
coding
makes
The shortest Distance between coding and makes is : 1

Timple Complexity : O(n)
Space Complexity : O(1)
*/