-
Notifications
You must be signed in to change notification settings - Fork 2
feat(strings, trie): is prefix of word #108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a28d87d
feat(strings, trie): is prefix of word
BrianLusina cb0b9c4
chore: remove redundant test
BrianLusina cbf1727
updating DIRECTORY.md
3166339
refactor(trie): fix none check
BrianLusina 3b41d91
Meerge branch 'feat/is-prefix-of-word' of github.com:BrianLusina/Pyth…
BrianLusina 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| # Check if a Word is a Prefix of Any Word in a Sentence | ||
|
|
||
| You are given a sentence containing words separated by single spaces and a searchWord. Your task is to determine whether | ||
| searchWord is a prefix of any word in the sentence. | ||
| Return the 1-based index of the first word in which searchWord appears as a prefix. | ||
|
|
||
| - If searchWord is a prefix of multiple words, return the index of the earliest such word. | ||
| - If no word starts with searchWord, return −1 | ||
|
|
||
| > A prefix of a string is any contiguous substring that begins at the first character. | ||
|
|
||
| Constraints: | ||
|
|
||
| - 1 <= sentence.length <= 100 | ||
| - 1 <= search_word.length <= 10 | ||
| - The sentence consists of lowercase English letters and spaces. | ||
| - search_word consists of lowercase English letters. | ||
|
|
||
| ## Examples | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  |
This file contains hidden or 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,23 @@ | ||
| from datastructures.trees.trie.trie_node import TrieNode | ||
|
|
||
|
|
||
| def is_prefix_of_word(sentence: str, search_word: str) -> int: | ||
| """ | ||
| This function will check if a given search_word is a prefix of any word in a sentence. | ||
|
|
||
| Parameters: | ||
| sentence (str): The sentence to search in. | ||
| search_word (str): The prefix to search for. | ||
|
|
||
| Returns: | ||
| int: The index of the word if the prefix is found, -1 otherwise. | ||
| """ | ||
| trie = TrieNode() | ||
| word_list = sentence.split(" ") | ||
|
|
||
| # Insert the words into the trie with their respective index | ||
| for index, word in enumerate(word_list, start=1): | ||
| trie.insert(word, index) | ||
|
|
||
| # Search for the prefix in the trie | ||
| return trie.search_prefix(search_word) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,57 @@ | ||
| import unittest | ||
| from . import is_prefix_of_word | ||
|
|
||
|
|
||
| class IsPrefixOfWordTestCase(unittest.TestCase): | ||
| def test_1(self): | ||
| sentence = "i love coding" | ||
| search_word = "lov" | ||
| expected = 2 | ||
| actual = is_prefix_of_word(sentence, search_word) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_2(self): | ||
| sentence = "hello world" | ||
| search_word = "he" | ||
| expected = 1 | ||
| actual = is_prefix_of_word(sentence, search_word) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_3(self): | ||
| sentence = "please playground player" | ||
| search_word = "pla" | ||
| expected = 2 | ||
| actual = is_prefix_of_word(sentence, search_word) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_4(self): | ||
| sentence = "open source ai" | ||
| search_word = "deep" | ||
| expected = -1 | ||
| actual = is_prefix_of_word(sentence, search_word) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_5(self): | ||
| sentence = "cats dog cattle category" | ||
| search_word = "cat" | ||
| expected = 1 | ||
| actual = is_prefix_of_word(sentence, search_word) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_6(self): | ||
| sentence = "this is fine" | ||
| search_word = "fi" | ||
| expected = 3 | ||
| actual = is_prefix_of_word(sentence, search_word) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_7(self): | ||
| sentence = "hello world" | ||
| search_word = "x" | ||
| expected = -1 | ||
| actual = is_prefix_of_word(sentence, search_word) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.