Data Structure String In JavaScript  | Frontend Interview Helper

Data Structure String In JavaScript | Frontend Interview Helper


3 min read

The basics | Inbuilt Function of Js

  1. How to declare a String in JS

  • Accessing Characters

  1. Looping a string

  1. Modifying Strings

  1. Concat & Trim (Trim has only removed side spaces | didn't remove space in between )

  1. Searching in String

  1. Took a part from a string ( Slice, Subdtring)

  1. 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

  1. Convert a String

  1. Check Ascii value or Convert to Ascii Value

  1. String Compare

it returns 0 if it is the same otherwise -1

  1. To check whether something is included in String or not

It returns True | False values

  1. 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 {
// 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

now โ€ข

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


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 ):


Top 10 most asked frontend interview questions from string

  1. Reverse a String

  2. Check Palindrome

  3. String Anagrams

  4. Longest Substring Without Repeating Characters

  5. String Compression

  6. Implement strStr()

  7. Valid Parentheses

  8. Longest Common Prefix

  9. Group Anagrams

  10. Count and Say

Thank you So Much For Reading if you get some knowledge from this BLOG Connect with me on LinkedIn

Linkedin | GitHub

Made by Lust Love By Sameer Faridi (21 April 2023)

Thank you Roadside Coder (Piyush) bhaiya for such amazing content.

๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚ Sorry