The output of this code continues to be 4. However, the output should be 3. The set intersection is present because I believe that is the key towards the answer. The reasoning for the answer being 4 instead of 3 comes from the number of 2 qs and 1 r that match s2 in s1. Show
Any help is much appreciated. asked May 30, 2017 at 18:28
Drew BennettDrew Bennett 4833 gold badges6 silver badges25 bronze badges 7 If you want to maintain a count of the number of characters in common, you should use collections.Counter instead of
answered May 30, 2017 at 18:39
lazy doglazy dog 2,29612 silver badges21 bronze badges 1 Here is a solution that doesn't involve sets:
answered May 30, 2017 at 18:41
Scott HunterScott Hunter 47.5k11 gold badges56 silver badges95 bronze badges 1
This results in;
Analysis, 2 is the correct answer (q and r are the only letters common to both), 3 is the lower number of either sets unique values. answered May 30, 2017 at 18:48
1
answered Dec 24, 2020 at 11:13
1 I replaced your original chunk of code
to:
It outputs, it is the letter with occurrence counts:
It is correct, string
therefore the minimum count is 4. answered May 30, 2017 at 18:36
Haifeng ZhangHaifeng Zhang 28.3k19 gold badges72 silver badges119 bronze badges Do a count for each letter, and take the minimum to find out how many of that letter are common to both strings. Then sum that up, and that's your answer.
answered May 30, 2017 at 18:37
Sepehr NazariSepehr Nazari 3,6754 gold badges11 silver badges17 bronze badges 0 Here is another way to do it using no modules.
answered May 30, 2017 at 18:43
gold_cygold_cy 12.7k3 gold badges21 silver badges42 bronze badges
answered Dec 28, 2018 at 21:41
1 Another way, though very late...
answered Apr 27, 2021 at 14:27
0Knowledge0Knowledge 7193 silver badges11 bronze badges How do you count the common characters in two strings?Approach: Count the frequencies of all the characters from both strings. Now, for every character if the frequency of this character in string s1 is freq1 and in string s2 is freq2 then total valid pairs with this character will be min(freq1, freq2). The sum of this value for all the characters is the required answer.
How do you count occurrences of a word in a string in Python?The count() method returns the number of occurrences of a substring in the given string.. substring - string whose count is to be found.. start (Optional) - starting index within the string where search starts.. end (Optional) - ending index within the string where search ends.. How do you count occurrences of a letter in Python?Use the count() Function to Count the Number of a Characters Occuring in a String in Python. We can count the occurrence of a value in strings using the count() function. It will return how many times the value appears in the given string.
How do you count similar characters in a string?Python. string = "Great responsibility";. print("Duplicate characters in a given string: ");. #Counts each character present in the string.. for i in range(0, len(string)):. count = 1;. for j in range(i+1, len(string)):. if(string[i] == string[j] and string[i] != ' '):. count = count + 1;. |