• Post Reply Bookmark Topic Watch Topic
  • New Topic

count of string in list  RSS feed

 
Carl John
Ranch Hand
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm having a list of string like

List<String> ls = new ArrayList<String> ();
ls.add("CAT");
ls.add("MAT");
ls.add("TAM");
ls.add("CTA");
ls.add("AMT");


Now i want the count of the string in the list like

CAT --> 2
MAT --> 3
TAM --> 3
CTA --> 2
AMT --> 3


(the string characters can be interchanged...(CAT/CTA can be both considered as same) .

How to get a count like this.?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl John wrote:Now i want the count of the string in the list like
...
(the string characters can be interchanged...(CAT/CTA can be both considered as same) .
How to get a count like this.?

Well, the first thing to do is write down the steps involved in what you want to do before you write one line of Java code.

For example, if CAT and CTA are considered "equal", then you obviously won't be able to use String.equals(); so, how else do you think you might do that comparison?

It's a really important lesson to learn: You will NEVER (or almost never) code your way to a solution; you have to think about it.

For more information, read the StopCoding (←click→) and WhatNotHow pages.

Winston
 
Carl John
Ranch Hand
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Winston,

I was asked this question couple of weeks back in an interview...

For example, if CAT and CTA are considered "equal", then you obviously won't be able to use String.equals(); so, how else do you think you might do that comparison?


The way to compare is to convert the strings to ASCII values so that CAT/CTA will have same values.



But in some cases this scenario fails too..

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in some cases this scenario fails too..

Not really. It will fail in lot of scenarios if you are using int ascii = w.charAt(0) + w.charAt(1) + w.charAt(2);

Anyway. Did you start writing logical steps on paper like Winston suggested?
 
Carl John
Ranch Hand
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Maneesh,


Anyway. Did you start writing logical steps on paper like Winston suggested?


That's the idea i've been following till date.

Coming back to this scenario, Find the count of the string in the list ? .Please Suggest me Is there any logical steps to do that or it is impossible to achieve that..?

Only thing i can figure out is to convert them to ascii values and do further comparison.

As i said before , i was asked in the interview panel.Certainly i don't know what they really expect from this question.!

Thanks.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: If as per the rules CAT is "equal" to CTA, will it help of you change all CTA values to CAT in the list? If yes, can you now build the logical steps on paper?
 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the ascii approach, what would be the ascii for 'ACA' and 'ABB'?
 
Carl John
Ranch Hand
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh,

Hint: If as per the rules CAT is "equal" to CTA, will it help of you change all CTA values to CAT in the list?...


You're asking to change the all CTA values to CAT..

Suppose if you have a list over hundreds and thousands of string values , to which extent you will change the values ?

Certainly as i said before, i don't have any logical steps left over, for this scenario..!

I just want to know whether there is a possibility to achieve this ?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl John wrote:You're asking to change the all CTA values to CAT..

Well, you could do that, or simply change all strings with exactly the same set of letters to look the same (Chan hinted as to how you could do this, but I notice he's removed it now - so I'll add back his hint: Look at the String.toCharArray() and Arrays.sort() methods and think about how they might help to "normalize" your Strings).

Suppose if you have a list over hundreds and thousands of string values , to which extent you will change the values ?

Don't worry about efficiency right now; worry about getting the process right.

Certainly as i said before, i don't have any logical steps left over, for this scenario..!
I just want to know whether there is a possibility to achieve this ?

We're not torturers; we would have told you if it wasn't possible.

Your problem right now is that you're too concentrated on coding, when what you need to do is step back and think about what you need to do to solve the problem. And for that you need a pencil, an eraser and LOTS of paper.

Oh, and a brain. Programming is thought, NOT code.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl John wrote:I just want to know whether there is a possibility to achieve this ?

Yes. I can think of several ways of doing it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!