I am trying to get all Values from Keys that contain the same substring. For example:
If a Key's string is "AAABBB' and another Key's string is 'XXXBBB' I want to get the Value from both of those Keys. (Since BBB matches)
The relationship of the substring match should be 3 characters in length. The prefix is from index 0-3 and the suffix index is from 3-6.
For example: AAABBB (AAA is the suffix and BBB is the prefix.)
(The relationship AABBBA is ignored because AAB and BBA do not match.)
I'm trying to avoid using nested for loops because my algorithm will run very slow at O(N^2). I'm working on finding a solution with 1 HashMap and 1 for loop.
HashMap a = new HashMap();
map.put("AAABBB", 1);
map.put("CCCPPP", 2);
map.put("XXXBBB", 3);
map.put("AAAOOO",4);
for (Entry<String, String> entry : a.entrySet()) {
String prefix = entry.getKey().substring(0,3);
String suffix = entry.getKey().substring(3,6);
if(map.contains("ANY_SUBSTRING" + suffix){
System.out.println(map.get("ANY_SUBSTRING" + suffix);
}
}
Output: (1,3)
AAABBB => 1 XXXBBB => 3