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 - 92MS, MEMORY - 40.7MB)
var reverse = function(x) {
    // set variable that converts integer to a string, splits into an arr, reverses arr, and rejoins into a string
    const temp = x.toString().split("").reverse().join("");
    // converts string back into an integer
    let reversedNum = parseInt(temp);
    
    // using original argument, check if reversed number needs to be a positive or negative value
    if (Math.sign(x) === -1) reversedNum = reversedNum * -1
    
    // check to see if reversed integer fits within listed 32-bit contraint
    if (Math.pow(-2, 31) > reversedNum || reversedNum > Math.pow(2, 31)) return 0;
    else return reversedNum  
};

// SOLUTION 2 (BEST RUNTIME - 60MS)
var reverse = function(x) {
    const limit = 2147483648;
    const reversedNum = parseFloat(x.toString().split("").reverse().join(""))
    return reversedNum > limit ? 0 : reversedNum * Math.sign(x);
};

// SOLUTION 3 (RUNTIME - 60MS)
var reverse = function(x) {
    const sign = x < 0 ? -1 : 1;
    const positiveX = x * sign;
    const y = parseInt(positiveX.toString().split('').reverse().join('')) * sign;
    const absoluteY = y * sign;
    
  //0x7FFFFFFF is a number in hexadecimal (2,147,483,647 in decimal) that represents the maximum positive value for a 32-bit signed binary integer
    return absoluteY <= (y < 0 ? 0x80000000 : 0x7FFFFFFF) ? y : 0;
};
// 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;
    }    
};
const date = '2021-06-14T08:29:40.886+00:00'
const formattedDate = date.substring(0,date.lastIndexOf('T'))
console.log(formattedDate)

// prints: "2021-06-14"
function palindrome(str) {
    const alphanumericOnly = str
        // 1) Lowercase the input
        .toLowerCase()
        // 2) Strip out non-alphanumeric characters
        .match(/[a-z0-9]/g);
        
    // 3) return string === reversedString
    return alphanumericOnly.join('') ===
        alphanumericOnly.reverse().join('');
}



palindrome("eye");

n = 2

s ="Programming"

print(s * n) # ProgrammingProgramming
This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.
   
#make a function:
def get_vowels(string):

#return is the keyword which means function have to return value: 
 return [each for each in string if each in 'aeiou']


#assign the words and function will return vowels words.
get_vowels('foobar') # ['o', 'o', 'a']


get_vowels('gym') # []
#use print command
1.print (“Mary had a little lamb.”)
2.print (“I am 19 years old.”)
def byte_size(string):




 return(len(string.encode('utf-8')))


  


 


byte_size('😀’) # 4
byte_size('Hello World') # 11
#assign a value to a variable:
types_of_people = 10 
# make a string using variable name:
X = f “there are {types_of_people} types of people.”

Output:
There are 10 types of people
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

Wed Sep 15 2021 13:34:13 GMT+0000 (Coordinated Universal Time) https://leetcode.com/submissions/detail/555353674/?from=explore&item_id=880

#javascript #strings #reversal #integers
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 Jun 17 2021 11:04:08 GMT+0000 (Coordinated Universal Time)

#javascript #strings #substring #lastindexof
star

Tue Aug 04 2020 19:25:25 GMT+0000 (Coordinated Universal Time) https://www.freecodecamp.org/news/freecodecamp-palindrome-checker-walkthrough/

#javascript #strings #arrays
star

Mon Apr 20 2020 13:58:55 GMT+0000 (Coordinated Universal Time) https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172

#python #python #strings
star

Tue Mar 31 2020 11:35:03 GMT+0000 (Coordinated Universal Time) https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172

#python #python #strings #vowels #function
star

Tue Mar 31 2020 11:27:37 GMT+0000 (Coordinated Universal Time)

#python #python #printfunction #strings
star

Tue Mar 31 2020 06:23:25 GMT+0000 (Coordinated Universal Time) https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172

#python #python #len #bytesize #strings
star

Mon Mar 30 2020 10:16:54 GMT+0000 (Coordinated Universal Time) https://www.amazon.com/Learn-Python-Hard-Way-Introduction/dp/0321884914

#python ##python #strings #comments

Save snippets that work with our extensions

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