<jupyter_start><jupyter_text>Sparse Arrays<jupyter_code>strings=['abcde',
         'sdaklfj',
         'asdjf',
         'na',
         'basdn',
         'sdaklfj',
         'asdjf',
         'na',
         'asdjf',
         'na',
         'basdn',
         'sdaklfj',
         'asdjf']
queries=['abcde','sdaklfj','asdjf','na','basdn']
def mapa(string):
    mapa={}
    for word in string:
        if word not in mapa:
            mapa[word]=1
        else:
            mapa[word]+=1
    return mapa
mapa_string=mapa(strings)
mapa_string
contador=[]

for word in queries:
    if word in mapa_string:
        print(word)
        print(mapa_string[word])
        contador.append(mapa_string[word])
    else:
        contador.append(0)
    
    
contador
def matchingStrings(strings, queries):
    mapa_string=mapa(strings)
    count=[]
    for word in queries:
        if word in mapa_string:
            count.append(mapa_string[word])
        else:
            count.append(0)
    return count
    
matchingStrings(strings,queries)<jupyter_output><empty_output><jupyter_text>find loop on linked list<jupyter_code>def has_cycle(head):
    current=head
    mapa={}
    while current:
        if current in mapa:
            return True
        else:
            mapa[current]=True
            current=current.next
    return False<jupyter_output><empty_output><jupyter_text>swap nodes bts<jupyter_code>class TreeNode(object):
    def __init__(self,data,left=None,right=None):
        self.data=data
        self.left=left
        self.right=right
node1=TreeNode(1)
node2=TreeNode(2)
node3=TreeNode(3)
node4=TreeNode(4)
node5=TreeNode(7)
node6=TreeNode(8)

node1.left=node2
node1.right=node3
node2.left=node4
node3.left=node5
node3.right=node6
root=node1
def inOrder(root,array=[]):
    if root and root.left:
        inOrder(root.left,array)
    array.append(root.data)
    if root and root.right:
        inOrder(root.right,array)
    return array
inOrder(root)
def swapEveryLevel(root):
    if (root is None or (root.left is None and root.right is None)):
        return
    root.left,root.right=root.right,root.left
    swapEveryLevel(root.left)
    swapEveryLevel(root.right)
    
    
swapEveryLevel(root)
inOrder(root)
## si se busca intercambiar nodos, cada k numero de niveles
def swapKLevel(root,level=1,k=1):
    if (root is None or (root.left is None and root.right is None)):
        return
    if (level+1)%k==0:
        root.left,root.right=root.right,root.left
    root.left,root.right=root.right,root.left
    swapKLevel(root.left,level+1,k)
    swapKLevel(root.right,level+1,k)<jupyter_output><empty_output><jupyter_text>check BST<jupyter_code># corregir cuando hay un valor repetido
def check_bst_helper(root,minimo,maximo):
    if root is None:
        return True
    if (minimo is not None and root.data<=minimo) or (maximo is not None and root.data>maximo):
        return False
    if not (check_bst_helper(root.left,minimo,root.data)):
        return False
    if not (check_bst_helper(root.right,root.data,maximo)):
        return False
    return True
def check_duplicate(root,array=[]):
    if root and root.left:
        check_duplicate(root.left,array)
    if root.data in array:
        return False
    array.append(root.data)
    if root and root.right:
        check_duplicate(root.right,array)
    return  True
    
def check_binary_search_tree_(root):
    return check_bst_helper(root,None,None) and check_duplicate(root)
check_binary_search_tree_(root)
s=set()
s
### solucicon con elementos repetidos
def check_bst_helper(root,minimo,maximo):
    if root is None:
        return True
    if (minimo is not None and root.data<=minimo) or (maximo is not None and root.data>maximo):
        return False
    if not (check_bst_helper(root.left,minimo,root.data)):
        return False
    if not (check_bst_helper(root.right,root.data,maximo)):
        return False
    return True

def check_duplicate( root, s) : 
    
    if (root == None) : 
        return False
    if root.data in s:  
        return True
    s.add(root.data)  
    return check_duplicate(root.left, s) or check_duplicate(root.right, s)  
def check_binary_search_tree_(root):
    s=set()
    return check_bst_helper(root,None,None) and not(check_duplicate(root,s))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10868047474982546, 'fraction_numerical': 0.0076797765883174305, 'mean_word_length': 3.0243445692883895, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '250999', 'license': 'MIT', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1497, 'n_words': 317, 'path': 'hackerRank/.ipynb_checkpoints/data Structures medium-checkpoint.ipynb', 'repo_name': 'emmanuelol/CodingPractice', 'size': '4297'}