Given a string,determine if it is compressed of all unique characters. For example, the string ‘abcde’ has all unique characters and should return True. The string ‘aabcde’ contains duplicate characters and should return false.
We’ll see two possible solutions, one using a built-in data structure and a built in function, and another using a built-in data structure but using a look-up method to check if the characters are unique.
def uni_char(s): return len(set(s)) == len(s)
def uni_char2(s): chars = set() for let in s: # Check if in set if let in chars: return False else: #Add it to the set chars.add(let) return True
""" RUN THIS CELL TO TEST YOUR CODE """ from nose.tools import assert_equal class TestUnique(object): def test(self, sol): assert_equal(sol(''), True) assert_equal(sol('goo'), False) assert_equal(sol('abcdefg'), True) print('ALL TEST CASES PASSED') # Run Tests t = TestUnique() t.test(uni_char)
ALL TEST CASES PASSED
Recommended: Understand Big-O Notation Complexity Of Algorithm
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…