Rotate String【判断一个字符串是否为另一个字符串的偏转】

本文介绍了一种用于判断通过旋转操作使两个字符串相等的高效算法。该算法利用双倍拼接源字符串的方法来快速查找目标字符串是否存在于源字符串的某种旋转形式中。此方法简单且易于实现。

PROBLEM:

We are given two strings, A and B.

shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.

Example 1:
Input: A = 'abcde', B = 'cdeab'
Output: true

Example 2:
Input: A = 'abcde', B = 'abced'
Output: false

Note:

  • A and B will have length at most 100.

SOLVE:

class Solution {
public:
    bool rotateString(string A, string B) {
        return A.size()==B.size()&&(A+A).find(B)!=string::npos;
    }
};
def rotateString(self, A, B):        
    return len(A) == len(B) and B in A + A
C语言实现字符串重新排列有多种方法,以下介绍常见的字符串旋转、排列组合和排序三种方式: ### 字符串旋转 字符串旋转是将字符串中的字符按照指定规则重新排列。在C语言中,可以使用数组表示字符串,并操作数组元素实现旋转 [^1]。 ### 字符串排列组合 对于每个字符位置 `begin`,先将该位置字符与第一个位置交换,递归调用函数对剩余的 `n - 1` 个字符做全排列。递归返回时,把交换过的字符还原。再将第 `begin` 位置的字符依次换成第二个、第三个……直到最后一个字符的位置,重复上述步骤,可得到所有可能的排列组合 [^2]。 ### 字符串排序 原理是利用256个ASCII码,两个字符串比较时,逐步比较每个字符的ASCII码。`<string.h>` 中内置的 `strcmp` 函数能直接实现该要求,也可自定义 `my_strcmp` 函数 [^3]。 以下是各方法的示例代码: #### 字符串旋转示例代码 ```c #include <stdio.h> #include <string.h> // 反转字符串函数 void reverse(char* str, int start, int end) { while (start < end) { char temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } // 字符串旋转函数 void rotateString(char* str, int n) { int len = strlen(str); n = n % len; // 处理 n 大于字符串长度的情况 reverse(str, 0, n - 1); reverse(str, n, len - 1); reverse(str, 0, len - 1); } int main() { char str[] = "abcdefg"; int n = 2; rotateString(str, n); printf("旋转后的字符串: %s\n", str); return 0; } ``` #### 字符串排列组合示例代码 ```c #include <stdio.h> #include <string.h> // 交换两个字符 void swap(char* a, char* b) { char temp = *a; *a = *b; *b = temp; } // 生成全排列 void permutations(char* str, int begin, int end) { if (begin == end) { printf("%s\n", str); } else { for (int i = begin; i <= end; i++) { swap(&str[begin], &str[i]); permutations(str, begin + 1, end); swap(&str[begin], &str[i]); // 还原 } } } int main() { char str[] = "abc"; int len = strlen(str); permutations(str, 0, len - 1); return 0; } ``` #### 字符串排序示例代码 ```c #include <stdio.h> #include <string.h> // 自定义字符串比较函数 int my_strcmp(const char* str1, const char* str2) { while (*str1 && *str2 && *str1 == *str2) { str1++; str2++; } return *str1 - *str2; } // 冒泡排序字符串数组 void bubbleSort(char strs[][50], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (my_strcmp(strs[j], strs[j + 1]) > 0) { char temp[50]; strcpy(temp, strs[j]); strcpy(strs[j], strs[j + 1]); strcpy(strs[j + 1], temp); } } } } int main() { char strs[][50] = {"banana", "apple", "cherry"}; int n = sizeof(strs) / sizeof(strs[0]); bubbleSort(strs, n); for (int i = 0; i < n; i++) { printf("%s\n", strs[i]); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值