Shorter concat [Reverse longer]

Source: Internet
Author: User

Shorter concat [Reverse longer]description:

Given 2 strings, a and b , return a string of the form:shorter+reverse(longer)+shorter.

In other words, the shortest string have to is put as prefix and as suffix of the reverse of the longest.

Strings a b and May is empty, but is not null (in C # Strings may also is null. Treat them as if they is empty.).
If and has the a b same length treat as the a longer producingb+reverse(a)+b

My solution to the dissatisfaction is that if and else judge too much.

An empty string and a non-empty string, which can be directly spliced

usingSystem;usingSystem.Linq;classreverselonger{ Public Static stringShorterreverselonger (stringAstringb) {a= a??string.            Empty; b= b??string.            Empty; stringstr =string.            Empty; if(A.equals (string. Empty)) {if(B.equals (string. Empty)) {}Else{str=string. Join (string.                Empty, B.reverse ()); }            }            Else            {                if(B.equals (string. Empty)) {str=string. Join (string.                Empty, A.reverse ()); }                Else                {                    if(A.length <b.length) {STR= A +string. Join (string. Empty, B.reverse ()) +A; }                    Else{str= B +string. Join (string. Empty, A.reverse ()) +b; }                }            }            returnstr; }}

Other people's Solution:

Better than I was, by comparing size, then using a uniform format to handle

usingSystem.Linq;classreverselonger{ Public Static stringShorterreverselonger (stringAstringb) {if(A = =NULL) A=string.    Empty; if(b = =NULL) b=string.    Empty; if(A.length <b.length) {      stringD =A; A=b; b=D; }    returnB + (New string(A.reverse (). ToArray ())) +b; }}

Shorter concat [Reverse longer]

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.