[Google電面] String decompression

Problem Description

    input              output

2[abc]3[a]c => abcabcabcaaac;     2[ab3[d]]2[cc] => abdddabdddcccc


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:                                      // using divide and conquer,  recursion
    string decompression(string src) {      // 2[ab3[d]]2[cc] => abdddabdddcccc
       if(src=="") return "";
       if(src[0]>='a'&&src[0]<='z'||src[0]>='A'&&src[0]<='Z') {
            for(int i=0;i<src.size();i++) {
                if(src[i]>='0'&&src[i]<='9') {
                    return src.substr(0,i)+decompression(src.substr(i));
                }
            }
            return src;
       }
       else {
           int start=-1,end,ans=0;
           for(int i=0;i<src.size();i++) {
               if(src[i]=='[') {
                   ans++;
                   if(start==-1) {
                      start=i;
                   }
               }
               else if(src[i]==']') {
                   ans--;
                   if(ans==0) {
                      end=i;break;
                   }
               }
           }
           string numStr=src.substr(0,start);
           stringstream trans;
           trans<<numStr;
           int num;
           trans>>num;
           string res="";
           string nex=decompression(src.substr(start+1,end-start-1));
           for(int i=0;i<num;i++) {
               res+=nex;
           }
           if(end==src.size()-1) return res;
           else return res+decompression(src.substr(end+1));
       }
    }

    char findKthChAfterDecompression(string src,int k) {    //follow up Question (presume k<=src.size())
        if(src[0]>='a'&&src[0]<='z'||src[0]>='A'&&src[0]<='Z') {
            for(int i=0;i<src.size();i++) {
                if(src[i]>='0'&&src[i]<='9') {
                    if(i>=k) return src[k-1];
                    else return findKthChAfterDecompression(src.substr(i),k-i);
                }
            }
            return src[k-1];
       }
       else {
           int start=-1,end,ans=0;
           for(int i=0;i<src.size();i++) {
               if(src[i]=='[') {
                   ans++;
                   if(start==-1) {
                      start=i;
                   }
               }
               else if(src[i]==']') {
                   ans--;
                   if(ans==0) {
                      end=i;break;
                   }
               }
           }
           string numStr=src.substr(0,start);
           stringstream trans;
           trans<<numStr;
           int num;
           trans>>num;
           string res="";
           string nex=decompression(src.substr(start+1,end-start-1));
           int curlen=nex.size()*num;
           if(curlen>=k) {
               return nex[k%nex.size()];
           }
           else return findKthChAfterDecompression(src.substr(end+1),k-curlen);

       }
    }

};
int main()
{
    string test="2[ab3[d]]2[cc]";
    string test1="2[abc]3[a]c";
    Solution t;
    cout<<t.decompression(test1)<<endl;
    cout<<t.findKthChAfterDecompression(test1,10)<<endl;
    return 0;
}

也可以用棧實現,附上別人的代碼

public class Solution {
    public String decompression(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        LinkedList<Character> stack = new LinkedList<Character>();
        for(int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ']') {
                stack.push(s.charAt(i));
            } else if (s.charAt(i) == ']') {
                StringBuilder sb = new StringBuilder();
                char curC = stack.pop();
                while(cur != '[') {
                    sb.append(curC);
                    curC = stack.pop();
                }
                int num = getRepeatTimes(stack);
                String str = sb.toString();
                for(int i = 0; i < num; i++) {
                    for(int j = str.length() - 1; j >= 0; j--) {
                        stack.push(str.charAt(j));
                    }
                }
            }
        }
        
        StringBuilder ret = new StringBuilder();
        while(!stack.isEmpty()) {
            ret.append(stack.pop());
        }
        return ret.reverse().toString();
    }
   
    int getRepeatTimes(LinkedList<Character> stack) {
        int ret = 0;
        int base = 1;
        while(stack.peek() >= '0' && stack.peek() <= '9') {
            ret += Integer.parseInt(stack.pop()) * base;
            base *= 10;
        }
    }
}




     
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章