JavaScript implements Palindrome decoding

Source: Internet
Author: User

The title is also from the front-end engineer Pen questions. Title Description:

Now there is a string that you want to perform n operations on the string, giving two numbers per operation: (P, L) a substring of l that starts with the character from Subscript p in the current string. You're going to flip this substring around and plug it right behind the substring's original position, and ask what the last string is. The subscript for a string is 0-based.

Idea: First get the substring about (p,l), get substring can use string.substring method. Then the inverse of the rotor string, there is no reversal method, but the array has, so you can first convert the string into an array, reversed and then converted to a string. Finally, the inverted substring is inserted after the original substring. The implementation of inserting strings into the original string at a specific location, the string does not have a ready-made method, the two methods, 1 is can use Substr,slice and other methods to first split the string and then in the connection. 2 is to use the splice method of the array, first convert the string to an array, and then use the Solice method to insert the element at the specified position and then into the string.

varinput = "ab\n2\n0 2\n1 3"; functionReverscon (input) {Input_array= Input.split ("\ n"); varNLine = 0;  while(NLine <input_array.length) {          varline = input_array[nline++].trim (); if(line = = = "){              Continue; }          vars =Line ; varn = +input_array[nline++];  while(n--){              varInput_arrays = Input_array[nline++].trim (). Split ('); varp = +input_arrays[0]; varL = +input_arrays[1]; //Your code            varsubstring = s.substring (p,p+l); Substring= Substring.split ("). Reverse (). Join ('); varArrs = S.split (");//This step requires a variable to save the result, because splice returns theArrs.splice (p+l,0,substring);//The characters are removed, so the ligatures will return emptys = Arrs.join (");//Abbaabb}} console.log (s);       };  Reverscon (input); 

JavaScript implements Palindrome decoding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.