The basics | Inbuilt Function of Js
How to declare a String in JS
Accessing Characters
Looping a string
Modifying Strings
Concat & Trim (
Trim has only removed side spaces | didn't remove space in between
)
Searching in String
Took a part from a string ( Slice, Subdtring)
Convert Case ( Str -> number | Num -> Str | etc..)
This is important because if we Typecast an Object in Js it gives a [ Object Object ] simple mines gives us its type
The Right way to do that
Convert a String
Check Ascii value or Convert to Ascii Value
String Compare
it returns 0 if it is the same otherwise -1
To check whether something is included in String or not
It returns True | False
values
Splitting and Joining Strings
It breaks the string from " " (space) occurs
Finally The Main Data Structure & Algorithm Questions Start.
My Solution:
const mainString = ` Hay Good Morning Brother`;
const maxLenght = 10;
if (mainString.length >= maxLenght) {
let trimedvalue = mainString.trim();
let finalVersionOfString = trimedvalue.replaceAll(" ", "");
console.log(`${finalVersionOfString.slice(0, 10)}...`);
} else {
console.log(mainString);
}
// Output : HayGoodMor...
Piyush's Solution:
Q2: Plaindrome
Piysh's solution.
The { + } operator makes this string to a number
eg: console.log(+"10"); ==> 10
My Solution:
I add comments with ChatGPT (I think it is a better approach because sometimes interviewers don't allow to use inbuilt functions of a language )
quick Question
Can Someone Explain?
Hey, I tried my best and approached this question in five different ways using JavaScript. The average runtime is around 140ms. However, when I attempted the same question in Java, it only took around 9ms. How is this possible? Why is there such a significant difference? Could someone please explain?
JavaScript Solution (140ms)
var isPalindrome = function(x) { const numStr = x.toString(); const length = numStr.length; const halfLength = Math.floor(length / 2); for (let i = 0; i < halfLength; i++) { if (numStr[i] !== numStr[length - 1 - i]) { return false; } } return true; };
Java Solution (9ms)
class Solution { public boolean isPalindrome(int x) { int rev=0,rem=0; int temp=0; temp=x; while(x!=0) { rem=x%10; rev=rev*10+rem; x=x/10; } if(temp<0) { rev=rev*-1; } if(temp==rev) { return true; } else return false; } }
Question 03: 461. Hamming Distance
A quick tip: Convert a number to binary just | let num = 10; num.toString(2) --> that 2 refers base^2
Solution:
I don't know why on leetcode in this program time limit exceeds.
Here is the optimized Solution.
Question 04: 242. Valid Anagram
Solution: (UnOptimized ) Easy one:
Solution: (Optimized ):
Bonus:
Top 10 most asked frontend interview questions from string
Reverse a String
Check Palindrome
String Anagrams
Longest Substring Without Repeating Characters
String Compression
Implement strStr()
Valid Parentheses
Longest Common Prefix
Group Anagrams
Count and Say
Thank you So Much For Reading if you get some knowledge from this BLOG Connect with me on LinkedIn
Made by Lust Love By Sameer Faridi (21 April 2023)
Thank you Roadside Coder (Piyush) bhaiya for such amazing content.
๐๐๐๐ Sorry