Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a Subset of Strings from 10000 ascii strings

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

  1. You have a set of 10000 ascii strings (loaded from a file)
  2. A string is input from stdin.
  3. 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.
  4. 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.

like image 269
AKIWEB Avatar asked Nov 16 '12 22:11

AKIWEB


People also ask

Should return 1 if the secondString comes after after firstString in the searchString?

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

How do you check if a string can be formed from another string?

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.

How do I check if a string contains a substring in R?

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.

Can you index a string in Java?

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.


1 Answers

Here is an O(1) algorithm!

Initialization:

  • For each string, sort characters, removing duplicates - eg "trees" becomes "erst"
  • load sorted word into a trie tree using the sorted characters, adding a reference to the original word to the list of words stored at the each node traversed

Search:

  • sort input string same as initialization for source strings
  • follow source string trie using the characters, at the end node, return all words referenced there
like image 146
Bohemian Avatar answered Sep 28 '22 07:09

Bohemian