#Find most common substring
1 messages · Page 1 of 1 (latest)
;compile java
import java.util.*;
public class M {
public static String findCommonSubstring(final String str)
{
if (str == null || str.isEmpty()) return "";
final int n = str.length();
final Map<String, Integer> map = new LinkedHashMap<String, Integer>(n * n * n);
int maxValue = 1;
String maxString = str;
for(int i = 0; i < n; ++i)
{
for(int j = i; j >= 0; --j)
if ((j-i) > 0)
{
final String key = str.substring(j, i);
final Integer v = map.get(key);
System.out.println("[" + j + "," + i + "]["+key+"] = "+ v);
if (v == null || v.intValue() < 1)
{ map.put(key, Integer.valueOf(1)); }
else {
final int x = 1 + v.intValue();
map.put(key, Integer.valueOf(x));
if (x > maxValue) { maxValue = x; maxString = key; }
}
}
}
return maxString;
}
public static void main(String[] args)
{
final String x1 = findCommonSubstring("aa");
System.out.println(x1);
final String x2 = findCommonSubstring("abcd");
System.out.println(x2);
final String x3 = findCommonSubstring("abcdaaaaaaaaaaa");
System.out.println(x3);
}
}
Program Output
aa
abcd
abcdaaaaaaaaaaa
fd26 | 116ms | java | jdk 21.0.2 | godbolt.org
;compile java
import java.util.*;
public class M {
public static String findCommonSubstring(final String str, int lookup)
{
if (str == null || str.isEmpty()) return "";
if (lookup < 1) { lookup = 0; }
if (str.length() < lookup) return "";
final int n = str.length();
final Map<String, Integer> map = new LinkedHashMap<String, Integer>(n * n * n);
int maxValue = 1;
String maxString = "";
for(int i = 0; i < n; ++i)
{
for(int j = i; j >= 0; --j)
if ( ((j-i) > 0)
&& ( (lookup < 1) || ( (j-i) == lookup) )
)
{
final String key = str.substring(j, i);
final Integer v = map.get(key);
System.out.println("[" + j + "," + i + "]["+key+"] = "+ v);
if (v == null || v.intValue() < 1)
{ map.put(key, Integer.valueOf(1)); }
else {
final int x = 1 + v.intValue();
map.put(key, Integer.valueOf(x));
//if (x > maxValue) { maxValue = x; maxString = key; }
}
final int m = (j-i);
int count = 0;
for(int k = 0; k < (n-m); ++k)
try
{
final String s = str.substring(k, k+m);
if (key.equals(s)) { ++count; }
}
catch(Exception ex) {}
System.out.println(count);
if (count > maxValue) { maxValue = count; maxString = key; }
}
}
return maxString;
}
public static void main(String[] args)
{
int lookup = 3;
final String x1 = findCommonSubstring("aaa",lookup);
System.out.println(x1);
final String x2 = findCommonSubstring("abcd",lookup);
System.out.println(x2);
final String x3 = findCommonSubstring("abcdaaaaaaaaaaa",lookup);
System.out.println(x3);
}
}
Compilation successful
No output.
fd26 | 85ms | java | jdk 21.0.2 | godbolt.org
;compile java
import java.util.*;
public class M {
public static String findCommonSubstring(final String str, int lookup) {
if (str == null || str.isEmpty()) return "< EMPTY >";
if (lookup < 1) lookup = 0;
if (str.length() < lookup) return "< TOO SMALL >";
final int n = str.length();
final Map<String, Integer> map = new LinkedHashMap<String, Integer>(n * n * n);
int maxValue = 1;
String maxString = null;
for (int i = 0; i < n; ++i)
{
for (int j = i; j >= 0; --j)
if (
((j - i) > 0) &&
((lookup < 1) || ((j - i) == lookup))
)
{
final String key = str.substring(j, i);
final Integer v = map.get(key);
System.out.println("[" + j + "," + i + "][" + key + "] = " + v);
if (v == null || v.intValue() < 1) {
map.put(key, Integer.valueOf(1));
} else {
final int x = 1 + v.intValue();
map.put(key, Integer.valueOf(x));
//if (x > maxValue) { maxValue = x; maxString = key; }
}
final int m = (j - i);
int count = 0;
for (int k = 0; k < (n - m); ++k) {
try {
final String s = str.substring(k, k + m);
if (key.equals(s)) {
++count;
}
}
catch (Exception ex) {}
}
System.out.println(count);
if (count > maxValue) {
maxValue = count;
maxString = key;
}
}
}
return maxString;
}
public static void main(String[] args) {
int lookup = 3;
final String x1 = findCommonSubstring("aaa", lookup);
System.out.println(x1);
final String x2 = findCommonSubstring("abcd", lookup);
System.out.println(x2);
final String x3 = findCommonSubstring("abcdaaaaaaaaaaa", lookup);
System.out.println(x3);
}
}
Program Output
null
null
null
fd26 | 112ms | java | jdk 21.0.2 | godbolt.org
;compile java
import java.util.*;
public class M {
public static String findCommonSubstring(final String str, int lookup) {
if (str == null || str.isEmpty()) return "< EMPTY >";
if (lookup < 1) lookup = 0;
if (str.length() < lookup) return "< TOO SMALL >";
int n = str.length();
Map<String,Integer> map = new LinkedHashMap<String,Integer>(n*n*n);
int maxValue = -1;
String maxString = null;
for (int i = 0; i < n; ++i) {
for (int j = i; j >= 0; --j) {
int ji = i-j;
//System.out.println("[" + j + "," + i + "][" +ji + "]");
if (ji >= 0) {
//if((lookup < 1) || (ji == lookup))
{
final String key = str.substring(j, i);
final Integer v = map.get(key);
//System.out.println("[" + j + "," + i + "][" + key + "] = " + v);
if (v == null || v.intValue() < 1) {
map.put(key, Integer.valueOf(1));
} else {
final int x = 1 + v.intValue();
map.put(key, Integer.valueOf(x));
//if (x > maxValue) { maxValue = x; maxString = key; }
}
int count = 0;
for (int k = 0; k < (n - ji); ++k) {
try {
String s = str.substring(k, k + ji);
if (key.equals(s)) {
++count;
}
}catch(Exception ex){}
}
System.out.println(key+":"+count);
if (count > maxValue) {
maxValue = count;
maxString = key;
}
}
}
}
}
System.out.println(maxString+"::"+maxValue );
return maxString;
}
public static void main(String[] args) {
int lookup = 3;
final String x1 = findCommonSubstring("aaa", lookup);
System.out.println(x1);
/*
final String x2 = findCommonSubstring("abcd", lookup);
System.out.println(x2);
final String x3 = findCommonSubstring("abcdaaaaaaaaaaa", lookup);
System.out.println(x3);*/
}
}