-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93186d8
Shortest Distance Between Words
ErzaTitania-2001 d404d2d
Update and rename GC2.java to Shortest_Dist.java
ErzaTitania-2001 01b12e2
Update README.md
ErzaTitania-2001 596a6c9
Removed spaces between < >
ErzaTitania-2001 3c8ad7b
Update Shortest_Dist.java
ErzaTitania-2001 26b20c3
formatted code
iamrajiv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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) | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atarax665 done.