My c# solution


  • 0
    T
    public class Solution {
        public string SplitLoopedString(string[] strs) {
            
            int len = strs.Length;
            if(len == 0)
                return "";
            char max_c = 'a';
            
            var queue = new Queue<string>();
            for(int i = 0;i<len;i++){
                string s = strs[i];
                string r_s = Reverse(s);
                if(r_s.CompareTo(s)  == 1){
                    strs[i] = r_s;
                    s = r_s;
                }
                foreach(var c in s){
                    if(c > max_c){
                        max_c = c;
                    }
                }
                queue.Enqueue(s);
            }
            
            string result = string.Join("",strs);
            int n = 0;
            while(n < len){
                var s = queue.Dequeue();
                var backUp_s = s;
                var s_len = s.Length;
                var remain = string.Join("",queue);
                for(int i = 0;i<s_len;i++){
                    if(s[i] == max_c){
                        string newS= s.Substring(i)+remain+s.Substring(0,i);
                        //Console.WriteLine(newS+","+result);
                        if(newS.CompareTo(result) == 1){
                            result = newS;
                        }
                    }
                }
                s = Reverse(s);
                for(int i = 0;i<s_len;i++){
                    if(s[i] == max_c){
                        string newS= s.Substring(i)+remain+s.Substring(0,i);
                        if(newS.CompareTo(result) == 1){
                            result = newS;
                        }
                    }
                }
                
                queue.Enqueue(backUp_s);
                n++;
            }
            return result;
        }
        
        private string Reverse(string s){
            int i = 0;
            int j = s.Length-1;
            var arr = s.ToCharArray();
            while(i < j){
                char c = arr[i];
                arr[i] = arr[j];
                arr[j] = c;
                i++;
                j--;
            }
            return new string(arr);
        }
    }
    

  • 0
    Y

    In Reverse method, you can use Array.Reverse(arr) to reverse it.


  • 0
    T

    @YCJ that is good idea. I forgot . Thanks


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.