In: Computer Science
Write a SuffixArray client that, given a string and an
integer L, finds all repeated substrings of length L or
more.(Please give solution in java)
Hi ,
PFB the class. Please comment for any queries/feedbacks.
Thanks,
Anita
SuffixArray.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class SuffixArray
{
public Map<String, Integer>
findOccurences(String str, int length) {
Map<String, Integer> map = new HashMap<>();
int limit = str.length() - length + 1;
for (int i = 0; i < limit; i++) {
String sub = str.substring(i, i + length);
Integer counter = map.get(sub);
if (counter == null) {
counter = 0;
}
map.put(sub, ++counter);
}
Iterator iter = map.entrySet().iterator();
while(iter.hasNext()){
Entry entry = (Entry)iter.next();
int len = (int)entry.getValue();
if(len==length){
System.out.println("
"+entry.getKey()+ " ");
}
}
return map;
}
public static void main(String...arg)throws IOException
{
String text = "";
int L = 0;
String lStr="";
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Text String ");
text = reader.readLine();
System.out.println("Enter the value of L: ");
lStr = reader.readLine();
L= Integer.valueOf(lStr);
SuffixArray suffixarray = new SuffixArray();
suffixarray.findOccurences(text,L);
}
}
Sample ouput:
Enter the Text String
banana
Enter the value of L:
2
na
an