'''
Given two strings that include only lowercase alpha 
characters, str_1 and str_2, write a function that 
returns a new sorted string that contains any character 
(only once) that appeared in str_1 or str_2.

Examples:

csLongestPossible("aabbbcccdef", "xxyyzzz") -> "abcdefxyz"
csLongestPossible("abc", "abc") -> "abc"
'''
'''
Understand:
"Hello" "Friend"
output = "e"

"aabbccddef" "xxyyzz"
output = "abcdefxyz"

"e" "c"
output = 0
'''

def csLongestPossible(str_1, str_2):
    string_ = str_1 + str_2
    dict_ = {}
    for letter in string_.lower():
        if letter not in dict_:
            dict_[letter] = 1
        else:
            dict_[letter] += 1
            
    new_string = []
    for (letter, timesAppeared) in dict_.items():
        new_string.append(letter)
        new_string.sort()
        result = "".join(new_string)
    
    return result
    
    
'''
Runtime: two lengths of strings with for loops -> O(n)
Space: two variables -> O(n)
'''

'''
Given a sorted array (in ascending order) of integers 
and a target, write a function that finds the two integers 
that add up to the target.

Examples:

csSortedTwoSum([3,8,12,16], 11) -> [0,1]
csSortedTwoSum([3,4,5], 8) -> [0,2]
csSortedTwoSum([0,1], 1) -> [0,1]
Notes:

Each input will have exactly one solution.
You may not use the same element twice.
'''

'''
Understand:
[3,8,12,16], 11
output = [0,1]

[0,1], 1
output = [0,1]

Plan:
Iterate over the array to find its complement. Once it's found, return the indices.
'''

def csSortedTwoSum(numbers, target):
    for num in numbers:
        comp = target - num
        if comp in numbers:
            i = numbers.index(num)
            j = numbers.index(comp)
            break
    return i, j            

'''
Runtime: length of numbers in for loop -> O(n)
Space: constant variable -> O(1)
'''

'''
You are given two strings, str_1 and str_2, 
where str_2 is generated by randomly shuffling 
str_1 and then adding one letter at a random position.

Write a function that returns the letter that was added to str_2.

Examples:

csFindAddedLetter(str_1 = "bcde", str_2 = "bcdef") -> "f"
csFindAddedLetter(str_1 = "", str_2 = "z") -> "z"
csFindAddedLetter(str_1 = "b", str_2 = "bb") -> "b"
csFindAddedLetter(str_1 = "bf", str_2 = "bfb") -> "b"
'''

'''
Understand:
"bcde", "bcdef"
output = "f"

"b", "bb"
output = "b"

Plan:
Create a dictionary to store the first string and occurences, then add the second string. Search for the letter that occurs once.
'''

def csFindAddedLetter(str_1, str_2):
    dict_ = {}
    for letter in str_1:
        if letter not in dict_:
            dict_[letter] = 1
        else:
            dict_[letter] += 1
    for letter in str_2:
        if letter in dict_ and dict_[letter] > 0:
            dict_[letter] -= 1
        else:
            return letter
            
'''
Runtime: two strings and separate for loops -> O(n)
Space: one variable -> O(1)
'''
'''
Given a string, write a function that returns the 
index of the first unique (non-repeating) character. 
If there isn't a unique (non-repeating) character, return -1.

Examples:

csFirstUniqueChar(input_str = "lambdaschool") -> 2
csFirstUniqueChar(input_str = "ilovelambdaschool") -> 0
csFirstUniqueChar(input_str = "vvv") -> -1
'''
'''
Understand:
"lambdaschool"
output = 2

"vvv"
output = 0

Plan:
Create a dictionary that stores each character and the timesAppeared. Then find the character with the timesAppeared == 1 and return its index in the string. Else return -1.
'''

def csFirstUniqueChar(input_str):
    unique = []
    for i in range(len(input_str)):
        letters_list = list(input_str)
        letter = letters_list.pop(i)
        if letter not in letters_list:
            unique.append(letter)

    for i in range(len(input_str)):
        if input_str[i] in unique:
            return i
            
    return -1

'''
Runtime: 2 for loops based on string length -> O(n)
Space: one variable -> O(n)
'''

