Overview
ThecontainsSource function checks if an exact source string exists in the trie produced by buildTrie. This is useful to verify if a source word has been included in the trie and is therefore searchable and replaceable using the searchAndReplace function.
Function signature
Parameters
The trie to search within. This trie should be constructed using the buildTrie function.
The candidate source string to look up. The function checks if this exact string exists as a source in the trie.
Returns
True if the trie contains the source string, otherwise false. The function returns true only if the string exists as a complete source word with an associated target.
Example usage
Basic usage
Checking partial matches
Verifying rule coverage
Implementation details
The function performs a character-by-character traversal of the trie:- Starting at the root node, iterate through each character of the input text
- For each character, check if a corresponding child node exists
- If any character is not found, return false immediately
- After traversing all characters, check if the final node has a target (indicating a complete source word)
- Return true only if a target exists at the final node
Runtime complexity
The complexity is O(k) where:k= length of the text string to look up
Use cases
- Validation: Verify that expected sources were added to the trie after building
- Testing: Confirm rule coverage in unit tests
- Debugging: Check if a specific source is present when troubleshooting replacement issues
- Rule management: Determine if a source already exists before adding new rules
Related functions
- buildTrie - Constructs the trie that this function searches
- containsTarget - Checks if a target string exists in the trie
- searchAndReplace - Uses the trie to perform text replacements