It enterprise face question (Java description)-string rotation (rotate letters or words)

Source: Internet
Author: User

In this chapter we discuss the IT Enterprise interview: string rotation (rotating letters or words).

Topic:

Rotate the string "abcdef" to "DEFABC"

Or

The string "I am a student." Turn into "student." A am I "

And in the above topics will be added to not be able to use the library function restrictions, we will discuss the problem-solving ideas and specific code.


1. Ideas

(1) Violence solution

It's just a character, a character, thrown backwards.

(2) Division method

The non-rotating part and the rotating part are processed separately, first reversing, then the whole reversal

(3) using the stack feature to handle


For the first question we can use the first second way of thinking to deal with, and the third idea is to be in full reversal of the time more brilliant (if it is a certain number of rotations, the third idea will be a lot of trouble to deal with, not recommended in this case)


2. Specific code

(1) The following code is a problem: Rotate the string "abcdef" to "DEFABC"

Package Com.ray.datastructure.ch01.topic_1_1;public class Reversestring_1 {private static String firstchartoend ( String source) {char[] Chararray = Source.tochararray (); char firstletter = chararray[0];for (int i = 1; i < Source.leng Th (); i++) {Chararray[i-1] = chararray[i];} CHARARRAY[CHARARRAY.LENGTH-1] = Firstletter;return new String (Chararray);} public static string Somecharstoend (string source, int. Countofchar) {for (int i = 0; i < Countofchar; i++) {Source = Fi Rstchartoend (source);} return source;} public static void Main (string[] args) {String Source = "abcdef"; System.out.println (Somecharstoend (source, 3));}}

Test output:

Defabc


The above is a brute force solution, a character is thrown back, so the time complexity is O (m*n) relatively poor performance, of course, if the written test is more tense, this is an answer


(2) The following code is a problem: Rotate the string "abcdef" to "DEFABC"

Package Com.ray.datastructure.ch01.topic_1_1;public class Reversestring_2 {private static string reverse (string source , int start, int end) {char[] Chararray = Source.tochararray (); while (start < end) {Char temp = Chararray[start];charar ray[start++] = chararray[end];chararray[end--] = temp;} return new String (Chararray);} public static string reverse (string source, int pos) {if (pos > Source.length ()) {return ' error:post is bigger than sou Rce ' s length;} Source = reverse (source, 0, pos-1); source = reverse (source, POS, Source.length ()-1); source = reverse (source, 0, source . Length ()-1); return source;} public static void Main (string[] args) {String Source = "abcdef"; System.out.println (Reverse (source, 3));}}

Test output:

Defabc


The above code uses the partial method, first the string as two parts, part of the need to move the part, is the above "ABC", the other part is not required to move the part of "Def", the second is the two parts of their respective reversal into the "CBA" "fed", now the return string is "cbafed", The third is the "cbafed" this string of the entire reversal, it becomes the "defabc", such an algorithm, the time complexity becomes O (n)



(3) The following code questions the problem: the string "I am a student." Turn into "student." A am I "

According to the example given in the topic, we should have a word exchange here, so it is no longer like the rotation of each character above

Package Com.ray.datastructure.ch01.topic_1_1;public class Reversestring_3 {public static string reverse (string source, string regex, int start, int end) {string[] Strarray = Source.split (regex); while (start < end) {String temp = strarray[ start];strarray[start++] = strarray[end];strarray[end--] = temp;} String rtnstr = ""; for (int i = 0; i < strarray.length; i++) {rtnstr + = Strarray[i] + regex;} return Rtnstr.trim ();} public static void Main (string[] args) {String Source = "I am a student."; /student. A am istring regex = ""; System.out.println (reverse (source, regex, 0, Source.split (regex). length-1));}}

Test output:

Student. A am I

In general terms, the above is better, because it can customize the exchange of words, for the time being the best implementation



(4) The following code questions the problem: the string "I am a student." Turn into "student." A am I "

For this problem, if we allow the use of library functions, we can introduce the characteristics of the stack

Package Com.ray.datastructure.ch01.topic_1_1;import Java.util.stack;public class Reversestring_4 {public static String reverse (string source, string regex) {string[] Strarray = Source.split (regex); stack<string> stack = new stack<string> (); for (String Item:strarray) {Stack.push (item);} String rtnstr = ""; while (!stack.isempty ()) {rtnstr + = Stack.pop () + regex;} return Rtnstr.trim ();} public static void Main (string[] args) {String Source = "I am a student."; /student. A am istring regex = ""; System.out.println (reverse (source, regex));}}

Test output:

Student. A am I

The above code uses the advanced post-out features of the stack to implement the topic

But there are limitations, it is only in the full rotation of the premise to make, if like the above part can also reverse the situation, it is not recommended to use.


Summary: This chapter notes the end of several methods for string inversion (rotating letters or words).

It enterprise face question (Java description)-string rotation (rotate letters or words)

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.