From efbfe88a7cf3dfc630f6f02b9bfbe434c1b59489 Mon Sep 17 00:00:00 2001 From: Tomasz Polgrabia Date: Sat, 4 Jan 2025 00:09:49 +0100 Subject: [PATCH] Implemented task per content. However, real T9 would guess fitting words also by frequency of usage #3. --- .../crackingcodeinterview/t16x20/RstTree.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/2025/01/cracking_code_interview_16_20_t9/src/main/java/pl/polgrabia/demos/crackingcodeinterview/t16x20/RstTree.java b/2025/01/cracking_code_interview_16_20_t9/src/main/java/pl/polgrabia/demos/crackingcodeinterview/t16x20/RstTree.java index 7ecf6fe..1a9e879 100644 --- a/2025/01/cracking_code_interview_16_20_t9/src/main/java/pl/polgrabia/demos/crackingcodeinterview/t16x20/RstTree.java +++ b/2025/01/cracking_code_interview_16_20_t9/src/main/java/pl/polgrabia/demos/crackingcodeinterview/t16x20/RstTree.java @@ -58,7 +58,7 @@ public class RstTree { assert !word.isBlank() : "word must not be blank"; assert word.matches("^[a-z]+$") : "word '" + word + "' must contain only small letters"; - RstTreeNode searchedNode = lookupNodeByPrefixOrValueInternal(rstTreeRoot, word, SearchMode.VALUE); + RstTreeNode searchedNode = lookupNodesBySearchKeyInternal(rstTreeRoot, word, SearchMode.VALUE); if (searchedNode == null) { return false; } @@ -67,7 +67,7 @@ public class RstTree { return word.equals(value); } - private RstTreeNode lookupNodeByPrefixOrValueInternal(RstTreeNode node, String searchValue, SearchMode searchMode) { + private RstTreeNode lookupNodesBySearchKeyInternal(RstTreeNode node, String searchValue, SearchMode searchMode) { if (node == null) { return null; } @@ -85,6 +85,11 @@ public class RstTree { return node; } + if (SearchMode.PREFIX.equals(searchMode) + && (node.getPrefix().startsWith(searchValue))) { + return node; + } + if (node.getPrefix().startsWith(searchValue)) { return null; } @@ -95,7 +100,7 @@ public class RstTree { // logger.info("Word: {}, prefix: {}, index: {}, c: {}, offset: {}", searchValue, node.getPrefix(), node.getPrefix().length(), c, offset); var childItem = node.getRstChildren().length <= offset ? null : node.getRstChildren()[offset]; // logger.info("Childitem: {}, c: {}, offset: {}", childItem, c, offset); - return lookupNodeByPrefixOrValueInternal(childItem, searchValue, SearchMode.VALUE); + return lookupNodesBySearchKeyInternal(childItem, searchValue, SearchMode.VALUE); } public void dfsByPrefix(String prefix, Consumer wordConsumer) { @@ -103,7 +108,7 @@ public class RstTree { assert !prefix.isBlank() : "prefix must not be blank"; assert prefix.matches("^[a-z]+$") : "prefix '" + prefix + "' must contain only small letters"; - RstTreeNode node = lookupNodeByPrefixOrValueInternal(rstTreeRoot, prefix, SearchMode.PREFIX_OR_VALUE); + RstTreeNode node = lookupNodesBySearchKeyInternal(rstTreeRoot, prefix, SearchMode.PREFIX_OR_VALUE); dfs(node, wordConsumer); } @@ -119,6 +124,7 @@ public class RstTree { private enum SearchMode { VALUE, - PREFIX_OR_VALUE + PREFIX_OR_VALUE, + PREFIX } }