Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can also search a word with O(n) complexity provided that you build an appropriate internal data structure from your <code>List&lt;List&lt;String&gt;&gt;</code>. Here's an example:</p> <pre><code>public class SO5890087 { Map&lt;Character, List&lt;Position&gt;&gt; pmaps = new HashMap&lt;Character, List&lt;Position&gt;&gt;(); public static void main(String[] args) { String[][] strings = new String[][] { { "r", "x", "f", "b", "e", "a", "r" }, { "a", "r", "q", "b", "o", "y", "t" }, { "s", "q", "z", "b", "s", "b", "r" } }; List&lt;List&lt;String&gt;&gt; sss = new ArrayList&lt;List&lt;String&gt;&gt;(); for (int i = 0; i &lt; strings.length; i++) sss.add(new ArrayList&lt;String&gt;(Arrays.asList(strings[i]))); SO5890087 finder = new SO5890087(sss); List&lt;Position&gt; positions = finder.findWord("bob"); for (Position position : positions) System.out.println(position); } public SO5890087(List&lt;List&lt;String&gt;&gt; sss) { for (int i = 0; i &lt; sss.size(); i++) { List&lt;String&gt; ss = sss.get(i); for (int j = 0; j &lt; ss.size(); j++) { Character c = ss.get(j).charAt(0); if (! pmaps.containsKey(c)) pmaps.put(c, new ArrayList&lt;Position&gt;()); pmaps.get(c).add(new Position(i, j)); } } } public List&lt;Position&gt; findWord(String word) { List&lt;Position&gt; result = new ArrayList&lt;Position&gt;(); char[] cs = word.toCharArray(); for (int i = 0; i &lt; cs.length; i++) { if (pmaps.containsKey(cs[i])) { result.add(pmaps.get(cs[i]).get(0)); } else { result.clear(); break; } } return result; } class Position { int list; int index; public Position(int list, int index) { this.list = list; this.index = index; } public int getList() { return list; } public int getIndex() { return index; } @Override public String toString() { return "Position [list=" + list + ", index=" + index + "]"; } } } </code></pre> <p>Output for <code>"bob"</code>:</p> <pre><code>Position [list=0, index=3] Position [list=1, index=4] Position [list=0, index=3] </code></pre> <p>Notes:</p> <ul> <li><p>a <code>Position</code> object identify a <code>list</code> and an <code>index</code> in that list</p></li> <li><p>at construction time I build a <code>Map</code> where keys are characters, and values are list of <code>Position</code> objects, each telling me in which list an and which index in that list I can find that character</p></li> <li><p>when finding a word, I simply scan the word characters and retrieve the first available <code>Position</code> for that character in my <code>Map</code></p></li> <li><p>you can easily modify the <code>findWord</code> method to return all possible combinations</p></li> <li><p>you can easily add method to update the internal <code>Map</code> with additional string in an existing list, or with new lists</p></li> <li><p>complexity: initialization is O(n^2) (must scan the list of lists of strings); finding a word is O(n)</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload