Rotate String

Tags: String, Easy

Question

Problem Statement

Given a string and an offset, rotate string by offset. (rotate from left to
right)

Example

Given "abcdefg".

  1. offset=0 => "abcdefg"
  2. offset=1 => "gabcdef"
  3. offset=2 => "fgabcde"
  4. offset=3 => "efgabcd"

Challenge

Rotate in-place with O(1) extra memory.

题解

常见的翻转法应用题,仔细观察规律可知翻转的分割点在从数组末尾数起的offset位置。先翻转前半部分,随后翻转后半部分,最后整体翻转。

Python - immutable string

  1. class Solution:
  2. """
  3. param A: A string
  4. param offset: Rotate string with offset.
  5. return: Rotated string.
  6. """
  7. def rotateString(self, A, offset):
  8. if A is None or len(A) == 0:
  9. return A
  10. offset %= len(A)
  11. before = A[:len(A) - offset]
  12. after = A[len(A) - offset:]
  13. # [::-1] means reverse in Python
  14. A = before[::-1] + after[::-1]
  15. A = A[::-1]
  16. return A

Python - mutable list

  1. class Solution:
  2. # @param A: a list of char
  3. # @param offset: an integer
  4. # @return: nothing
  5. def rotateString(self, A, offset):
  6. if A is None or len(A) == 0:
  7. return
  8. offset %= len(A)
  9. self.reverse(A, 0, len(A)-offset-1)
  10. self.reverse(A, len(A)-offset, len(A)-1)
  11. self.reverse(A, 0, len(A)-1)
  12. def reverse(self, str_l, start, end):
  13. while start < end:
  14. str_l[start], str_l[end] = str_l[end], str_l[start]
  15. start += 1
  16. end -= 1

C++

  1. class Solution {
  2. public:
  3. /**
  4. * param A: A string
  5. * param offset: Rotate string with offset.
  6. * return: Rotated string.
  7. */
  8. string rotateString(string A, int offset) {
  9. if (A.empty() || A.size() == 0) {
  10. return A;
  11. }
  12. int len = A.size();
  13. offset %= len;
  14. reverse(A, 0, len - offset - 1);
  15. reverse(A, len - offset, len - 1);
  16. reverse(A, 0, len - 1);
  17. return A;
  18. }
  19. private:
  20. void reverse(string &str, int start, int end) {
  21. while (start < end) {
  22. char temp = str[start];
  23. str[start] = str[end];
  24. str[end] = temp;
  25. start++;
  26. end--;
  27. }
  28. }
  29. };

Java

  1. public class Solution {
  2. /*
  3. * param A: A string
  4. * param offset: Rotate string with offset.
  5. * return: Rotated string.
  6. */
  7. public char[] rotateString(char[] A, int offset) {
  8. if (A == null || A.length == 0) {
  9. return A;
  10. }
  11. int len = A.length;
  12. offset %= len;
  13. reverse(A, 0, len - offset - 1);
  14. reverse(A, len - offset, len - 1);
  15. reverse(A, 0, len - 1);
  16. return A;
  17. }
  18. private void reverse(char[] str, int start, int end) {
  19. while (start < end) {
  20. char temp = str[start];
  21. str[start] = str[end];
  22. str[end] = temp;
  23. start++;
  24. end--;
  25. }
  26. }
  27. };

源码分析

  1. 异常处理,A为空或者其长度为0
  2. offset可能超出A的大小,应模len后再用
  3. 三步翻转法

Python 虽没有提供字符串的翻转,但用 slice 非常容易实现,非常 Pythonic!

通常来说,字符串在各种编程语言中的实现一般为 immutable 的,对字符串做改变时往往会生成新的字符串,所以如果要达到空间复杂度为 O(1) 的效果,需要用可变数据结构来实现。

复杂度分析

翻转一次时间复杂度近似为 O(n), 原地交换的空间复杂度为 O(1), 非原地交换的空间复杂度为 O(n). 总共翻转3次,所以总的时间复杂度为 O(n), 空间复杂度为 O(1) 或者 O(n).

Reference