如何检查2个字符串是否相互旋转?

发布于 2024-12-12 02:12:46 字数 276 浏览 0 评论 0原文

给定 2 个字符串,设计一个函数,可以检查它们是否相互旋转而不对其进行任何更改?返回值是布尔值。

例如ABCD、ABDC,它们不是旋转。返回 false

ABCD、CDAB 或 DABC 是旋转。返回真。

我的解决方案:

将其中一个向右或向左移动一个位置,然后在每次迭代时比较它们。 如果它们在所有迭代中都不相等,则返回 false。否则,返回 true。

是O(n)。还有其他更有效的解决方案吗? 如果它们的内容无法更改怎么办?

谢谢

Given 2 strings, design a function that can check whether they are rotations to each other without making any changes on them ? The return value is boolean.

e.g ABCD, ABDC, they are not rotations. return false

ABCD, CDAB or DABC are rotations. return true.

My solution:

shift one of them to right or left one position and then compare them at each iteration.
If they are not equal at all iterations, return false. Otherwise, return true.

It is O(n). Are there other more efficient solutions ?
What if the contents of them cannot be changed ?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

零崎曲识 2024-12-19 02:12:46
  1. 将给定字符串与给定字符串连接起来。

  2. 在连接字符串中搜索目标字符串。

例子:

Given = CDAB

After step 1, Concatenated = CDABCDAB

After step 2, Success CDABCDAB
                        ^^^^
  1. Concatenate the given string with the given string.

  2. Search for the target string in the concatenated string.

Example:

Given = CDAB

After step 1, Concatenated = CDABCDAB

After step 2, Success CDABCDAB
                        ^^^^
凉风有信 2024-12-19 02:12:46

使用两个索引变量可能更有效,而不是移动其中之一。每次从 0 开始,另一个从每个可能的位置(0 到 N-1)开始,然后递增 模组N

Rather than shifting one of them, it might be more efficient to use two index variables. Start one at 0 each time and the other at each of the possible positions (0 to N-1) and increment it mod N.

悸初 2024-12-19 02:12:46

如果无法修改字符串,只需取出 string1 的第一个字符并将其与 string2 的每个字符进行比较。当获得匹配项时,将 string1 的第二个字符与 string2 的下一个字符进行比较,依此类推。

伪代码:

len = strlen(string1);
len2 = strlen(string2);
if( len != len2 )
  printf("Nope.");

for( int i2=0; i2 < len; i2++ ) {
  for( int i1=0; i1<len; i1++ ) {
    if( string1[i1] != string2[(i2+i1)%len] )
      break;
  }
  if( i1 == len ) {
    print("Yup.");
    break;
  }
}

If you can't modify the strings, just take the first character of string1 and compare it to each character of string2. When you get a match, compare the second char of string1 to the next char of string2, and so on.

Pseudocode:

len = strlen(string1);
len2 = strlen(string2);
if( len != len2 )
  printf("Nope.");

for( int i2=0; i2 < len; i2++ ) {
  for( int i1=0; i1<len; i1++ ) {
    if( string1[i1] != string2[(i2+i1)%len] )
      break;
  }
  if( i1 == len ) {
    print("Yup.");
    break;
  }
}
沉睡月亮 2024-12-19 02:12:46

一个简单的例子是:

(s1+s1).find(s2) != string::npos && s1.size() == s2.size();

A simple one would be:

(s1+s1).find(s2) != string::npos && s1.size() == s2.size();
追风人 2024-12-19 02:12:46
  #include <iostream>
  #include <cstring>
  #include<string>
  using namespace std;
  void CompareString(string, string, int);
  int ComputeStringLength(string str);
  int main()
  {
   string str = ""; string str1 = ""; int len = 0, len1 = 0;
   cout << "\nenter string ";
   cin >> str;
   cout << "\nenter string 2 to compare:-  ";
   cin >> str1;

   len = ComputeStringLength(str);
   len1 = ComputeStringLength(str1);
   if (len == len1)
    CompareString(str, str1, len);
   else
    cout << "rotation not possible";
   getchar();
   return 0;
  }

  int ComputeStringLength(string str)
  {
   int len = 0;
   for (int i = 0; str[i] != '\0'; i++)
   {
    len++;
   }
   return len;
  }


  void  CompareString(string str, string str1, int n)
  {
   int index = 0, flag = 0, curr_index = 0, count1 = 0, flagj = 0;
   for (int i = 0; i<n; i++)
   {
    for (int j = flagj; j<n; j++)
    {
     if (str[i] == str1[j])
     {
      index = j;
      flagj =j;
      count1++;
      flag++;
      if (flag == 1)
      {
       curr_index = index;
      }
      break;
     }

    }
   }
   int temp = count1;
   if (count1 != n)
   {
    if (curr_index>=0)
    {
     int k = 0;
     for (int i = n - 1; i>n - curr_index - 1; i--)
     {
      if (str[i] == str1[k])
      {
       temp++;
       k++;
      }

     }
    }
    if (temp == n)
    {
     cout << "\n\nstring is same after rotation";
    }
    else
    {
     cout << "\n\nstring is not same after rotation";
    }
   }
   else
   {
    cout << "\n\nstring is same after rotation";
   }

  }

https://dsconceptuals.blogspot.in /2016/10/a-program-to-check-if-strings-are.html

  #include <iostream>
  #include <cstring>
  #include<string>
  using namespace std;
  void CompareString(string, string, int);
  int ComputeStringLength(string str);
  int main()
  {
   string str = ""; string str1 = ""; int len = 0, len1 = 0;
   cout << "\nenter string ";
   cin >> str;
   cout << "\nenter string 2 to compare:-  ";
   cin >> str1;

   len = ComputeStringLength(str);
   len1 = ComputeStringLength(str1);
   if (len == len1)
    CompareString(str, str1, len);
   else
    cout << "rotation not possible";
   getchar();
   return 0;
  }

  int ComputeStringLength(string str)
  {
   int len = 0;
   for (int i = 0; str[i] != '\0'; i++)
   {
    len++;
   }
   return len;
  }


  void  CompareString(string str, string str1, int n)
  {
   int index = 0, flag = 0, curr_index = 0, count1 = 0, flagj = 0;
   for (int i = 0; i<n; i++)
   {
    for (int j = flagj; j<n; j++)
    {
     if (str[i] == str1[j])
     {
      index = j;
      flagj =j;
      count1++;
      flag++;
      if (flag == 1)
      {
       curr_index = index;
      }
      break;
     }

    }
   }
   int temp = count1;
   if (count1 != n)
   {
    if (curr_index>=0)
    {
     int k = 0;
     for (int i = n - 1; i>n - curr_index - 1; i--)
     {
      if (str[i] == str1[k])
      {
       temp++;
       k++;
      }

     }
    }
    if (temp == n)
    {
     cout << "\n\nstring is same after rotation";
    }
    else
    {
     cout << "\n\nstring is not same after rotation";
    }
   }
   else
   {
    cout << "\n\nstring is same after rotation";
   }

  }

https://dsconceptuals.blogspot.in/2016/10/a-program-to-check-if-strings-are.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文