I am just checking for an efficient algorithm with best computational complexity to check if a child string - tobeVerified exists in a huge parent string
I was going through different algorithms but I am yet to find something which offers O(n)
I came up with the below implementation using HashSet which is gives me O(n+m) ~ O(n)
I wanted to check if this is the right way of doing it or if any other optimization is possible. But in this approach there is problem of consuming more space
String parent = "the value is very high";
String tobeVerified = "is";
Set wordSet = new HashSet<String>();
String[] words = parent.trim().toUpperCase().split("\\s+");
//This is O(n) n - Parent Size m - substring size
for(String word: words){
wordSet.add(word);
}
//This is O(1)
System.out.println(wordSet.contains(tobeVerified.toUpperCase()));
}
String.contains()has a complexity ofO(m*n). It again callsindexOf()which has this complexity .. please check here - stackoverflow.com/questions/12752274/…