Snippets Collections
// SOLUTION 1 - MY (DISCOVERED) SOLUTION (RUNTIME - 104MS, 44.9 MB)
var isAnagram = function(s, t) {
    
    // set function to split, sort, and rejoin characters in a string
    const sortString = (str) => {
        return str.split("").sort().join("");
    }
    
    // regex removes any non-alphabet characters in the string and makes it lowercase
    s = s.replace(/[^\w]/g, '').toLowerCase()
    t = t.replace(/[^\w]/g, '').toLowerCase()

    // final comparison
    return sortString(s) === sortString(t)
  
  // ATTEMPT 1
  //     let anagram = []
    
  // return false is lengths don't match
  //     if (s.length !== t.length) return false;
  //     else {
  //         for (let i = 0; i < s.length; i++) {
  //             let arrT = t.split("")
  //             let newArrT;
  //             if (arrT.includes(s.charAt(i))) {
  //                 let index = arrT.indexOf(s.charAt(i));
  //                 console.log("s char: ", s.charAt(i));
  //                 console.log("index: ", index);

  //                 anagram.push(s.charAt(i))

  //                 console.log("splice: ", arrT.splice(index, 1))
  //                 arrT.splice(index, 0)

  //                 newArrT = arrT
  //                 console.log("newArr: ", newArrT)
  //             };
  //         };
  //     }
  //     console.log(anagram)
  //     return anagram.join("") === s;
}

// SOLTUION 2 - (BEST RUNTIME - 60MS)
var isAnagram = function(s, t) {
    const key = w => Object.entries([...w].reduce((a, c) => {
        if (!(c in a)) a[c] = 0;
        a[c] += 1;
      
        return a;
    }, {})).sort(([c1], [c2]) => c1.localeCompare(c2)).flat().join('');
  
    return key(s) === key(t);
};

// SOLUTION 3 - (RUNTIME - 72MS)
var isAnagram = function(s, t) {
    if(s.length !== t.length) return false;
  
    let map = {};
  
    for(let item of s) {
        map[item] = map[item] + 1 || 1;
    }
    
    for(let item of t) {
        if(!map[item]) return false;
        else map[item]--;
    }
  
    return true;
};
// SOLUTION 1 - MY (DISCOVERED) SOLUTION (RUNTIME - 84MS, MEMORY - 41.2MB)
var firstUniqChar = function(s) {
  // loop through the characters of the string
  for (var i = 0; i < s.length; i++) {
    // set a variable for the char
    var c = s.charAt(i);
    // if the index of the char == i, and the index of 'c', starting search from index 'i + 1' == -1
    if (s.indexOf(c) == i && s.indexOf(c, i + 1) == -1) {
      return s.indexOf(c);
    }
  }
  return -1;
};

// SOLUTION 2 (BEST RUNTIME - 68MS)
var firstUniqChar = function(s) {
  count = []

    for(let i=0;i<s.length;i++){
      index = s.charCodeAt(i)-'a'.charCodeAt(0)
   
      if(count[index]==undefined){
        count[index]=1
      }else{
        count[index]++
      }
    }
    
    for(let i=0;i<s.length;i++){
      index = s.charCodeAt(i)-'a'.charCodeAt(0)
      if(count[index]==1){
        return i
      }
    }
    return -1
};

// SOLUTION 3 - (RUNTIME - 96MS)
var firstUniqChar = function(s) {
    for (i=0; i < s.length; i++) {
      if (s.indexOf(s[i]) == s.lastIndexOf(s[i])) {
        return i
      }
    }
   return -1
}
// SOLUTION 1 - MY SOLUTION (RUNTIME - 104MS, 45.9 MB)
var reverseString = function(s) {
    s.reverse()
};

// SOLUTION 2 (BEST RUNTIME - 80MS)
var reverseString = function(s) {
    // declared here to set a stable loop constant
    // sets the indecies (ie. s.length = 6, indecies -> 0-5)
    let length = s.length - 1;
    
    //a will store value of current str so it isn't lost during reassignment
    let a;
    
    //loop will only need to reach the midpoint to reverse string
    for(let i = Math.floor(length/2); i >= 0; i--){
        a = s[i];
        s[i] = s[Math.abs(i - length)];
        s[Math.abs(i - length)] = a;
    }
    return s;
};

// SOLUTION 3 (RUNTIME - 108MS)
var reverseString = function(s) {
  // set up a for loop with 'i' only iteration up to half the length of 's'
    for(let i = 0; i < s.length / 2; i++) {
        let temp = s[s.length-1-i];
        s[s.length-1-i] = s[i];
        s[i] = temp;
    }    
};
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
for (x =6; x >= 1; x--)
{
for (y = 1; y < x; y++)
{
Console.Write(" ");
}
for (z = 6; z >= x; z--)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
1.# For-Else Syntax

2.for item in seq:
    3.statement 1
    4.statement 2
    5.if <cond>:
        6.break
7.Else:
8.Example:
     9.birds = ['Belle', 'Coco', 'Juniper', 'Lilly', 'Snow']
10.ignoreElse = False


11.for theBird in birds:
    12.print(theBird )
    13.if ignoreElse and theBird is 'Snow':
        14.break
15.else:
    16.print("No birds left.")
                              
                                
days = 0
week = [‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, 3.‘Sunday’]
while day < 7:
print(“Today is” + week[days])
days += 1
                                
                                
# Program to add natural
# numbers upto 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

1.n = 10

# initialize sum and counter
2.sum = 0
3.i = 1

4.while i <= n:
   5. sum = sum + i
   6. i = i+1    # update counter

# print the sum
7.print("The sum is", sum)

When you run this code output will be:
Enter n: 10
The sum is 55
#make two lists:
1.num_list = [1, 2, 3]
2.alpha_list = ['a', 'b', 'c']

#use for loop for 1st list:
3.for number in num_list:
#print the list    
4.print(number)
#use for loop for @nd list:    
5.for letter in alpha_list:

send(to, from, count)
	register short *to, *from;
	register count;
	{
		register n=(count+7)/8;
		switch(count%8){
		case 0:	do{	*to = *from++;
		case 7:		*to = *from++;
		case 6:		*to = *from++;
		case 5:		*to = *from++;
		case 4:		*to = *from++;
		case 3:		*to = *from++;
		case 2:		*to = *from++;
		case 1:		*to = *from++;
			}while(--n>0);
		}
	}
#include <iostream>
#include "unordered_set"
#include "basicActions.h"
using namespace std;

void createLoop(Node *head)
{
    Node *p = head;

    while (p->next != nullptr)
    {
        p = p->next;
    }
    p->next = head;
    return;
}

bool detectLoop(Node *head)
{
    unordered_set<Node *> s;
    Node *p = head;

    while (p != nullptr)
    {
        if (s.find(p) != s.end())
            return true;

        s.insert(p);
        p = p->next;
    }
    return false;
}

int main()
{
    Node *head = nullptr;
    insertAtEnd(head, 1);
    insertAtEnd(head, 2);
    insertAtEnd(head, 3);
    insertAtEnd(head, 4);
    insertAtEnd(head, 5);

    createLoop(head);

    detectLoop(head) ? cout << "Loop Detected.." << '\n' : cout << "No Loop Detected.." << '\n';
}
star

Tue Sep 21 2021 14:51:47 GMT+0000 (Coordinated Universal Time) https://leetcode.com/submissions/detail/558649741/?from=explore&item_id=882

#javascript #strings #regex #sort #anagram #loops #unsolved
star

Fri Sep 17 2021 16:21:34 GMT+0000 (Coordinated Universal Time) https://leetcode.com/submissions/detail/555922232/?from=explore&item_id=881

#javascript #strings #uniquecharacter #characters #loops #unsolved
star

Tue Sep 14 2021 13:26:49 GMT+0000 (Coordinated Universal Time) https://leetcode.com/submissions/detail/554777828/?from=explore&item_id=879

#javascript #strings #loops #reversal
star

Thu Nov 12 2020 22:03:53 GMT+0000 (Coordinated Universal Time) https://www.educba.com/patterns-in-c-sharp/

#c# #loops #for
star

Tue Apr 21 2020 05:48:50 GMT+0000 (Coordinated Universal Time)

#python #python #loops #forloop #forelse
star

Tue Apr 21 2020 05:36:09 GMT+0000 (Coordinated Universal Time) https://www.freecodecamp.org/news/python-example/

#python #python #loops #whileloop
star

Tue Mar 31 2020 11:54:39 GMT+0000 (Coordinated Universal Time) ttps://www.programiz.com/python-programming/while-loop

#python #pyhton #loops #whileloop
star

Tue Mar 31 2020 05:29:39 GMT+0000 (Coordinated Universal Time)

#python #python #loops #forloop #nestedfor loop
star

Sun Feb 09 2020 19:39:03 GMT+0000 (Coordinated Universal Time) http://www.lysator.liu.se/c/duffs-device.html

#C #historicalcode #loops #starwars
star

Fri Jan 03 2020 19:00:00 GMT+0000 (Coordinated Universal Time) https://github.com/manosriram/Data-Structures/blob/master/linkedLists/detectLoop.cpp

#ios #swift #apps #loops

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension