Question

In: Computer Science

String search   Describe the brute force solution to counting the number of times the letter “a”...

String search  

  1. Describe the brute force solution to counting the number of times the letter “a” occurs in a text.

  1. Outline a divide-and-conquer algorithm for counting the number of times the letter “a” occurs in a text.

  1. Analyze each approach and compare the efficiencies.

Solutions

Expert Solution

Don't forget to upvote if you like my answer
Please comment if you have any queries or suggestions.

Brute force solution:

For searching a particular alphabet using brute force, we will do it in the following method:

  • We will initialize a variable count_a to 0(zero).
  • Then we will start iterating each character from the start of the string till the end.
  • We will increase count_a by 1 when the character iterated is a.
  • The final value of count_a at the end of the loop will be the number of As.

Dry run:

Lets take a string "abracadabra". We start iterating from the first alphabet('a'). The first letter is 'a', so we increase count_a by 1. Now count_a =1. The next letter('b') is not 'a'. We keep the value of count_a unchanged. In this way we iterate through the whole string and the final value of count_a is 5.

Efficiency:

This algorithm will take O(n) time where n is the length of the string because it has to iterate through the whole string.

Divide-conquer

For searching the string using divide and conquer method, the string needs to be in an ascending order. If the string is not sorted, we need to sort it using some algorithm like merge sort.

Divide and conquer or binary search needs a condition to split the string. If the string is not sorted, there will no definite condition on the basis of which the string can be split.

Now, when the string is sorted, we know that all the similar letters will be grouped together. While searching for an alphabet in the sorted sequence, what we need to find is the difference between the start index and the end index of that group containing our concerned letter. This difference increased by unity will be our result.

Consider the example 'abracadabra'. When we sort it, it becomes 'aaaaabbcdrr'. The starting index for 'a' in the sorted sequence is 1 and the end index is 5. The difference between the end index and the start index is 4. The difference increased by unity is 5 which is our result.

Now it is evident that the start index of 'a' in any sorted sequence of alphabets is 1 because 'a' is the first alphabet. The problem boils down to finding the end index of the group of 'a's. We find that index using binary search.

Efficiency:

If the string is sorted, the binary search will take O(log n) time.
But if we have to sort the string, we also have to consider the complexity of sorting algorithms which is O(n*logn) for algorithms like merge sort and quick sort.


Related Solutions

Counting evens Write the pseudo-code for a brute force approach to counting the number of even...
Counting evens Write the pseudo-code for a brute force approach to counting the number of even integers in an array of n integers. Write the pseudo-code divide-and-conquer algorithm for counting the number of even integers in an array of n integers. Give the recurrence relation for the number of additions in your divide-and-conquer algorithm.  
Counting integers greater than 10 Write the pseudo-code for a brute force approach to counting the...
Counting integers greater than 10 Write the pseudo-code for a brute force approach to counting the number of integers greater than 10 in an array of n integers. Write the pseudo-code divide-and-conquer algorithm for counting the number of integers greater than 10 in an array of n integers. Give the recurrence relation for the number of comparisons in your divide-and-conquer algorithm in part b.  
Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And...
Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And why
3.       How many successful and unsuccessful comparisons will by made using brute force string matching in a...
3.       How many successful and unsuccessful comparisons will by made using brute force string matching in a binary string of 1000 zeros while trying to match the following patters: a.       1000 b.       0001 c.       00010
III. Answer part A,B, and C. (in JAVA) 3a) Return the number of times the letter...
III. Answer part A,B, and C. (in JAVA) 3a) Return the number of times the letter 'a' occurs in a String. countA("abc") → 1 countA("wxyz") → 0 countA("bcaca") → 2 3b) Return the number of times the given letter ch occurs in the given String str. countNumChar("abc", "a") → 1 countNumChar("wxyz", "b") → 0 countNumChar("bcaca", "c") → 2 3c) Count the number of characters that are either a or b in a String. Try to use only one for loop....
Discuss potential methods to linearize body source terms in search for a stable solution and Describe:...
Discuss potential methods to linearize body source terms in search for a stable solution and Describe: -Relations that could be used to couple pressure and density with heat addition -The role or meaning of characteristic curves in a flow field -Shock-capturing techniques in flow fields
Discuss potential methods to linearize body source terms in search for a stable solution and Describe:...
Discuss potential methods to linearize body source terms in search for a stable solution and Describe: -Relations that could be used to couple pressure and density with heat addition -The role or meaning of characteristic curves in a flow field -Shock-capturing techniques in flow fields
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
1. To obtain a solution through superposition of solutions, describe the number of non-homogeneous conditions per...
1. To obtain a solution through superposition of solutions, describe the number of non-homogeneous conditions per sub-problem. 2.    Discuss the relevance of shape factors. 3.    Discuss the requirements for using the superposition method of solution. 4.    Describe what is meant by eigen-problem solution in heat transfer. 5.    Discuss what values a separation constant can attain during the separation of variables method of solution.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT