6 Replies - 651 Views - Last Post: 04 October 2012 - 03:06 PM Rate Topic: -----

#1 henryvuong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-April 09

How do I compare strings to see if they are composed of the same words

Posted 04 October 2012 - 12:13 PM

I need a method that can compare two strings and tells me if they are composed of the same words, regardless of the word order. For example, the method should return "true" (i.e. the strings are the same) when comparing these two strings:

Mary has a little lamp
Little lamp has a Mary

and return "false" (i.e. the strings are different) when comparing these two strings:

Mary has a little lamp
Mary has a big lamp

Is This A Good Question/Topic? 0
  • +

Replies To: How do I compare strings to see if they are composed of the same words

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: How do I compare strings to see if they are composed of the same words

Posted 04 October 2012 - 12:22 PM

What have you thought of and/or tried so far to make this?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: How do I compare strings to see if they are composed of the same words

Posted 04 October 2012 - 01:21 PM

Take the two sentences and use string.split to break them into string arrays

sort each array

compare the elements now that they are in order
[0] to [0]
[1] to [1]
[2] to [2]
If they are of different sizes - fail.
If you get any element that doesn't match - fail.
If you can get to the end of each without a mismatch - success.

Now you have an algorithm to code up. Looking forward to seeing your code.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How do I compare strings to see if they are composed of the same words

Posted 04 October 2012 - 01:54 PM

Since you said you wanted to match regardless of the order, I would like to add a little to what tlhIn`toq supplied. You will need to loop through the first array by its total number of elements and with each iteration of that loop, loop through each element of the second array to compare against the current element in the primary loop for comparison. The nice thing is, in any iteration of the primary loop, if there is not a match you can stop because the strings don't match.

EDIT:
One of these days tlhin`toq, I'm going to spell your handle correctly the first time.

And disregard my post as I missed where he sorted the array :(

This post has been edited by CharlieMay: 04 October 2012 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: How do I compare strings to see if they are composed of the same words

Posted 04 October 2012 - 01:58 PM

No worries about the spelling, Charlie.

THough you don't need to do that nested loop business as that is going to increase the search time exponentially. That's the reason for sorting each array first - to take the original order out of consideration.

"Mary had a little lamp" and "A little lamp had Mary" would both sort to
  • a
  • had
  • lamp
  • little
  • Mary


Thus as long as [0] of each array are both "a", [1] of both arrays should be "had"... and so on.

This post has been edited by tlhIn`toq: 04 October 2012 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How do I compare strings to see if they are composed of the same words

Posted 04 October 2012 - 03:01 PM

Yea, I came to that realization rather quickly after I realized that sorting the words in the array would have made it easier and quicker to solve.

As far as misspelling, it's not OK when you're using [ member= ] because if it's not spelled right for the tag nothing appears. I just always seem to miss a letter.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: How do I compare strings to see if they are composed of the same words

Posted 04 October 2012 - 03:06 PM

Despite the lyrics of Weird Al Yankovich's "White and Nerdy" (I'm fluent in javascript as well as Klingon), not many developers actually speak Klingon. So the spelling TLHIN`TOQ does seem odd.

I had to change the normal apostophe (') to the accent (`) in the middle because it would also throw of the site parsing and turn tlhin'toq into tlhin because member='tlhin'toq' would naturally end at the second '
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1