📌  相关文章
📜  字典上最小的字符串旋转 |设置 1

📅  最后修改于: 2022-05-13 01:57:07.584000             🧑  作者: Mango

字典上最小的字符串旋转 |设置 1

编写代码以查找圆形数组中的字典最小值,例如对于数组 BCABDADAB,字典最小值为 ABBCABDAD。
来源:谷歌笔试
更多示例:

Input:  GEEKSQUIZ
Output: EEKSQUIZG

Input:  GFG
Output: FGG

Input:  GEEKSFORGEEKS
Output: EEKSFORGEEKSG

以下是一个简单的解决方案。让给定的字符串为 'str'
1) 将 'str' 与其自身连接并存储在一个临时字符串中,例如 'concat'。
2)创建一个字符串数组来存储'str'的所有旋转。让数组为“arr”。
3) 通过在索引 0、1、2..n-1 处获取 'concat' 的子字符串来查找 'str' 的所有旋转。将这些旋转存储在 arr[]
4) 排序 arr[] 并返回 arr[0]。

以下是上述解决方案的实现。

C++
// A simple C++ program to find lexicographically minimum rotation
// of a given string
#include 
#include 
using namespace std;
 
// This functionr return lexicographically minimum
// rotation of str
string minLexRotation(string str)
{
    // Find length of given string
    int n = str.length();
 
    // Create an array of strings to store all rotations
    string arr[n];
 
    // Create a concatenation of string with itself
    string concat = str + str;
 
    // One by one store all rotations of str in array.
    // A rotation is obtained by getting a substring of concat
    for (int i = 0; i < n; i++)
        arr[i] = concat.substr(i, n);
 
    // Sort all rotations
    sort(arr, arr+n);
 
    // Return the first rotation from the sorted array
    return arr[0];
}
 
// Driver program to test above function
int main()
{
    cout << minLexRotation("GEEKSFORGEEKS") << endl;
    cout << minLexRotation("GEEKSQUIZ") << endl;
    cout << minLexRotation("BCABDADAB") << endl;
}


Java
// A simple Java program to find
// lexicographically minimum rotation
// of a given String
import java.util.*;
 
class GFG
{
 
    // This functionr return lexicographically
    // minimum rotation of str
    static String minLexRotation(String str)
    {
        // Find length of given String
        int n = str.length();
 
        // Create an array of strings
        // to store all rotations
        String arr[] = new String[n];
 
        // Create a concatenation of
        // String with itself
        String concat = str + str;
 
        // One by one store all rotations
        // of str in array. A rotation is
        // obtained by getting a substring of concat
        for (int i = 0; i < n; i++)
        {
            arr[i] = concat.substring(i, i + n);
        }
 
        // Sort all rotations
        Arrays.sort(arr);
 
        // Return the first rotation
        // from the sorted array
        return arr[0];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(minLexRotation("GEEKSFORGEEKS"));
        System.out.println(minLexRotation("GEEKSQUIZ"));
        System.out.println(minLexRotation("BCABDADAB"));
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# A simple Python3 program to find lexicographically
# minimum rotation of a given string
 
# This function return lexicographically minimum
# rotation of str
def minLexRotation(str_) :
 
    # Find length of given string
    n = len(str_)
 
    # Create an array of strings to store all rotations
    arr = [0] * n
 
    # Create a concatenation of string with itself
    concat = str_ + str_
 
    # One by one store all rotations of str in array.
    # A rotation is obtained by getting a substring of concat
    for i in range(n) :
        arr[i] = concat[i : n + i]
 
    # Sort all rotations
    arr.sort()
 
    # Return the first rotation from the sorted array
    return arr[0]
 
# Driver Code
print(minLexRotation("GEEKSFORGEEKS"))
print(minLexRotation("GEEKSQUIZ"))
print(minLexRotation("BCABDADAB"))
 
# This code is contributed by divyamohan123


C#
// A simple C# program to find
// lexicographically minimum rotation
// of a given String
using System;
 
class GFG
{
 
    // This functionr return lexicographically
    // minimum rotation of str
    static String minLexRotation(String str)
    {
        // Find length of given String
        int n = str.Length;
 
        // Create an array of strings
        // to store all rotations
        String []arr = new String[n];
 
        // Create a concatenation of
        // String with itself
        String concat = str + str;
 
        // One by one store all rotations
        // of str in array. A rotation is
        // obtained by getting a substring of concat
        for (int i = 0; i < n; i++)
        {
            arr[i] = concat.Substring(i, n);
        }
 
        // Sort all rotations
        Array.Sort(arr);
 
        // Return the first rotation
        // from the sorted array
        return arr[0];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        Console.WriteLine(minLexRotation("GEEKSFORGEEKS"));
        Console.WriteLine(minLexRotation("GEEKSQUIZ"));
        Console.WriteLine(minLexRotation("BCABDADAB"));
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

EEKSFORGEEKSG
EEKSQUIZG
ABBCABDAD