Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Counting no of common characters in String  RSS feed

 
Ranch Hand
Posts: 36
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I write a code to count no of common characters(if single character appear more than once in all string it is counted as no of times it appears) in n strings but it does not give expected output



Input : 1
          3
          abcaa
          bcbd
          bgc

Output:3
Expected Output : 2
 
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain exactly how you are counting letters. I can see c in all three Strings, so I would expect the answer 3.
 
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think List1.retainAll(List2.retainAll(List3)) will do the job.

problem is on line 19, 27. you are not clearing the List before proceeding to the next iteration. and on line 27 you need to concatenate the characterss currently in List1 but you are simply assigning the character each time making s1 of length 1 containing the single character. Though your code needs refactoring and also their is much redundant code. why don't you use char[] instead of ArrayList...
 
praveen kumaar
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain exactly how you are counting letters. I can see c in all three Strings, so I would expect the answer 3.


I think he just needs the number of intersection characters of the n strings..
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see all sorts of problems with your code style. What does while(n-->1) mean? There's nothing like code easy to understand, and I am afraid that is nothing like code easy to understand. Also make sure that your variable names accurately reflect their meaning.

Also, go through your code and work out what each line does. I can see several lines which don't appear to do anything at all.
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praveen kumaar wrote:. . . I think he just needs the number of intersection characters of the n strings..

What's an intersection character?
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praveen kumaar wrote:I think List1.retainAll(List2.retainAll(List3)) will do the job. . . .

That depends on what the job is; I don't think it has been explained yet.
 
praveen kumaar
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What's an intersection character?

I mean characters common among all the String and if their is some character common among the input strings increment the count by 1 additionally if some counted character repeats in any string then it will be counted again if the same character repeats also in all other strings. That's a guess though. let the OP decide if it's what he wants.[Edited]
 
praveen kumaar
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List1.retainAll(List2.retainAll(List3)) -> I am sorry but it will fortunately fail as retainAll returns a boolean. it should have been
In plain english it means first retain only the elements in list1 which are present in list2 and then further again  retain olnly those elements in list1 that are present in list3. where list1, list2, list3 should be a List of Characters.
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praveen kumaar wrote:. . . That's a guess though. . . .

Yes, but I guessed that OP wanted something different; we can only help when we know.
 
Shubham Rai
Ranch Hand
Posts: 36
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praveen kumaar wrote:

Campbell Ritchie wrote:Please explain exactly how you are counting letters. I can see c in all three Strings, so I would expect the answer 3.


I think he just needs the number of intersection characters of the n strings..



Yes,i need the intersection of character but in my code retain all method does not work properly
 
Shubham Rai
Ranch Hand
Posts: 36
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run my above code
using input  1  //No of test case
            3    // No of String in first test case
            abcaa  // first string
            bcbd   // Second string
            bgc    // Third string
Actual Output:3
Expected output:2
When i try to print element instead of set1 size in above code it prints(b c c) which directly indicates that these characters are not the intersection of 3 string of test case 1
 
Campbell Ritchie
Marshal
Posts: 62801
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what does intersection of character mean?
 
Shubham Rai
Ranch Hand
Posts: 36
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And what does intersection of character mean?



Intersection means common characters in all the given strings.
 
praveen kumaar
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:Intersection means common characters in all the given strings.

Shubham, it would be a lot better if you can post the original language in the problem. please don't forget to mention the source(for copyright issues). if it's from some book tell us the book name, page number etc. if you have a link to the original problem you have an option to cite the link also. We really want to help but we need to understand the problem first. As you can see in the previous discussion of this thread, i have guessed something and campbell has something else which is creating ambiguity.
And I think you should have a look at HowToAskQuestions.
 
Master Rancher
Posts: 3080
108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Shubham
you are performing the loop starting at line 14 twice. After the first loop. set1 contains the 2 expected chars, that's oke. But then, in the second loop, have a look at line 19. Can you spot the error? What is the meaning of the loop at line 25?


PS: I had no trouble understanding the question.
 
Shubham Rai
Ranch Hand
Posts: 36
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:@Shubham
you are performing the loop starting at line 14 twice. After the first loop. set1 contains the 2 expected chars, that's oke. But then, in the second loop, have a look at line 19. Can you spot the error? What is the meaning of the loop at line 25?


PS: I had no trouble understanding the question.



The loop at line no 14 i started is for n number of strings and after intersection of set1 and set2 ,I have to make a string of intersection parts so that i compare this new string to other input strings to find out the intersection of n strings.So loop at 25 is made to make a string of intersection of  characters of set1 and set2
 
Piet Souris
Master Rancher
Posts: 3080
108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. But have a look at line 27. Do you think that you create s1 in a correct way? Add a println right behind that line and print out what s1 is. Or use a single stepper if you are using an IDE.

Then the second pass of the loop starts. Have a look at lines 18-20. What happens to 'set1'? Add again a println there as well, to see what happens to set1. Do you think the creation of set1 should be in that inner loop?
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!