String Permuations

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

Solution

Complexity:

Assuming that the length are the same, we can count all the characters in one string, and see if the numbers match in the other string. For fast access we can use a hash table or an array to store the counts.

1
2
3
4
5
6
7
8
9
def is_permutation(str1, str2):
  hash = dict()
  for char in str1:
    hash[char] = hash.get(char, 0) + 1
  for char in str2:
    if hash.get(char, 0) == 0:
      return False
    hash[char] -= 1
  return True

Zafar

Zafar
I am a 5th year PhD student at Boston University, working towards my degree in Computer Engineering. While my work focuses on digital design,error mitigation, and machine learning, my non-work interests range widely from information theory (go Shannon!), quantum computing, grandfather paradox, Star Trek, Little Mermaid, 'why is the grass green?', 1Q84, etc., etc., etc. If you want to talk about, well, anything - just ping me.

String Compression

Implement a method to perform string compression using the counts of repeated characters. If the "compressed" string would not become sma...… Continue reading

%20fy the string

Published on December 01, 2016

Reverse String

Published on December 01, 2016