Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fast way to find if two strings have a character in common

I have various string comparison and diff algorithms, but at some point, before I apply them I want to find if two strings have at least one character in common. This way I will be able to skip more complex functions. So I need a very fast function in JavaScript that will find if string A and string B has at least one common character.

First I was thinking to create a character map for a string A, and then check every character in a string B against that map until something is found. But then I realized that if both strings are huge and they have a common first character, this will be inefficient to create a full map for string A.

UPDATE: someone answered with using indexOf(), this confuses me. Maybe the phrase "have character in common" means the same as "string is a substring of another"? Let me give an example of what I want:

For example JavaScript and Stop and stay have a character S in common. Other example would be please look right and break the ice they have a character k in common.

like image 771
exebook Avatar asked Dec 19 '13 07:12

exebook


People also ask

How do you check if two strings have common characters?

For every character in string 1 we increment vector index of that character eg: v[s1[i]-'a']++, for every character of string 2 we check vector for the common characters if v[s2[i]-'a'] > 0 then set flag = true and v[s2[i]-'a']– such that one character of string 2 is compared with only one character of string 1.

How do you find the common substring between two strings?

Approach: Let m and n be the lengths of the first and second strings respectively. Dynamic Programming can be used to find the longest common substring in O(m*n) time. The idea is to find the length of the longest common suffix for all substrings of both strings and store these lengths in a table.

How do I check if two strings have the same character in Python?

String Comparison using == in PythonThe == function compares the values of two strings and returns if they are equal or not. If the strings are equal, it returns True, otherwise it returns False.

How do you find the common string between two strings in python?

To find common substrings between two strings with Python, we can use the difflib module. We have 2 strings string1 and string2 that we want to find the common substring that's in both strings. To do that, we use the SequenceMatcher class with string1 and string2 .


1 Answers

Simplest method would be to loop through each letter in one string and see if the other contains any of the single letters.

You can't really get more efficient than to go over each letter unless the strings are sorted in alphabetical order to start with.

function anythingInCommon(a, b){
    if( b.length < a.length )
        return anythingInCommon(b, a)

    for( var i = 0, len = a.length; i < len; i++ ) 
        if(b.indexOf(a[i]) != -1)
            return true;
  
    return false
}

console.log(
  anythingInCommon("aaaaaaaaaaaaaabbbbbbccccc", "xc"),
  anythingInCommon("aaaaaaaaaaaaaabbbbbbccccc", "x")
)
anythingInCommon('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','cddabddde')

Fiddler: http://jsfiddle.net/LRxGK/4/

like image 142
JensB Avatar answered Sep 23 '22 09:09

JensB