Advertisement
samimwebdev

Example Problem- Problem Solving Approach

Aug 30th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // function countCharacter(inputStr) {
  2. //   convert the string to lowercase
  3. //   const lowerCaseStr = inputStr.toLowerCase()
  4. //   Remove Spaces and special characters
  5. //   const cleanStr = lowerCaseStr.replace(/[^a-z0-9]/gi, '')
  6. //   create a object to hold the counts or frequency
  7. //   const charCount = {}
  8. //    loop the cleaned string and count each character
  9. //   for (let i = 0; i < cleanStr.length; i++) {
  10. //     const char = cleanStr[i]
  11. //     if (charCount[char]) {
  12. //       charCount[char] += 1
  13. //     } else {
  14. //       charCount[char] = 1
  15. //     }
  16. //   }
  17.  
  18. //  return the result
  19. //   return charCount
  20. // }
  21. // countCharacter('Hel @lo') // Output: { h: 1, e: 1, l: 2, o: 1 }
  22.  
  23. //optimization
  24. //Time complexity - O(n + n) = O(n)
  25. //Space complexity- O(n)
  26.  
  27. // function countCharacter(inputStr) {
  28. //   convert the string to lowercase
  29. //   const lowerCaseStr = inputStr.toLowerCase()
  30. //   create a object to hold the counts or frequency
  31. //   const charCount = {}
  32. //   loop the cleaned string and count each character
  33. //   for (let i = 0; i < lowerCaseStr.length; i++) {
  34. //     const char = lowerCaseStr[i]
  35. //    Remove Spaces and special characters during add frequency of the character to object
  36. //     if ((char >= 'a' && char <= 'z') || (char >= '0' && char <= '9')) {
  37. //ternary operator
  38. // charCount[char] = charCount[char] ? charCount[char] + 1 : 1
  39. //Alternate solution
  40. //       charCount[char] = (charCount[char] || 0) + 1
  41. //     }
  42. //   }
  43.  
  44. // return the result
  45. //   return charCount
  46. // }
  47. // console.log(countCharacter('Hel @lo'))
  48. // console.log(countCharacter('')) // Output: {}
  49. // console.log(countCharacter('Hello123!@#Hello'))
  50. // console.log(countCharacter('héllo😊héllo'))
  51.  
  52. //time complexity O(n * m) -> O(nm)
  53. //space complexity - O(1)
  54. // function isElementExists(arr1, arr2) {
  55. // loop arr1 and check each element with 2nd array element
  56. //   for (let elm of arr1) {
  57. //compare each element with 2nd arr element
  58. // if (arr2.includes(elm)) {
  59. //   return true
  60. // }
  61. //     for (let elm2 of arr2) {
  62. //       if (elm === elm2) {
  63. //         return true
  64. //       }
  65. //     }
  66. //   }
  67.  
  68. //   return false
  69. // }
  70. //Time Complexity O(m +  n)
  71. //Space complexity - O(n)
  72. function isElementExists(arr1, arr2) {
  73.   //loop arr1 and tack down each data
  74.   //   const trackingObj = {}
  75.  
  76.   //   for (let elm of arr1) {
  77.   //     trackingObj[elm] = true
  78.   //   }
  79.  
  80.   const arr1Set = new Set(arr1)
  81.   console.log(arr1Set)
  82.   //loop arr2 and check each element existence in the tracking Object
  83.   for (let elm of arr2) {
  84.     // if (elm in trackingObj) {
  85.     //   return true
  86.     // }
  87.     if (arr1Set.has(elm)) {
  88.       return true
  89.     }
  90.   }
  91.   return false
  92. }
  93. console.log(isElementExists(['a', 'b', 'z'], [1, 2, 3, 'z']))
  94. console.log(isElementExists(['a'], [])) // Output: false
  95. console.log(isElementExists([], [1, 2, 3])) // Output: false
  96. console.log(isElementExists(['x', 'y', 'z'], [1, 2, 3, 'a'])) // Output: false
  97. console.log(isElementExists(['a', 'b', 'c'], [1, 2, 3, 'b'])) // Output: true
  98. console.log(isElementExists(['a', 'b', 'c'], ['a', 'b', 'c'])) // Output: true
  99. console.log(isElementExists(['a', 'b', 'c'], ['a', 'b', 'c'])) // Output: true
  100. console.log(isElementExists(['1', '2', '3'], [1, 2, 3])) // Output: false
  101. console.log(isElementExists([1, 2, 3], [1, 2, 3, '1'])) // Output: true
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement