Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no chars that can be the same so this word should be leaved.

    static StringBuilder BeBalsiu(string fd, string eilute, string balses, char[] skyrikliai)
        {
            using (StreamReader sr = new StreamReader(fd, Encoding.GetEncoding(1257)))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = eilute.Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string word in parts)
                        naujas.Add(word);
                }
            }

                return nauja;
        }

Recommended Answers

All 12 Replies

I would stream characters one by one into an array, and if letter appears, add 1 next to it, so that array ends up (for potato):

"p" => 1,
"o" => 2,
"t" => 1,
"a" => 1,
"t" => 1

Then look up if there are any values > 1. If true, remove the word. If false, uh, process it.

Alternatively, this link could be interesting:
http://stackoverflow.com/a/1212118

This one probably helps you regex out repeated words, I'm pretty sure if you nibble long enough, you'll find regex for repeating letters. Match, if true, remove the word, if false, once again, JUST DO IT!!

How about forgetting the code for a second. Let's talk about how to achieve the requirement first. How do you deal with checking for the same character in a word? Well, there are steps to solve the problem.

  1. You need to be able to iterate through each character in a given word. How to do that? You could take a look this post which gives you an example of how to do it.
  2. You need to be able to keep track of what characters you have seen so far, so that you would know if there is any repeat characters in your word. Again, there is an example out there.

I am not going to give you the code, but I want you to learn from reading solution from other similar problems. ;)

@Aeonix , I disagree using (or even mentioning) regex to solve this very simple problem. Also, it is not easy for a beginner to understand what regex is. As a result, it could turn out to be a copy-and-paste solution which in turn teaches the OP nothing.

Another way to approach this is with a LINQ extension(Distinct). This will return a collection of elements that are unique. If the count is the same as the original collection, then the original collection is all unique elements. Since a string is basically a collection of characters, this method can be applied:

foreach (string word in parts)
{
    if(word.Length == word.Distinct().Count())
    {
        naujas.Add(word);            
    }
}

I'm wondering how this forum is turning into... The OP is obviously doing an assignment. Instead of teaching him, a solution is given for copy-and-paste...

While I agree on teaching vs. handing out fish, the OP is not engaged here. I think at this point this is their only post with no other feedback.

On some boards threads where the OP doesn't participate are closed very quickly.

Also for many people learning is done through examining existing code and reasoning out how it works. This is just as valid a teaching tool as any other.

commented: Good point! I learned much that way. +15

Hold the pan by the handle and make use of
Regex.Matches(inputString, "\W*\w*(?<c>\w)\w*\k<c>\w*")
for pattern.

A working solution would be to add every letter of each word to an array and just check if they are the same. Bam, easy, done. It works, but I know this would be very inefficient code if this was being done on a larger scale. How could this problem be solved most efficiently with C++?

Why not an algorithm in pseudo-code:

currWordStream <-- null (clear)
currWord <-- null (clear)
do

 currChar <--- getNextCharFromInputStream
while (currChar != abcdefg...) && (!EOInputStream)

do while !EOChars

Found = searchCurrCharInCurrWord
    if (!Found)

    if (!EOWord)
            currChar <--- getNextCharFromInputStream
        else

    OutputStream <-- append CurrWord
    CurrWord <-- null (clear)
    do
         currChar <--- getNextCharFromInputStream
     while (currChar != abcdefg....) && (!EOInputStream)

else

  // discard currWord
       currWord <-- null
   do
     currChar <--- getNextCharFromInputStream
   loop while currChar alphabetic && !EOChars

Of course, a line was missing:
...
if (!EOWord)
currWord <--- append currChar
currChar <--- getNextCharFromInputStream
else
...

For c++ I would use a map:

bool isDistinct( string word)
{
    map<char , int> letters;
    for (char c : word )
    {
        letters[c] += 1;
    }
    for ( auto pr : letters )
    {
        if ( pr.second != 1 )
        {
            return false;
        }
    }
    return true;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.