Find same characters in 2 stringsFind ASCII codes and replace with charactersTypeahead Talent BuddyFind...
Basic combinatorial probability problem
Does a log transform always bring a distribution closer to normal?
The screen of my macbook suddenly broken down how can I do to recover
Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?
On a tidally locked planet, would time be quantized?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
Is there any references on the tensor product of presentable (1-)categories?
Freedom of speech and where it applies
What was this official D&D 3.5e Lovecraft-flavored rulebook?
Why a symmetric relation is defined: ∀x∀y( xRy⟹yRx) and not ∀x∀y (xRy⟺yRx)?
Biological Blimps: Propulsion
What is Cash Advance APR?
Fear of getting stuck on one programming language / technology that is not used in my country
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
Not using 's' for he/she/it
Is aluminum electrical wire used on aircraft?
How do you make your own symbol when Detexify fails?
What should you do if you miss a job interview (deliberately)?
Store Credit Card Information in Password Manager?
Redundant comparison & "if" before assignment
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Is preaching recommended or mandatory to a temple priest?
Are paving bricks differently sized for sand bedding vs mortar bedding?
Creepy dinosaur pc game identification
Find same characters in 2 strings
Find ASCII codes and replace with charactersTypeahead Talent BuddyFind function for strings in cellsCleaning up and reformatting imported data in an Excel sheetNumber of sub strings with same first and last characterCodeEval solutions to find trailing stringsGenerate strings of random charactersBinary search for a missing or default value by a given formulaFind spanning tree with maximum edges with the same weightPHP class that behaves like enums
$begingroup$
I have a solution, which compares 2 given strings and returns the number of total matching characters.
I know I haven't initialized the variables and arrays and that this (correctly) yields warnings. I know the variables aren't self-explanatory as they should be.
function commonCharacterCount($s1, $s2) {
$a = (strlen($s1) > strlen($s2)) ? $s2 : $s1;
$b = (strlen($s1) > strlen($s2)) ? $s1 : $s2;
foreach(count_chars($a, 1) as $i => $v) {
$c[chr($i)] = $v;
}
foreach (count_chars($b, 1) as $i => $v) {
$d[chr($i)] = $v;
}
$t = 0;
foreach($c as $k => $v) {
if($c[$k] <= $d[$k]) {
$t += $c[$k];
} else {
$t += $d[$k];
}
}
return $t;
}
For instance, I have the two strings:
$s1 = "abacadeee";
and $s2 = "aabbccddee";
, the expected output would be 7
.
As required, this solution works so far and you can test it here:
sandbox
Which steps are unnecessary and how can I improve this algorithm?
performance php
$endgroup$
add a comment |
$begingroup$
I have a solution, which compares 2 given strings and returns the number of total matching characters.
I know I haven't initialized the variables and arrays and that this (correctly) yields warnings. I know the variables aren't self-explanatory as they should be.
function commonCharacterCount($s1, $s2) {
$a = (strlen($s1) > strlen($s2)) ? $s2 : $s1;
$b = (strlen($s1) > strlen($s2)) ? $s1 : $s2;
foreach(count_chars($a, 1) as $i => $v) {
$c[chr($i)] = $v;
}
foreach (count_chars($b, 1) as $i => $v) {
$d[chr($i)] = $v;
}
$t = 0;
foreach($c as $k => $v) {
if($c[$k] <= $d[$k]) {
$t += $c[$k];
} else {
$t += $d[$k];
}
}
return $t;
}
For instance, I have the two strings:
$s1 = "abacadeee";
and $s2 = "aabbccddee";
, the expected output would be 7
.
As required, this solution works so far and you can test it here:
sandbox
Which steps are unnecessary and how can I improve this algorithm?
performance php
$endgroup$
1
$begingroup$
what is the logic of common character count? from you test data characters that matches are a, b, c, d, e = 5. Again if I count occurrence the value is not 7. or even i count maximum occurrence the value is not 7
$endgroup$
– Muhammed Imran Hussain
Mar 14 at 15:22
add a comment |
$begingroup$
I have a solution, which compares 2 given strings and returns the number of total matching characters.
I know I haven't initialized the variables and arrays and that this (correctly) yields warnings. I know the variables aren't self-explanatory as they should be.
function commonCharacterCount($s1, $s2) {
$a = (strlen($s1) > strlen($s2)) ? $s2 : $s1;
$b = (strlen($s1) > strlen($s2)) ? $s1 : $s2;
foreach(count_chars($a, 1) as $i => $v) {
$c[chr($i)] = $v;
}
foreach (count_chars($b, 1) as $i => $v) {
$d[chr($i)] = $v;
}
$t = 0;
foreach($c as $k => $v) {
if($c[$k] <= $d[$k]) {
$t += $c[$k];
} else {
$t += $d[$k];
}
}
return $t;
}
For instance, I have the two strings:
$s1 = "abacadeee";
and $s2 = "aabbccddee";
, the expected output would be 7
.
As required, this solution works so far and you can test it here:
sandbox
Which steps are unnecessary and how can I improve this algorithm?
performance php
$endgroup$
I have a solution, which compares 2 given strings and returns the number of total matching characters.
I know I haven't initialized the variables and arrays and that this (correctly) yields warnings. I know the variables aren't self-explanatory as they should be.
function commonCharacterCount($s1, $s2) {
$a = (strlen($s1) > strlen($s2)) ? $s2 : $s1;
$b = (strlen($s1) > strlen($s2)) ? $s1 : $s2;
foreach(count_chars($a, 1) as $i => $v) {
$c[chr($i)] = $v;
}
foreach (count_chars($b, 1) as $i => $v) {
$d[chr($i)] = $v;
}
$t = 0;
foreach($c as $k => $v) {
if($c[$k] <= $d[$k]) {
$t += $c[$k];
} else {
$t += $d[$k];
}
}
return $t;
}
For instance, I have the two strings:
$s1 = "abacadeee";
and $s2 = "aabbccddee";
, the expected output would be 7
.
As required, this solution works so far and you can test it here:
sandbox
Which steps are unnecessary and how can I improve this algorithm?
performance php
performance php
edited Mar 14 at 15:11
Stephen Rauch
3,77061630
3,77061630
asked Mar 14 at 13:24
DasSaffeDasSaffe
1335
1335
1
$begingroup$
what is the logic of common character count? from you test data characters that matches are a, b, c, d, e = 5. Again if I count occurrence the value is not 7. or even i count maximum occurrence the value is not 7
$endgroup$
– Muhammed Imran Hussain
Mar 14 at 15:22
add a comment |
1
$begingroup$
what is the logic of common character count? from you test data characters that matches are a, b, c, d, e = 5. Again if I count occurrence the value is not 7. or even i count maximum occurrence the value is not 7
$endgroup$
– Muhammed Imran Hussain
Mar 14 at 15:22
1
1
$begingroup$
what is the logic of common character count? from you test data characters that matches are a, b, c, d, e = 5. Again if I count occurrence the value is not 7. or even i count maximum occurrence the value is not 7
$endgroup$
– Muhammed Imran Hussain
Mar 14 at 15:22
$begingroup$
what is the logic of common character count? from you test data characters that matches are a, b, c, d, e = 5. Again if I count occurrence the value is not 7. or even i count maximum occurrence the value is not 7
$endgroup$
– Muhammed Imran Hussain
Mar 14 at 15:22
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
I would recommend a process like this:
Code: (Demo)
$string1 = "abacadeee";
$string2 = "aabbccddee";
$counts2 = count_chars($string2, 1);
$tally = 0;
foreach (array_intersect_key(count_chars($string1, 1), $counts2) as $charcode1 => $count1) {
$tally += min($counts2[$charcode1], $count1);
}
echo $tally;
count_chars()
lends itself beautifully to this task, so using array functions onward is a sensible choice.
It is important to try to minimize iterations and not perform any useless iterations. By calling array_intersect_key()
on the two count_chars()
results, the foreach()
loop is only going to iterate elements with keys which are shared between the two arrays. In doing this, you don't need to check which array is smaller (which is otherwise how you would choose which array to iterate).
$tally
is incremented by the lesser of the two counts for each char.
p.s. calling chr()
is irrelevant to your objective.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f215420%2ffind-same-characters-in-2-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I would recommend a process like this:
Code: (Demo)
$string1 = "abacadeee";
$string2 = "aabbccddee";
$counts2 = count_chars($string2, 1);
$tally = 0;
foreach (array_intersect_key(count_chars($string1, 1), $counts2) as $charcode1 => $count1) {
$tally += min($counts2[$charcode1], $count1);
}
echo $tally;
count_chars()
lends itself beautifully to this task, so using array functions onward is a sensible choice.
It is important to try to minimize iterations and not perform any useless iterations. By calling array_intersect_key()
on the two count_chars()
results, the foreach()
loop is only going to iterate elements with keys which are shared between the two arrays. In doing this, you don't need to check which array is smaller (which is otherwise how you would choose which array to iterate).
$tally
is incremented by the lesser of the two counts for each char.
p.s. calling chr()
is irrelevant to your objective.
$endgroup$
add a comment |
$begingroup$
I would recommend a process like this:
Code: (Demo)
$string1 = "abacadeee";
$string2 = "aabbccddee";
$counts2 = count_chars($string2, 1);
$tally = 0;
foreach (array_intersect_key(count_chars($string1, 1), $counts2) as $charcode1 => $count1) {
$tally += min($counts2[$charcode1], $count1);
}
echo $tally;
count_chars()
lends itself beautifully to this task, so using array functions onward is a sensible choice.
It is important to try to minimize iterations and not perform any useless iterations. By calling array_intersect_key()
on the two count_chars()
results, the foreach()
loop is only going to iterate elements with keys which are shared between the two arrays. In doing this, you don't need to check which array is smaller (which is otherwise how you would choose which array to iterate).
$tally
is incremented by the lesser of the two counts for each char.
p.s. calling chr()
is irrelevant to your objective.
$endgroup$
add a comment |
$begingroup$
I would recommend a process like this:
Code: (Demo)
$string1 = "abacadeee";
$string2 = "aabbccddee";
$counts2 = count_chars($string2, 1);
$tally = 0;
foreach (array_intersect_key(count_chars($string1, 1), $counts2) as $charcode1 => $count1) {
$tally += min($counts2[$charcode1], $count1);
}
echo $tally;
count_chars()
lends itself beautifully to this task, so using array functions onward is a sensible choice.
It is important to try to minimize iterations and not perform any useless iterations. By calling array_intersect_key()
on the two count_chars()
results, the foreach()
loop is only going to iterate elements with keys which are shared between the two arrays. In doing this, you don't need to check which array is smaller (which is otherwise how you would choose which array to iterate).
$tally
is incremented by the lesser of the two counts for each char.
p.s. calling chr()
is irrelevant to your objective.
$endgroup$
I would recommend a process like this:
Code: (Demo)
$string1 = "abacadeee";
$string2 = "aabbccddee";
$counts2 = count_chars($string2, 1);
$tally = 0;
foreach (array_intersect_key(count_chars($string1, 1), $counts2) as $charcode1 => $count1) {
$tally += min($counts2[$charcode1], $count1);
}
echo $tally;
count_chars()
lends itself beautifully to this task, so using array functions onward is a sensible choice.
It is important to try to minimize iterations and not perform any useless iterations. By calling array_intersect_key()
on the two count_chars()
results, the foreach()
loop is only going to iterate elements with keys which are shared between the two arrays. In doing this, you don't need to check which array is smaller (which is otherwise how you would choose which array to iterate).
$tally
is incremented by the lesser of the two counts for each char.
p.s. calling chr()
is irrelevant to your objective.
answered Mar 18 at 12:50
mickmackusamickmackusa
1,739218
1,739218
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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%2f215420%2ffind-same-characters-in-2-strings%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
1
$begingroup$
what is the logic of common character count? from you test data characters that matches are a, b, c, d, e = 5. Again if I count occurrence the value is not 7. or even i count maximum occurrence the value is not 7
$endgroup$
– Muhammed Imran Hussain
Mar 14 at 15:22