id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

amjadm@miamioh.edu project5 > swapElems
prev  |  next  |  chance

We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over and return the given array of non-empty strings as follows: 1- If a string matches an earlier string in the array, swap the 2 strings. 2- A particular first char can only cause 1 swap, so once a char has caused a swap, its later swaps are disabled. This can be solved in one pass achieving Θ(n) amortized time only by using a HashMap and HashSet. So you MUST use HashMap and HashSet in your solution.


swapElems(["ab", "ac"]) → ["ac", "ab"]
swapElems(["ax", "bx", "cx", "ay", "by", "cy"]) → ["ay", "by", "cy", "ax", "bx", "cx"]
swapElems(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by", "ax", "bx", "ai", "aj", "bx", "by"]

...Save, Compile, Run (ctrl-enter)

public String[] swapElems(String[] strings) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 160 Post-solution available

Copyright Nick Parlante 2017 - privacy