Skip to content

Instantly share code, notes, and snippets.

@vitkarpov
Last active December 27, 2018 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitkarpov/577bfbcda27994355f1b2ab9fdc4e9dc to your computer and use it in GitHub Desktop.
Save vitkarpov/577bfbcda27994355f1b2ab9fdc4e9dc to your computer and use it in GitHub Desktop.
Computer Science Club, 27.12.2018 (Strings)

Computer Science Club, 27.12.2018

Here's a set of problems for the class. Exploiring strings.

Is Unique

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

Hints:

  • Try a hash table
  • Can you solve it in O(N log N) time? What might a solution like that look like?
  • Could a bit vector be useful?

Check Permutation

Given two strings, write a method to decide if one is a permutation of the other.

Hints:

  • Describe what it means for two strings to be permutations of each other. Now, look at that definition you provided. Can you check the strings against that definition?
  • There is one solution that is O(N log N) time. Another solution uses some space, but is O(N) time.
  • Could a hash table be useful?
  • Two strings that are permutations should have the same characters, but in different orders. Can you make the orders the same?

URLify

Write a method to replace all spaces in a string with %20. You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the "true" length of the string. Assume that you have mutable strings. It's not in many languages but you always can turn a string into an array of chars.

  • It's often easiest to modify strings by going from the end of the string to the beginning.
  • You might find you need to know the number of spaces. Can you just count them?

Credits

Problems and hints could be found in "Cracking the coding interview" by Gayle MacDowell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment