How to extract the leftmost characters in a string list?

Suppose I have the following list of string objects:

ABC1, ABC2, ABC_Whatever 

What is the most efficient way to extract the left most common characters from this list? So I would get ABC in my case.

+6
source share
7 answers
+6
source

This will work for you.

  public static void main(String args[]) { String commonInFirstTwo=greatestCommon("ABC1","ABC2"); String commonInLastTwo=greatestCommon("ABC2","ABC_Whatever"); System.out.println(greatestCommon(commonInFirstTwo,commonInLastTwo)); } public static String greatestCommon(String a, String b) { int minLength = Math.min(a.length(), b.length()); for (int i = 0; i < minLength; i++) { if (a.charAt(i) != b.charAt(i)) { return a.substring(0, i); } } return a.substring(0, minLength); } 
0
source

You have all the substrings of words in this list and keep track of their substrings. The one with the maximum occurrences is the one you want. Here is an example implementation. It returns the most common substring

 static String mostCommon(List<String> list) { Map<String, Integer> word2Freq = new HashMap<String, Integer>(); String maxFreqWord = null; int maxFreq = 0; for (String word : list) { for (int i = 0; i < word.length(); ++i) { String sub = word.substring(0, i + 1); Integer f = word2Freq.get(sub); if (f == null) { f = 0; } word2Freq.put(sub, f + 1); if (f + 1 > maxFreq) { if (maxFreqWord == null || maxFreqWord.length() < sub.length()) { maxFreq = f + 1; maxFreqWord = sub; } } } } return maxFreqWord; } 

The above implementation may not be sufficient if you use more than one common substring. Use the card inside it.

 System.out.println(mostCommon(Arrays.asList("ABC1", "ABC2", "ABC_Whatever"))); System.out.println(mostCommon(Arrays.asList("ABCDEFG1", "ABGG2", "ABC11_Whatever"))); 

Returns

 ABC AB 
0
source

Your problem is simply to rephrase the standard problem of finding the longest common prefix

0
source

If you know what common characters are, you can check if other strings contain these characters using the .contains () method.

0
source

If you want to use a third-party library, then the following: jOOλ generates this prefix for you:

 String prefix = Seq.of("ABC1", "ABC2", "ABC_Whatever").commonPrefix(); 

Disclaimer: I work for jOOλ

0
source

if there are N lines, and the minimum length among them is charterers M, then the most efficient (correct) answer will be N * M in the worst case (when all lines are the same).

 outer loop - each character of first string at a time inner loop - each of the strings test - each charterer of the string in inner loop against the charterer in outer loop. 

performance can be tuned to (N-1) * M if we do not test the first line in the inner loop

-1
source

Source: https://habr.com/ru/post/951462/


All Articles