You are viewing our Forum Archives. To view or take place in current topics click here.
Algorithm Help - Splitting one string into multiple words
Posted:

Algorithm Help - Splitting one string into multiple wordsPosted:

Cyimking
  • 2 Million
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Hi,

I am trying to split a string into multiple strings. The catch?

"thisisastring" must be split to "this is a string" .

One solution I have is to use a mini dictionary of words and have a brute force method by using substrings and checking if that word is in the dictionary. Aka use a for loop.

I saw this problem on google for an interview and no this isn't for that. I am just looking for a more efficient way of doing this. For me, the dictionary is going to be filled with 25 ish words so this isn't a big deal but I am eager to find out how to implement this efficiently.


So more examples:
'keyme' = 'key me'
'testingthisout' = 'testing this out'
'lovewithme' = 'love with me'
'lovewithoutme' = 'love without me'

Ignore one letter words (like A, I, etc..)
#2. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
The trouble I see with this, which you don't seem to have pointed out..

Surely with something like "love without me" would come up as "love with out me" instead of the words that you wanted it to display?
#3. Posted:
ip
  • Blind Luck
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Okay, so lets say you have a list of words. You randomly select them and it outputs "thisisrandom". What you could do, since you have the words defined, just get the length of the string and add a space. You can go through the string and check if the word does exist, if so, add a space at that index of the string. I'll try to make something like this in a bit.
#4. Posted:
ProJimmyRustler
  • V5 Launch
Status: Offline
Joined: Jul 14, 20149Year Member
Posts: 1,720
Reputation Power: 71
Status: Offline
Joined: Jul 14, 20149Year Member
Posts: 1,720
Reputation Power: 71
So basically you are trying to convert CamelCase, or in the situation, Camelcase into readable text.

public static string DisplayCamelCaseString(string camelCase)
{
    List<char> chars = new List<char>();
    chars.Add(camelCase[0]);
    foreach(char c in camelCase.Skip(1))
    {
        if (char.IsUpper(c))
        {
            chars.Add(' ');
            chars.Add(char.ToLower(c));
        }
        else
            chars.Add(c);
    }

    return new string(chars.ToArray());
}
#5. Posted:
r00t
  • Administrator
Status: Offline
Joined: May 18, 201112Year Member
Posts: 16,359
Reputation Power: 24344
Status: Offline
Joined: May 18, 201112Year Member
Posts: 16,359
Reputation Power: 24344
Used Python. Since the computer can't know whether you want to say "without" or "with out", I have it place a slash where there are conflicts. It keeps the previously-detected word on hand to see if it shows up as part of another word and both parts of the word are not in the dictionary. This would give you "love with/out me". If "out" was added to the dictionary, it would give you "love with out me"

stringToSplit = "lovewithoutme"
dictionary = "love","with","me","without"
splitString = ""
detectedWord = ""
lastWordEnd = 0

for i in range(len(stringToSplit)+1):
   checkingStr = stringToSplit[lastWordEnd:i]
   if checkingStr in dictionary:
      detectedWord = stringToSplit[lastWordEnd:i]
      splitString += detectedWord + " "
      lastWordEnd = i
   if (detectedWord + checkingStr) in dictionary:
      splitString = splitString[:-1] + "/" + (checkingStr) + " "
      lastWordEnd = i
print(splitString)
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.