Question: Given the problem: Find a repeated substring in a string, is it possible to use hashing? I want to create a dictionary with the substrings as keys and the number of repeated instances as values. Here is what I have so far. I am getting an error because I am using a substring as a key for the dictionary. Can anyone spot my mistake? Thank you!!!

```
def findsubs(str):
  d={}
  for i in range(len(str)-1):
    for j in range(i+2, len(str)-2):
      if d[str[i:j]]>1:
        return str[i:j]
      else:
        d[str[i:j]] = d[str[i:j]] +1

   return 0

```

print findsubs("abcbc")

Answer: 
The general idea should work. It's just that if a key isn't found in the dictionary when you do a lookup, you get an error - so you have to check whether the key exists before doing a look-up and initialize if it is doesn't:

```
def findsubs(str):
  d={}
  for i in range(len(str)-1):
    for j in range(i+2, len(str)-2):
      if str[i:j] not in d:
        d[str[i:j]] = 0

      if d[str[i:j]]>1:
        return str[i:j]
      else:
        d[str[i:j]] = d[str[i:j]] +1

   return 0

```

Note that instead of `if str[i:j] not in d: d[str[i:j]] = 0`, you can do `d.setdefault(str[i:j], 0)`, which sets the value to `0` if the key isn't in the dict, and leaves it unchanged if it does.

A few more comments though:

* You should return `None`, not `0`, if you don't find anything.
* You shouldn't call a variable `str` since that's a built-in function.
* You want to iterate `j` until the end of the string.
* As-written, it'll only return a substring if it's been found 3 times. Really you can re-write it using a set of previously-found substrings instead:

So:

```
def findsubs(s):
  found = set()
  for i in range(len(s)-1):
    for j in range(i+2, len(s)+1):
      substr = s[i:j]
      if substr in found:
        return substr
      found.add(substr)

  return None

```
You were almost there

```
def findsubs(instr):
  d={}
  for i in range(len(instr)):
    for j in range(i+2, len(instr)+1):
      print instr[i:j]
      d[instr[i:j]] = d.get(instr[i:j],0) + 1
  return d      

instr = 'abcdbcab'
print instr
print findsubs('abcdbcab')

```

This will work, i added an inside print for debug purposes, remove it after you test it. 

The result is the dict with the substring count has you asked for :)

{'abcd': 1, 'ab': 2, 'cdb': 1, 'dbc': 1, 'cdbcab': 1, 'cd': 1, 'abc': 1, 'cdbc': 1, 'bcab': 1, 'abcdbc': 1, 'ca': 1, 'db
ca': 1, 'bc': 2, 'dbcab': 1, 'db': 1, 'cab': 1, 'bcdbcab': 1, 'bcdbc': 1, 'abcdbca': 1, 'cdbca': 1, 'abcdbcab': 1, 'bcdb
': 1, 'bcd': 1, 'abcdb': 1, 'bca': 1, 'bcdbca': 1}