Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rfaisal/5954331 to your computer and use it in GitHub Desktop.

Select an option

Save rfaisal/5954331 to your computer and use it in GitHub Desktop.
public class SimpleSpamDetectorSlightlyInefficient {
public static boolean doesDuplicatingMatch(String s,int s_i, int s_j, String p,int p_i, int p_j){
if(p_j-p_i>s_j-s_i) //if pattern is larger than the original string
return false;
else if(s_j-s_i==0 && p_j-p_i==0) //if both empty
return true;
else if(s_j<s_i||p_j<p_i)
return false;
else if(Character.toLowerCase(s.charAt(s_i)) == Character.toLowerCase(p.charAt(p_i)))
return doesDuplicatingMatch(s,s_i+1,s_j,p,p_i+1,p_j)
||doesDuplicatingMatch(s,s_i+1,s_j,p,p_i,p_j);
else
return false;
}
public static int countKeywords(String subjectLine, String[] keywords){
int s=0;
int count=0;
boolean isSpace=false;
for(int i=0;i<subjectLine.length();i++){
if(subjectLine.charAt(i)==' '){
if(!isSpace){
for(String p:keywords){
if(doesDuplicatingMatch(subjectLine,s,i-1,p,0,p.length()-1)){
count++;
break;
}
}
}
isSpace=true;
}
else{
if(isSpace){
s=i;
isSpace=false;
}
}
}
for(String p:keywords){
if(doesDuplicatingMatch(subjectLine,s,subjectLine.length()-1,p,0,p.length()-1)){
count++;
break;
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment