So I have array of String, and I'd like to see if one has (contains) others as part of String.
For example, consider following simple array.
s[0]="Java"
s[1]="Java Programming"
s[2]="C Programming"
s[3]="C Programming is Cool"
In the end, I only want to keep
s[1]="Java Programming"
s[3]="C Programming is Cool"
because s[1] contains s[0] and s[3] contains s[2].
This is my code to detect if array element contains array element using String.Contains() method, which seems really basic and inefficient..
int startPtr = 0;
while (startPtr < s.length-1) {
int tempPtr = startPtr+1;
while (tempPtr <= s.length-1) {
if (s[tempPtr].contains(s[startPtr])) {
//At this point, I know that I don't need s[startPtr] in result.
//Remove item at startPtr, if this were ArrayList or something.
startPtr++;
break;
} else { indexPtr++; }
}
And after startPtr reaches end, I think I have to do the same thing in reverse order (start from the end and check towards beginning of the array) to ensure no string is part of other string element.
Can someone help me with better algorithm? Also, I believe this alogirthm will have O(N^2), am I correct?
contains().