Advertisement
samimwebdev

Fundamentals problem solving-2

Aug 30th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Create a function in JavaScript that prints the numbers from 1 to n. For multiples of 3, print "Fizz" instead of the number. For multiples of 5, print "Buzz" instead of the number. For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.(Leetcode-412)
  2. // function fizzBuzz(n) {
  3. //   const resultArr = []
  4. //   for (let i = 1; i <= n; i++) {
  5. //     if (i % 3 === 0 && i % 5 === 0) {
  6. //       resultArr.push('FizzBuzz')
  7. //     } else if (i % 3 === 0) {
  8. //       resultArr.push('Fizz')
  9. //     } else if (i % 5 === 0) {
  10. //       resultArr.push('Buzz')
  11. //     } else {
  12. //       resultArr.push(i.toString())
  13. //     }
  14. //   }
  15. //   return resultArr
  16. // }
  17.  
  18. // fizzBuzz(3) // ['1', '2', 'Fizz']
  19. // fizzBuzz(5)// ['1', '2', 'Fizz', 4, 'Buzz']
  20. fizzBuzz(15) //["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
  21.  
  22. // Create a function in JavaScript that counts the number of vowels in a given string.
  23. //O(n)
  24. // const countVowels = (str) => {
  25. //   const vowels = 'aeoiouAEOIU'
  26. //   let count = 0
  27. //   for (let char of str) {
  28. //     if (vowels.includes(char)) {
  29. //       count++
  30. //     }
  31. //   }
  32.  
  33. //   return count
  34. // }
  35. // console.log(countVowels('hello')) // Output: 2
  36. // console.log(countVowels('world')) // Output: 1
  37. // console.log(countVowels('aeiouAEIOU')) // Output: 10
  38. // console.log(countVowels('bcdfghjklmnp')) // Output: 0
  39. // console.log(countVowels('')) // Output: 0
  40. // console.log(countVowels('xyz')) // Output: 0
  41.  
  42. //Leetcode-2586
  43. //TIme and space complexity O(n)
  44. // var vowelStrings = function (words, left, right) {
  45. //   let count = 0
  46. //   const vowels = 'aeiou'
  47. //   for (let i = left; i <= right; i++) {
  48. //     console.log(words[i][0], words[i][words[i].length - 1])
  49. //     if (
  50. //       vowels.includes(words[i][0]) &&
  51. //       vowels.includes(words[i][words[i].length - 1])
  52. //     ) {
  53. //       count++
  54. //     }
  55. //   }
  56.  
  57. //   return count
  58. // }
  59.  
  60. // console.log(vowelStrings(['are', 'amy', 'u'], 0, 2))
  61.  
  62. //Time complexity - O(n)
  63. //space complexity - O(n)
  64. // Leetcode-2129
  65. // var capitalizeTitle = function (title) {
  66. //  split the title into words and make it lowercase
  67. //   const words = title.toLowerCase().split(' ')
  68. //   const outputStrArr = []
  69. //   for (let word of words) {
  70. //     if (word.length > 2) {
  71. //       outputStrArr.push(word[0].toUpperCase() + word.slice(1))
  72. //     } else {
  73. //       outputStrArr.push(word)
  74. //     }
  75. //   }
  76.  
  77. //   return outputStrArr.join(' ')
  78. // }
  79.  
  80. // console.log(capitalizeTitle('capiTalIze tHe titLe'))
  81. // console.log(capitalizeTitle('i lOve leetcode'))
  82.  
  83. // Create a function in JavaScript that checks whether a given number is a prime number.
  84. // A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
  85. // The function should return false for numbers less than 2.
  86. // The function should efficiently check for divisors up to the square root of num.
  87.  
  88. //O(√n)
  89. //space complexity O(1)
  90. // function isPrime(n) {
  91. //   if (n <= 1) return false //0, 1 are not prime numbers
  92. //   if(n <= 3) return true  //2, 3 are prime numbers
  93. //   for(let i = 2; i <= Math.sqrt(n); i++){
  94. //     if(n % i === 0) return false
  95. //     console.log(n % i)
  96. //   }
  97. //   return true
  98. // }
  99. //5, 7, 11, 13
  100.  
  101. //6k + 1 = 7
  102. //6k - 1 = 5
  103. //optimization
  104. //time complexity O(√n).
  105. //space complexity O(1)
  106. // function isPrime(n) {
  107. //   if (n <= 1) return false
  108. //   if (n <= 3) return true
  109. //   if (n % 2 === 0 || n % 3 === 0) return false
  110.  
  111. //   for (let i = 5 ; i <= Math.sqrt(n); i+= 6 ){
  112. //     if(n % i === 0 || n % (i + 2) === 0) return false
  113. //   }
  114.  
  115. //   return true
  116. // }
  117. // // Test cases
  118. // console.log(isPrime(2)) // true
  119. // console.log(isPrime(3)) // true
  120. // console.log(isPrime(4)) // false
  121. // console.log(isPrime(5)) // true
  122. // console.log(isPrime(29)) // true
  123. // console.log(isPrime(97)) // true
  124. // console.log(isPrime(100)) // false
  125.  
  126. // Create a function in JavaScript that removes duplicate values from an array and returns a new array containing only unique values
  127. // function removeDuplicates() {}
  128. // console.log(removeDuplicates([1, 2, 2, 3, 4, 4])) // Output: [1, 2, 3, 4]
  129. // console.log(removeDuplicates(['a', 'b', 'a', 'c', 'b'])) // Output: ['a', 'b', 'c']
  130. // console.log(removeDuplicates([true, false, true, true])) // Output: [true, false]
  131. // console.log(removeDuplicates([1, '1', 2, '2', 2])) // Output: [1, '1', 2, '2']
  132. // console.log(removeDuplicates([])) // Output: []
  133. // console.log(removeDuplicates([n ull, null, undefined, undefined])) // Output: [null, undefined]
  134.  
  135. //Leetcode-217
  136. // var containsDuplicate = function (nums) {
  137. //   const set = new Set()
  138. //   for (num of nums) {
  139. //     if (set.has(num)) {
  140. //       return true
  141. //     } else {
  142. //       set.add(num)
  143. //     }
  144. //   }
  145.  
  146. //   return false
  147. // }
  148.  
  149. // Write a function to reverse a string(Leetcode-344)
  150. //Create a function in JavaScript that takes a string as input and returns a new string with the characters in reverse order.
  151.  
  152. // const reverseString = (str) => {}
  153.  
  154. //  Example of usage:
  155. // console.log(reverseString("hello"));           // Output: "olleh"
  156. // console.log(reverseString("world"));           // Output: "dlrow"
  157. // console.log(reverseString("!dlroW ,olleH"));   // Output: "Hello, World!"
  158. // console.log(reverseString(""));                // Output: ""
  159. // console.log(reverseString("12345"));           // Output: "54321"
  160. // console.log(reverseString("racecar"));         // Output: "racecar"
  161.  
  162. // Create a function in JavaScript that finds the intersection of two arrays, returning a new array containing elements that are present in both input arrays. (Leetcode-349)
  163. var intersection = function (nums1, nums2) {
  164.   const arr1Set = Array.from(new Set(nums1))
  165.   const arr2Set = new Set(nums2)
  166.   const resultArr = []
  167.   for (num of arr1Set) {
  168.     if (arr2Set.has(num)) {
  169.       resultArr.push(num)
  170.     }
  171.   }
  172.  
  173.   return resultArr
  174. }
  175.  
  176. console.log(intersection([1, 2, 2, 1], [2, 2]))
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement