Find if characters in string are present in another string

Multi tool use
up vote
0
down vote
favorite
I need to match if the characters in a string are present in a haystack with following conditions:
- If match string has repeated characters, haystack must contain multiple characters. In case of
hello
haystack must contain at-least 2l
. - Character insensitive.
- Order insensitive.
The example below worked well for me as passed all the use cases. Can it be done in a better way with least complexity? Or maybe without built-in functions like indexOf
, includes
, substr
. Though I am fine with map
, filter
like functions.
function array_diff(array1, array2) {
return array1.filter(e => array2.indexOf(e) === -1)
}
function compare(haystack, match) {
const haystackArr = haystack.toLowerCase().split('');
const matchArr = match.toLowerCase().split('');
return haystackArr.length >= array_diff(haystackArr, matchArr).length + matchArr.length;
}
console.log(compare('aldhedalfkjlhdlkjohd', 'hello'));
javascript performance memory-optimization
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I need to match if the characters in a string are present in a haystack with following conditions:
- If match string has repeated characters, haystack must contain multiple characters. In case of
hello
haystack must contain at-least 2l
. - Character insensitive.
- Order insensitive.
The example below worked well for me as passed all the use cases. Can it be done in a better way with least complexity? Or maybe without built-in functions like indexOf
, includes
, substr
. Though I am fine with map
, filter
like functions.
function array_diff(array1, array2) {
return array1.filter(e => array2.indexOf(e) === -1)
}
function compare(haystack, match) {
const haystackArr = haystack.toLowerCase().split('');
const matchArr = match.toLowerCase().split('');
return haystackArr.length >= array_diff(haystackArr, matchArr).length + matchArr.length;
}
console.log(compare('aldhedalfkjlhdlkjohd', 'hello'));
javascript performance memory-optimization
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to match if the characters in a string are present in a haystack with following conditions:
- If match string has repeated characters, haystack must contain multiple characters. In case of
hello
haystack must contain at-least 2l
. - Character insensitive.
- Order insensitive.
The example below worked well for me as passed all the use cases. Can it be done in a better way with least complexity? Or maybe without built-in functions like indexOf
, includes
, substr
. Though I am fine with map
, filter
like functions.
function array_diff(array1, array2) {
return array1.filter(e => array2.indexOf(e) === -1)
}
function compare(haystack, match) {
const haystackArr = haystack.toLowerCase().split('');
const matchArr = match.toLowerCase().split('');
return haystackArr.length >= array_diff(haystackArr, matchArr).length + matchArr.length;
}
console.log(compare('aldhedalfkjlhdlkjohd', 'hello'));
javascript performance memory-optimization
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I need to match if the characters in a string are present in a haystack with following conditions:
- If match string has repeated characters, haystack must contain multiple characters. In case of
hello
haystack must contain at-least 2l
. - Character insensitive.
- Order insensitive.
The example below worked well for me as passed all the use cases. Can it be done in a better way with least complexity? Or maybe without built-in functions like indexOf
, includes
, substr
. Though I am fine with map
, filter
like functions.
function array_diff(array1, array2) {
return array1.filter(e => array2.indexOf(e) === -1)
}
function compare(haystack, match) {
const haystackArr = haystack.toLowerCase().split('');
const matchArr = match.toLowerCase().split('');
return haystackArr.length >= array_diff(haystackArr, matchArr).length + matchArr.length;
}
console.log(compare('aldhedalfkjlhdlkjohd', 'hello'));
function array_diff(array1, array2) {
return array1.filter(e => array2.indexOf(e) === -1)
}
function compare(haystack, match) {
const haystackArr = haystack.toLowerCase().split('');
const matchArr = match.toLowerCase().split('');
return haystackArr.length >= array_diff(haystackArr, matchArr).length + matchArr.length;
}
console.log(compare('aldhedalfkjlhdlkjohd', 'hello'));
function array_diff(array1, array2) {
return array1.filter(e => array2.indexOf(e) === -1)
}
function compare(haystack, match) {
const haystackArr = haystack.toLowerCase().split('');
const matchArr = match.toLowerCase().split('');
return haystackArr.length >= array_diff(haystackArr, matchArr).length + matchArr.length;
}
console.log(compare('aldhedalfkjlhdlkjohd', 'hello'));
javascript performance memory-optimization
javascript performance memory-optimization
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 11 mins ago
Vikash Tiwari
1
1
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Vikash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Vikash Tiwari is a new contributor. Be nice, and check out our Code of Conduct.
Vikash Tiwari is a new contributor. Be nice, and check out our Code of Conduct.
Vikash Tiwari is a new contributor. Be nice, and check out our Code of Conduct.
Vikash Tiwari is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208670%2ffind-if-characters-in-string-are-present-in-another-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
E35VnULg0lpM7c6HVM,fRaYZbx