check string end among a list of strings

Hello,

I need to write a function that checks the end of a string among a list of strings. I don't want to know which string matches, I just need a true/false value to tell me whether any string matches or not.

First, I tried to call the fucntion String.endsWith() with each one separately. Then, I used the Patten, Matcher classes to check against regular expression in the form ".*("+end1+"|"+end2+"|"+...+"|"+endx+")". It worked well but it was much slower. Do I need to write a complete algorithm by my self, or there is a fast method in java that will do the work for me?

[598 byte] By [Ahmed.Saada] at [2007-10-2 1:40:29]
# 1
A for loop and endsWith() is probably your best bet.You could build a specialized finite state machine. Sort of a regex matcher optimized for this special case. Could be a bit faster in some circumstances. More likely a hundred times more code for little or no improvement.
sjasjaa at 2007-7-15 19:04:04 > top of Java-index,Other Topics,Algorithms...
# 2
Trie trie = new Trie();for each wordword = reverse(word)trie.add(word)***System.out.println(trie.hasPrefix(searchString)); http://www.graphbuilder.com/trie/
rkippena at 2007-7-15 19:04:04 > top of Java-index,Other Topics,Algorithms...
# 3

> Hello,

> I need to write a function that checks the end of a

> a string among a list of strings. I don't want to

> know which string matches, I just need a true/false

> value to tell me whether any string matches or not.

> First, I tried to call the fucntion

> on String.endsWith() with each one separately. Then,

> I used the Patten, Matcher classes to check against

> regular expression in the form

> ".*("+end1+"|"+end2+"|"+...+"|"+endx+")". It worked

> well but it was much slower. Do I need to write a

> complete algorithm by my self, or there is a fast

> method in java that will do the work for me?

try

"^.*"+end1+"$|^.*"+end2+"$|^.*"+...+"$|^.*"+endx+"$"

It may improve the performance

matfuda at 2007-7-15 19:04:04 > top of Java-index,Other Topics,Algorithms...
# 4

This Trie is what I thought of.

Actually I built it before I read this reply as I was in a hurry (I named it TestTree).

Mine is better in that it does not have to inverse strings as it is build in from rear to start of string.

I will try it anyway because it may be better.

Thank you all.

Here's my implementation

- TreeNode.java --

import java.util.LinkedHashMap;

class TreeNode<V> extends LinkedHashMap<Character,TreeNode><V>> {

public V value;

public TreeNode() {

super();

}

public TreeNode(V value) {

super();

this.value = value;

}

}

--

TestTree.java --

import java.util.Collection;

/**

* Builds a tree that has a character over each branch,

* and tests a string if it has a path in this tree.

* We add strings and check them in reverse order because

* this is what we need from it.

* @author Ahmed Saad

*/

@SuppressWarnings("unchecked")

public class TestTree {

/**The root node of the TestTree.*/

private TreeNode<?> root;

/**

* Builds an empty tree

*/

public TestTree() {

root = new TreeNode();

}

/**

* Adds the given String to the TestTree.

* It checks character one by one starting from the end,

* it adds nodes in the tree so that there is a path from the root with the given string

* @param s the string to be added

*/

public void add(String s) {

TreeNode<?> p = root;

for (int i=s.length()-1;i>=0;i--) {

char c = s.charAt(i);

TreeNode q = p.get(c);

if (q==null) {

q = new TreeNode();

p.put(c,q);

}

p = q;

}

}

/**

* Add all given strings to the tree using {link #add(String)}

* @param cs

*/

public void addAll(Collection<String> cs) {

for (String s:cs)

add(s);

}

/**

* Checks the given String in the TestTree.

* It checks character of the string one bye one starting from the rear.

*

* @param s the string to be checked

*/

public boolean contains(String s) {

TreeNode<?> p = root;

int i=s.length();

while (--i>=0) {

//get next character

char c = s.charAt(i);

//get the child node under the next character

TreeNode<?> q = p.get(c);

//no branch with this character

if (q==null)

return false;

//if this is a leaf node, we have finished

if (q.isEmpty())

return true;

p=q;

}

return false;

}

public static void main(String args[]) {

TestTree tt = new TestTree();

tt.add("er");

tt.add("or");

tt.add("ion");

tt.add("ed");

System.err.println(tt.contains("ionor"));

}

}

Ahmed.Saada at 2007-7-15 19:04:04 > top of Java-index,Other Topics,Algorithms...
# 5

Dear Ahmed,

i was trying to make a tree that takes inputs from linked list but it was very hard on me as im not an expert in java, my each node in the tree should contain a string item and a counter for it then based on their count they will either join or be pruned from the tree ( SPADE algorithm ) so i wonder if have a code that might help me with this coz im having hard time with it.

Thanks a lot

Saad Ashmawi

SaadAsha at 2007-7-15 19:04:04 > top of Java-index,Other Topics,Algorithms...