My college is getting over so I have started preparing for the interviews to get the JOB and I came across this interview question while I was preparing for the interview
- You have a set of 10000 ascii strings (loaded from a file)
- A string is input from stdin.
- Write a pseudocode that returns (to stdout) a subset of strings in (1) that contain the same distinct characters (regardless of order) as input in (2). Optimize for time.
- Assume that this function will need to be invoked repeatedly. Initializing the string array once and storing in memory is okay . Please avoid solutions that require looping through all 10000 strings.
Can anyone provide me a general pseudocode/algorithm kind of thing how to solve this problem? I am scratching my head thinking about the solution. I am mostly familiar with Java.
+getInput() : String - Should scan for three input strings naming searchString,firstString and secondString respectively. +findString(String,String,String) : int - Should return 1 if the secondString comes after after firstString in the searchString. - Should return 0 if the strings are not found as expected.
Approach: The above problem can be solved using hashing. The count of all the characters in S1 is stored in a hash-table. Traverse in the string, and check if the character in S2 is there in the hash-table, reduce the count of that particular character in the hash-table.
In R, we use the grepl() function to check if characters are present in a string or not. And the method returns a Boolean value, TRUE - if the specified sequence of characters are present in the string.
You can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1 . For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara.
Here is an O(1) algorithm!
Initialization:
Search:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With