Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Easy?  RSS feed

 
Saloon Keeper
Posts: 3250
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi All,

I was doing this exercise at HackerRank: HackerRank exercise.
The exercise is, as you see, categorized as "Easy". Do you agree? (just click on the page and the log-in pop-up will disappear)

(Please no solutions here, put them at their site, if you like to)
 
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looked a bit difficult until I saw the "Explanation" section.

My thought process:
1. Extract all unique chars
2. Make all permutations of 2 chars
3. Check string with all your permutations

I still have no idea why string length is provided as input.
 
Piet Souris
Saloon Keeper
Posts: 3250
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they gave away too much in their example. But nevertheless, implementing your correct thought process,  determining if a string has alternating characters, et cetera, Is not exactly easy.
 
salvin francis
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:... determining if a string has alternating characters...


This was my thought process for that test:
Maybe I am missing something ?
 
Piet Souris
Saloon Keeper
Posts: 3250
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"aaaab" would pass your test. And what if you've never heard of chars.distinct? Or of a regex?

By the way: as with most of these sites, you are always given the length of a string, or array, or list, java or no java
 
salvin francis
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew it, I was missing something I thought that having 2 characters was enough. But, let us leave the discussion here instead of discussing more solutions since it would be unfair to the original site.
Yes, it is not easy.
 
salvin francis
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:"aaaab" would pass your test...


How about this ?
 
Saloon Keeper
Posts: 2566
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking of something like this, but it does involve scanning the string multiple tmes:I like your approach  - more concise (although it does require regex knowledge, which isn't a bad thing).
 
salvin francis
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking of a complete regex solution, but couldn't come up with one.
Explanation: This regex fails for "aaaaaaaa" and hence I couldn't get rid of the distinct count.
 
salvin francis
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:I was thinking of something like this ...


How about this solution without regex:
 
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy to get lost coming up with efficient solutions. I started with a solution that eliminated characters that are followed by duplicates, but then you're left with a string that might contain more than two distinct characters, and eliminating one (preferably the one with the least occurrences) might lead to other duplicate characters getting jammed together.

Before continuing, I decided to go with a brute force approach to stay in the spirit of coming up with an "Easy" solution: https://www.hackerrank.com/challenges/two-characters/submissions/code/110731942

It's more verbose and less efficient than I would have liked, but it's straightforward and I hope not too difficult to understand.

Because the problem had a couple of gotchas, I would rate this as a medium problem, not an easy one. Probably somewhere in between.
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a 404 error when I follow that URL.  FYI.
 
salvin francis
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link does not work even for a logged in user.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!