Here is my requirement:
- Input: Random String of sufficiently long ex:
fdjhkajajkfdj - Output:
fdjhas a 2 occurences and separated byxchars
I want to put all three letter words in an array and check if they are the same
Eg:
a[0] = fdj
a[1] = djh
a[2] = jhk
a[3] = hka
a[4] = kaj
.
.
.
a[n] =fdj
My answer is a[0] and a[n] matches, may be more than 2 occurances.
Question: So what kind of array should I use which is optimal in this situation. I am using Java (and also python). I was thinking of Dict.
In Java you could use the Map interface ( http://download.oracle.com/javase/1.4.2/docs/api/java/util/Map.html )
I would use HashMap so that the key is the 3 letter word and the value is the count of occurances. Here’s some sample pseudo code
Then you can loop through the key/value pairs and return their occurance count.
To return the number of characters between the words, you can do this separately after counting the occurances by using String manipulation (see String.indexOf). Pseudo code for this is….