How do I check if a string is entirely made of the same substring?

I have to create a function which takes a string, and it should return&nbsp;<code>true</code>&nbsp;or&nbsp;<code>false</code>&nbsp;based on whether the input consists of a repeated character sequence. The length of given string is always greater than&nbsp;<code>1</code>&nbsp;and the character sequence must have at least one repetition.

I have to create a function which takes a string, and it should return true or false based on whether the input consists of a repeated character sequence. The length of given string is always greater than 1 and the character sequence must have at least one repetition.

"aa" // true(entirely contains two strings "a")
"aaa" //true(entirely contains three string "a")
"abcabcabc" //true(entirely containas three strings "abc")

"aba" //false(At least there should be two same substrings and nothing more)
"ababa" //false("ab" exists twice but "a" is extra so false)

I have created a function, but it's:

function check(str){
if(!(str.length && str.length - 1)) return false;
let temp = '';
for(let i = 0;i<=str.length/2;i++){
temp += str[i]
//console.log(str.replace(new RegExp(temp,"g"),''))
if(!str.replace(new RegExp(temp,"g"),'')) return true;
}
return false;
}

console.log(check('aa')) //true
console.log(check('aaa')) //true
console.log(check('abcabcabc')) //true
console.log(check('aba')) //false
console.log(check('ababa')) //false

Checking of this is part of the real problem. I can't afford a non-efficient solution like this. First of all it's looping through half of the string.

The second problem is that its using replace() in each loop which makes it slow. Is there a better solution regarding performance?

quicksort Algorithmic in JavaScript

quicksort Algorithmic in JavaScript explain in js


if we are used array in js it peovide collection of methed that found in javaScriptas push pop form and sort() before you start the algrathmic

What is Sorting?

it is only arranging elements in the order we want. You might have come across this in your school or college days. Like arranging numbers from smaller to greater (ascending) or from greater to smaller (descending) is what we saw till now and is called sorting.
if you in javascript and used sort this shape of the example var items = [5,3,7,6,2,9]; console.log(items.sort()); //prints [2, 3, 5, 6, 7, 9]

lets start to expline the Quicksort

Quick sort follows Divide and Conquer algorithm. It is dividing elements in to smaller parts based on some condition and performing the sort operations on those divided smaller parts. Hence, it works well for large datasets.
So, here are the steps how Quick sort works in simple words.

  1. First choose an element which is to be called as pivot element.
  2. Next, compare all array elements with the selected pivot element and arrange them in such a way that, elements less than the pivot element are to it's left and greater than pivot is to it's right.
    3.Finally, perform the same operations on left and right side elements to the pivot element.
    look to this code
 if (arr.length == 0) {return []};
var left=[];
var right=[];
var pivot=arr[0];
for(var i=1;i<arr.length;i++){

 if(arr[i]<pivot){left.push(arr[i])}

 else {right.push(arr[i])}

}
 return quickSort(left).concat(pivot, quickSort(right));

}
console.log(quickSort([5,8,2,1,9,1,0]))

explaine the code
first chek if the length of array no equaleies zero than element the create 2 arr one for left and other for right select the pivot is the firest elment then create loop tom pass all elemnt in array the n check if elment more privent it will push in the right otherwise it add in the left the make call the aagin to implemantation to the leftside with connect withe right withe prive in the midalle withe the quickSort for right side untill the for loop it finsh

Reverse a String in JavaScript and Solutions

Reverse a String in JavaScript and Solutions

Reversing a string is one of the most frequently asked JavaScript question in the technical round of interview, may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or using recursion.

The reverse a string problem is a common algorithm problem. In this article, we will consider four JavaScript solutions to it.

A Little Background Info

Lately, I have been taking a course on data structures and algorithms. This is because I realized that I suck at it. This has not always been the case. When I started learning JavaScript, solving algorithm challenges was the fun part for me. I could stay up for hours late at night just trying to solve a challenge. But two years and many frontend projects later, it seems I've forgotten everything I learned. That is why I decided to go back to learn and practice.

Solid knowledge of data structures and algorithms comes with much practice. And what better way to remember what I learn that to write about it. That's why I am presenting to you the first part of a series of articles to come.

Let's delve into our algorithm challenge for today: Reversing a String

The Problem

Reversing a string is, well, reversing a string. Okay, here's the problem statement: Write a function that reverses a string. If you pass "Sarah" to the function, it should return "haraS" and "listen" should become "netsil". Got it?

Now, let's look at four javascript solutions.

Solution 1. Using the array reverse() method

Thanks to the Array.reverse() method, we can reverse an array without much stress. The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

In our case though, we are working with strings. So this means that we have to convert the string to an array using the split method, reverse it using the reverse method and convert it back to a string using the join method. Here's the code example.

function reverseString(string) {
       //convert the string to an array
       let array = string.split("");
//Use the reverse method
array.reverse()

//Convert it back to a string and return
return array.join("")

}

We can convert this to a one-liner using arrow function and implicit return.

const reverseString = (string) => string.split("").reverse().join('');

That's it. One line of goodness. Let's move to the next solution.

Solution 2: Good Ol' For-loop

This is the classic example of reversing through a string. It might be one of the first methods that will come to your mind if you encounter this problem.

What we will do here is to create an empty string that will hold the reversed string, loop through each character in the string and append it to the beginning of the new string.

    function reverse(str) {
let reverseString = "";

for (let character of str) {
    reverseString = character + reverseString;
}

return reverseString

}

Solution 3 - Using the Array.reduce() method

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value. You can read about it, if you are not familiar with it.

To use the reduce method, we need to convert our string to an array. Next, we use the reduce method to convert it to a reversed string. The reducer, in this case, appends each character of the string to the beginning of the accumulator which, in the code below, is reversed.

function reverseString(string) {
//convert the string to an array
const array = string.split('');

//use the reduce method to convert the array to a reversed string
const reversedString = array.reduce((reversed, character) =&gt; {
    return character + reversed
}, '')

return reversedString

}

This function above can further be reduced to :

const reverseString = (string) => {
return string.split('').reduce((reversed, character) => character + reversed, '')
}

Making it a one-liner and shortening the name of the variables used in the reduce method.

const reverseString = (string) => string.split('').reduce((rev, char) => char + rev, '')
Solution 4 - Using recursion

Are you a friend of recursion? If not, let me introduce you to it.

Recursion is a way of solving a problem by using a function that calls itself. Each time the function calls itself, it reduces the problem into subproblems. This recursive call continues until a point is reached where the subproblem can be called without further recursion.

An important part of a recursive function is a base case. This is the condition(s) where the recursive call terminates to ensure it does not result in an infinite loop.

Let's get back to 'reversing a string'. In this case, our base case is when the string is empty. We use the string.substring() method to get remove the first character in the string and pass the other characters to the function. Then we append the first character to our return statement as seen in the code below.

function reverse(string){
//base case: if str is empty return str
if(string === ""){
return string
} else{
return reverse(string.substring(1)) + string[0]
}
}

We can use a ternary operator instead of if-else statements.

function reverse(string){
return string ? reverse(string.substring(1)) + string[0] : string
}
Conclusion

So there you have it, four cools ways to reverse a string in JavaScript. Have any other solution to this problem, please share it in the comments. I would really love to know.

Thanks for reading. If you liked this post, share it with all of your programming buddies!

Further reading

☞ Svelte.js - The Complete Guide

☞ The Complete JavaScript Course 2019: Build Real Projects!

☞ Become a JavaScript developer - Learn (React, Node,Angular)

☞ JavaScript: Understanding the Weird Parts

☞ Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

☞ The Full JavaScript & ES6 Tutorial - (including ES7 & React)

☞ JavaScript - Step By Step Guide For Beginners

☞ The Web Developer Bootcamp

How do I check if a string is entirely made of the same substring?

I have to create a function which takes a string, and it should return true or false based on whether the input consists of a repeated character sequence.

The length of the given string is always greater than 1 and the character sequence must have at least one repetition.

"aa" // true(entirely contains two strings "a")
"aaa" //true(entirely contains three string "a")
"abcabcabc" //true(entirely containas three strings "abc")

"aba" //false(At least there should be two same substrings and nothing more)
"ababa" //false("ab" exists twice but "a" is extra so false)

I have created the below function:

function check(str){
if(!(str.length && str.length - 1)) return false;
let temp = '';
for(let i = 0;i<=str.length/2;i++){
temp += str[i]
//console.log(str.replace(new RegExp(temp,"g"),''))
if(!str.replace(new RegExp(temp,"g"),'')) return true;
}
return false;
}

console.log(check('aa')) //true
console.log(check('aaa')) //true
console.log(check('abcabcabc')) //true
console.log(check('aba')) //false
console.log(check('ababa')) //false

Checking of this is part of the real problem. I can't afford a non-efficient solution like this. First of all, it's looping through half of the string.

The second problem is that it is using replace() in each loop which makes it slow. Is there a better solution regarding performance?