How can I determine whether two strings have the same characters?

Source: Internet
Author: User
How can I determine whether two strings have the same characters? $ A = 2344; // master string, fixed value.

// The following variables can be changed.
If $ B = 12; // determines whether the $ a string contains 1 and 2, the result is false.
If $ B = 22; // determines whether the $ a string contains two 2 s, the result is only one 2, false
If $ B = 234; // determines whether the $ a string contains 2, 3, and 4. the result $ a contains all. True
If $ B = 324; // determines whether the $ a string contains 3, 2, and 4. the result $ a contains all. True
If $ B = 4432; // determines whether the $ a string contains 4, 4, 3, and 2. if the result is determined, $ a contains all, true
If $ B = 4444; // determines whether the $ a string contains 4 4 4. after the judgment, the result $ a has only 2 4, false
If $ B
If $ B
If $ B
... There will be more

The judgment result I need is as above.
How to write judgment statements?


Reply to discussion (solution)

$ A = '20180101'; $ d = array ('12', '22', '20180101', '20180101', '20180101 '); $ a = count_chars ($ a, 1); foreach ($ d as $ B) {$ t = count_chars ($ B, 1); $ f = 0; if (array_keys (array_intersect_key ($ t, $ a) = array_keys ($ a) $ f = 1; printf ("B = % s \ n ", $ B, $ f? 'True': 'false ');}
B = 12 false B = 22 false B = 234 true B = 324 true B = 4432 true B = 4444 false B = false

If $ B = 4444; // determines whether the $ a string contains 4 4 4. after the judgment, the result $ a has only 2 4, false
This rule seems inappropriate.

Split $ a and $ B into arrays by characters, and then judge whether the subset is used?

I don't know what your purpose is?
Starting with the business logic, there may be better solutions.

$ A = '20180101'; $ d = array ('12', '22', '20180101', '20180101', '20180101 '); $ a = count_chars ($ a, 1); foreach ($ d as $ B) {$ t = count_chars ($ B, 1); $ f = 0; if (array_keys (array_intersect_key ($ t, $ a) = array_keys ($ a) $ f = 1; printf ("B = % s \ n ", $ B, $ f? 'True': 'false ');}
B = 12 false B = 22 false B = 234 true B = 324 true B = 4432 true B = 4444 false B = false

If $ B = 4444; // determines whether the $ a string contains 4 4 4. after the judgment, the result $ a has only 2 4, false
This rule seems inappropriate.



This judgment is still flawed,
What if $ a is 4444.
The result I want is that only $ B = 4444, $ B = 44, $ B = 444, or $ B = 4 can match successfully to be true, none of the above,

As I have already said, your rules are faulty (or incomplete)
The code I gave can only handle one situation: all characters in $ a (deduplication) must appear in $ B.

After you have developed a complete set of rules, you can expand the function.

First, convert the string into an array, and then use array_count_values to calculate the number of characters in each character. if the number of occurrences in $ B is less than or equal to the number of words in $ a, it is true. Otherwise, it is false.

As I have already said, your rules are faulty (or incomplete)
The code I gave can only handle one situation: all characters in $ a (deduplication) must appear in $ B.

After you have developed a complete set of rules, you can expand the function.



Let's talk about my business needs. maybe I can understand a little,
This way.
You should know about the dipper,
Our company needs to make such a game activity.

If the lottery number for this issue is 5667 321
Here we only extract the first four digits, that is, 5667, to determine whether to win the prize. the last three digits are omitted.
The winning rule is the fourth prize.
The first prize is the same as four numbers, meaning that four numbers appear at the same time.
The second prize is the same as the current three,
The third prize is the same as the second prize,
The fourth prize is the same number.
Therefore, many people will place bets before the award.

If a single vote for five, one of the lottery numbers is five, and the prize is received. But among them are the 4th prize.
If B invests 567 yuan, the lottery number also shows 567, winning 2 and so on.
If C has voted 66, two of the lottery numbers are both listed at the same time. (I may ask why, because if I repeat the code and win the prize, the reward will double .)
If D voted for 5676, all the lottery numbers match and won the first prize.

This is probably a rule. It means that, but if you cast one or four numbers, you must match all of them to win the prize. Including number of duplicates

$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.