Skip to content

Instantly share code, notes, and snippets.

@yssharma
Created November 16, 2012 07:06
Show Gist options
  • Select an option

  • Save yssharma/4084994 to your computer and use it in GitHub Desktop.

Select an option

Save yssharma/4084994 to your computer and use it in GitHub Desktop.
Sum of LCP's of string with each of its suffix - InArray Linear Approach
package strings;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class StringSimilarity {
public static void solution1(){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
String input = null;
char[] in = null;
int len = 0;
int pos=0;
int overlap=0;
Map<String, Integer> map = new HashMap<String, Integer>();
for(int i=0; i<num; i++){
overlap=0;
input = sc.next();
in = input.toCharArray();
if(null!=map.get(input)){
System.out.println(map.get(input));
continue;
}
len = in.length;
for(int j=0; j<len; j++){
pos = j;
for(int k=0; pos<len; k++){
if(in[pos]==in[k]){
overlap++;
pos++;
}
else break;
}
}
System.out.println(overlap);
map.put(input, overlap);
}
}
public static void main(String[] args){
StringSimilarity.solution1();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment