### Thread: Singletons in a string

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
28
Rep Power
0

#### Singletons in a string

I have to write a code that counts the number of singletons, stored in the variable n, in a string sequence. A singleton is a word in a sequence that does not appear before or after itself in the sequence.

I initialized the count variable n with 2, since the first and last tokens can only have one
copy before and after each other respectively. I divided the string sequence into tokens by splitting it with respect to the empty spaces between them.

Here is my code.

Code:
```String string = " ";
string = stdin.next();

String[] tokens = string.split(" ");

n = 2;

for (int i = 1; i < tokens.length()-1; i++){
while (!(tokens[i].equals("xxxxx"))){
if (!(tokens[i].equals(tokens[i-1]) && !(tokens[i].equals(tokens[i+1])))
n++;
}
}```
2. Do you have a question?
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
28
Rep Power
0
Originally Posted by NormR
Do you have a question?
I know. I posted the same question on two different forums. I hope that this is not an issue.
4. Originally Posted by ForTomorrow
I know.
Maybe it will be an idea to let us know your question too. (that is what NormR were asking for)
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
28
Rep Power
0
Originally Posted by NormR
Do you have a question?
Yes. My code is not working, and I want to know if you can help. As I stated, I am supposed to write a program that detects singletons, and count the number of times that they appear.
6. Do you have an algorithm/design for doing that? You should design code before writing it. For this kind of code, you need to work out the steps to solve the problem before writing any code.

I don't understand what a "singleton" is. How you have described it sounds like a word is a singleton if it only appears one time in a sequence. What is a sequence?

detects singletons, and count the number of times that they appear.
When would they appear more than once? Can you give an example?
7. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Feb 2001
Posts
1,481
Rep Power
16
I have to write a code that counts the number of singletons, stored in the variable n, in a string sequence. A singleton is a word in a sequence that does not appear before or after itself in the sequence.
How is that different than saying, "I want all the unique words in a string? If the word occurs more than once, I don't want it."

Dictionaries/hashes/maps are perfect for counting the occurrences of things:
PHP Code:
``` import java.util.*; public class MyProg {     public static void main(String[] args) {         String str = "hello world goodbye world";         String[] words = str.split("\\s+");         Map<String, Integer> counts = new HashMap<String, Integer>();         for (String word : words) {             Integer word_count = counts.get(word);             counts.put(                 word,                   (word_count == null) ? 1 : word_count+1             );         }         for (Map.Entry<String, Integer> e : counts.entrySet()) {             System.out.println(e.getKey() + "=>" + e.getValue());         }     } } --output:-- hello=>1 goodbye=>1 world=>2  ```
A Map is like an Array, but instead of having integer index values, a Map uses Strings (or any other object) for the index value. In a Map, the index value is known as "a key", and it has "a value" associated with it. Similarly, an element in an array has an index value and a value associated with the integer index.

My code is not working,
Here's the way it works on a computer programming forum: you must do one of the following:

1) Copy and paste the error message you got.
2) If there is no error message, copy and paste the actual output and then state your desired output.
Last edited by 7stud; April 15th, 2014 at 02:42 PM.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
28
Rep Power
0
Originally Posted by NormR
Do you have an algorithm/design for doing that? You should design code before writing it. For this kind of code, you need to work out the steps to solve the problem before writing any code.

I don't understand what a "singleton" is. How you have described it sounds like a word is a singleton if it only appears one time in a sequence. What is a sequence?

When would they appear more than once? Can you give an example?
Well, the problem specification gives the following example.
Here is a sequence in a string: "fish bird reptile reptile bird bird bird mammal fish".

The singletons in the above string would be: the first appearance of "fish", the first appearance of "bird", "mammal", and the second appearance of "fish. And then, I have to use a variable n and store the number of singletons in it. In this case, it would be 4.
9. Do you have an algorithm/design for doing that? You should design code before writing it. For this kind of code, you need to work out the steps to solve the problem before writing any code.
10. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Feb 2001
Posts
1,481
Rep Power
16
The singletons in the above string would be: the first appearance of "fish"
According to you:

A singleton is a word in a sequence that does not appear before or after itself in the sequence.
Because 'fish' appears after the first appearance of 'fish', right there:

"fish bird reptile reptile bird bird bird mammal fish" <---
the first appearance of 'fish' cannot be a singleton.

From your example, I gather that for a word to be a singleton, it cannot be *immediately* preceded by or followed by itself. In that case, step through the words and check if the preceding or following word is the same. You'll have to do some checking for when the index value is 0 and (length-1) because the index value will be out of range on on side of those positions; or you could just add some word that isn't going to occur in your string to either end and start examining the words at words[1] and stop at words[words.length-2].
Last edited by 7stud; April 15th, 2014 at 03:18 PM.