How can I tell if two sets of strings have the same characters?

Source: Internet
Author: User
$a =2344;//The main string, fixed value.

The following variables are changeable
If $b=12;//determines whether a $ A string contains 1 and 2, the result is false
If $b=22;//determines whether a $ A string contains two 2, the result is only one 2, false
If $b=234;//determines if a $ A string contains 2,3,4.
If $b=324;//determines if a $ A string contains 3,2,4.
If $b=4432;//determines if a $ A string contains 4,4,3,2.
If $b=4444;//determines whether a $ A string contains 4 4, the result is a $ A of two 4, false
If $b
If $b
If $b
...... There will be a lot more behind.

I need to judge the result as above.
How to write a judgment statement?


Reply to discussion (solution)

$a = ' 2344 '; $d = Array (' n ', ' a ', ' 234 ', ' 324 ', ' 4432 ', ' 4444 '); $a = Count_chars ($a, 1); foreach ($d as $b) {  $t = Coun T_chars ($b, 1);  $f = 0;  if (Array_keys (Array_intersect_key ($t, $a)) = = Array_keys ($a)) $f = 1;  printf ("b =%s%s\n", $b, $f? ' True ': ' false ');}
b = 12 false b = 22 false b = 234 true b = 324 true b = 4432 true b = 4444 false

If $b=4444;//determines whether a $ A string contains 4 4, the result is a $ A of two 4, false
This rule seems inappropriate.

Split $ A and $b into groups by character, and then decide if they are subsets?

Do not know what you are doing this purpose?
There may be a better solution to start with business logic.

$a = ' 2344 '; $d = Array (' n ', ' a ', ' 234 ', ' 324 ', ' 4432 ', ' 4444 '); $a = Count_chars ($a, 1); foreach ($d as $b) {  $t = Coun T_chars ($b, 1);  $f = 0;  if (Array_keys (Array_intersect_key ($t, $a)) = = Array_keys ($a)) $f = 1;  printf ("b =%s%s\n", $b, $f? ' True ': ' false ');}
b = 12 false b = 22 false b = 234 true b = 324 true b = 4432 true b = 4444 false

If $b=4444;//determines whether a $ A string contains 4 4, the result is a $ A of two 4, false
This rule seems inappropriate.



There is still a flaw in this judgment.
If $ A is 4444.
The result I want is, only $b=4444, or $b=44, or $b=444 or $b=4, can match success, can be true, others do not meet,

I've already said that your rules are problematic (or incomplete).
The code I gave can only handle one case: all characters in the $a (de-duplicated) must appear in the $b

After you've made the perfect rules, you can expand the functionality.

First convert the string into the array, and then use Array_count_values to calculate the number of characters appearing in the $b, if there is less than or equal to $ A in the word count is true, otherwise it is false.

I've already said that your rules are problematic (or incomplete).
The code I gave can only handle one case: all characters in the $a (de-duplicated) must appear in the $b

After you've made the perfect rules, you can expand the functionality.



Just talk about my business needs, maybe I can understand a little bit,
It's like this.
Seven Stars you should know,
Our company needs to do one of these game activities.

If this lottery number is 5667 321
Here we only intercept the front four digits, that is, 5667来 as whether to win, the back of the three bits omitted.
And the winning rule is four awards.
The first prize is four numbers, meaning that four numbers appear simultaneously
The second prize is three numbers in the same now,
The third prize is a two-digit co-occurrence,
The four-prize is a number of the same now.
Well, before the draw, there will be many people to bet on.

If a has a single cast of 5, then the lottery number has a 5, then the jackpot. But he is a four-prize.
If B throws 567, the lottery number appears at the same time 567, in a 2 prize.
If C throws 66, then there are two 6 of the lottery numbers appearing at the same time. (You might ask why, because if you put a duplicate code and you win, the reward doubles.) )
If D cast 5676, then the lottery numbers are all matched, in the first prize.

It's probably one of those rules. This means, however, that you have voted 1 numbers or 4 numbers, the lottery numbers must all match to win. Include repeat number

$a = ' 5415 '; $a =str_split ($a); $a =array_count_values ($a); $b = ' 514 '; $b =str_split ($b); $b =array_count_values ($b); $ Result=true;foreach ($b as $k = + $v) {    if (!isset ($a [$k]) OR $a [$k]< $v) {        $result =false;        break;    }} Var_dump ($result);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.