Skip to content

Instantly share code, notes, and snippets.

@valarpirai
Created November 12, 2025 05:56
Show Gist options
  • Select an option

  • Save valarpirai/bb06924bf6c02ef71004e0f86b212961 to your computer and use it in GitHub Desktop.

Select an option

Save valarpirai/bb06924bf6c02ef71004e0f86b212961 to your computer and use it in GitHub Desktop.
Word quest problem
/**
Problem Statement: The WordQuest Swipe Challenge
In the WordQuest championship, you’ve swiped the string s = "worlkdr" on your keyboard,
hoping to form a valid word from a given list of legendary words: ["word", "world", "wonder", "west"].
Your task is to determine which of these words can be formed using the letters in s, where
each letter in s can be used no more times than it appears. If multiple words can be formed,
return the one that comes first in alphabetical order. If no word can be formed, return "-1".
Input:
A string s (e.g., "worlkdr") representing the swiped letters.
A list of target words (e.g., ["word", "world", "wonder", "west", "wood"]).
Output:
A string representing the first valid word in alphabetical order that can be formed from the letters in s.
If no word can be formed, return "-1".
Constraints:
- The string s contains only lowercase English letters.
- Each letter in s can be used only as many times as it appears.
- The target words contain only lowercase English letters.
- If multiple words are valid, return the one that is first in alphabetical order.
Example:
Input: s = "worlkdr", target = ["word", "world", "wonder", "west", "wood"]
Output: "word"
Explanation:
Letters in s: w, o, l, k, d, r, e (each appears once).
"word" (w, o, r, d): All letters present → Valid.
"world" (w, o, r, l, d): All letters present → Valid.
"wonder" (w, o, n, d, e, r): Missing 'n' → Invalid.
"west" (w, e, s, t): Missing 's' and 't' → Invalid.
Valid words: ["word", "world"]. Alphabetical order: ["word", "world"] → Return "word".
Input: s = "root", target = ["cat", "door"]
Output: "-1"
Explanation:
No word can formed from "root"
Task: Implement the validWord method to check which target words can be formed from s,
returning the lexicographically smallest valid word or "-1" if none are valid.
*/
import java.util.List;
public class SwipeWordQuest {
public static String validWord(String s, List<String> target) {
// Write your code here
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the swipe string
System.out.println("Enter the swipe string:");
String str = scanner.nextLine().trim();
// Read the target words (space-separated)
System.out.println("Enter the target words (space-separated):");
String[] targetArray = scanner.nextLine().trim().split("\\s+");
List<String> targets = Arrays.asList(targetArray);
// Output the result
System.out.println(validWord(str, targets));
scanner.close();
}
}
@valarpirai
Copy link
Author

  • Use Anagram to find whether the target words can be formed
  • Then, use the Levenshtein distance to filter the closest possible word and return it
    OR
  • If asked for order of chars, then use a substring with a start index to find the next char present

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment