Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
3
Indexable
Never
import java.util.ArrayList;
 
class UserSolution {
    int h, w, cursor, length;
    int data[][];
    ArrayList<Character> arraylist = new ArrayList<>();
 
    void init(int H, int W, char mStr[]) {
        this.h = H;
        this.w = W;
        arraylist.clear();
        data = new int[H][26];
        cursor = 0;
        length = 0;
        for (int i = 0; mStr[i] != '\0'; i++) {
            arraylist.add(mStr[i]);
            //System.out.print(mStr[i]);
            data[i / W][mStr[i] - 'a']++;
            length++;
        }
    //  System.out.println();
    }
 
    void insert(char mChar) {
        int row = cursor / w;
        arraylist.add(cursor, mChar);
        length++;
        for (int i = row; i < (length - 1) / w; i++) {
            data[i][arraylist.get((i + 1) * w) - 'a']--;
            data[i + 1][arraylist.get((i + 1) * w) - 'a']++;
        }
        data[row][mChar - 'a']++;
        cursor++;
    }
 
    char moveCursor(int mRow, int mCol) {
        int row = mRow - 1;
        int col = mCol - 1;
        int newcusor = row * w + col;
        if (newcusor < length) {
            cursor = newcusor;
            //System.out.println("move: " + arraylist.get(cursor));
            return arraylist.get(cursor);
        }
        cursor = length;
        return '$';
    }
 
    int countCharacter(char mChar) {
        int row = cursor / w;
        int result = 0;
        for (int i = row; i <= (length - 1) / w; i++) {
            result += data[i][mChar - 'a'];
            //System.out.println("data[" + i / w + "][" + (mChar - 'a') + "]");
        }
        for (int i = row * w; i < cursor; i++) {
            if (arraylist.get(i) == mChar)
                result--;
        }
        //System.out.println();
        ///System.out.println("cnt: " + result);
        return result;
    }
}
Leave a Comment