• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Easy?

 
Saloon Keeper
Posts: 3297
146
  • 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: 2294
95
Google Web Toolkit Eclipse IDE 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: 3297
146
  • 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: 2294
95
Google Web Toolkit Eclipse IDE 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: 3297
146
  • 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: 2294
95
Google Web Toolkit Eclipse IDE 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: 2294
95
Google Web Toolkit Eclipse IDE 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: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • 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: 2294
95
Google Web Toolkit Eclipse IDE 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: 2294
95
Google Web Toolkit Eclipse IDE 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: 10308
217
  • 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.
 
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 2294
95
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link does not work even for a logged in user.
 
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this a couple of weeks ago and decided to try it out before reading further - fiddling with it sporadically, I eventually got a not-very-concise solution (of unknown efficiency)  and signed up with HackerRank to submit it, link = https://www.hackerrank.com/challenges/two-characters/submissions/code/113330918 .  I note that I can access Stephan's link when signed in (though I haven't gone through it properly yet to compare with my code).

I thought it would be fun (it was), but it took me embarrassingly long. False start with an approach that first considered character frequencies, and might correlate with some comments I later read on the problem's Discussion page, but where I got bogged down for some reason. Then switched to brute-force checks on all character-pair-combination as per salvin's summary (post #2), but with an initial iterative removal of chars that already had 'runs' in the string; I though the initial process (which may be similar to what Stephan mentions in his first paragraph, but iterative) would be optional, and maybe improve efficiency on average, but removing  it exposed bugs in the ability of the rest of my code to handle all strings, and I gave up trying to untangle them. Finally I replaced the offending logic aspect with something more straightforward and confirmed that it worked with or without the optional initial section (which I didn’t include in my upload).

On a tangent, I didn't realise before seeing salvin's second post (#4 in thread) that there was a "chars()" method in String. I see now it's offered on a string by my IDE, but it doesn't seem to be listed in https://docs.oracle.com/javase/8/docs/api/java/lang/String.html (unless I'm missing something obvious) - that seems odd?
 
Ron McLeod
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Elaine Byrne wrote:... I didn't realise before seeing salvin's second post (#4 in thread) that there was a "chars()" method in String. I see now it's offered on a string by my IDE, but it doesn't seem to be listed in https://docs.oracle.com/javase/8/docs/api/java/lang/String.html (unless I'm missing something obvious) - that seems odd?


String implements the CharSequence interface.

Starting with Java 1.8, CharSequence included CharSequence#chars which returns an IntStream.  CharSequence has a default implementation of this method.
 
Elaine Byrne
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thanks Ron, I didn't think to look at the documentation for the implemented interfaces (I think inherited methods are sometimes listed in these docs, and I had assumed it woud be the case here too)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!