Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.8 kB
0
Indexable
Never
package NotepadProgram;

import java.util.ArrayList;
class UserSolution
{
    ArrayList<Character> str;
    int H,W, curPos;
    char[][]notepad;
     
    void init(int H, int W, char mStr[])
    {
        this.H = H;
        this.W = W;
        notepad = new char[H][26];
        int i=0;
        str = new ArrayList();
        while(mStr[i] != '\0')
        {
            str.add(mStr[i]);
            notepad[i/W][mStr[i]-'a']++;
            i++;
        }
        curPos=0;
      }
     
    void insert(char mChar)
    {
        int curRow = curPos/W;
        str.add(curPos, mChar);
        int lastRow = (str.size()-1)/W;
        notepad[curRow][mChar-'a']++;
        for(int i =curRow;i<lastRow;i++)
        {
            int ind = i*W +W;
            notepad[i][str.get(ind)-'a']--;
            notepad[i+1][str.get(ind)-'a']++;
        }
        
        curPos++;
    }
 
    char moveCursor(int mRow, int mCol)
    {
        int pos = (mRow-1)*W+(mCol-1); // -1 bcoz starts from 0
        if(pos > str.size()-1)
        {
            curPos = str.size();
            return '$';
        }
        else
        {           
            curPos=pos;
            return str.get(curPos);
        }
             
    }
 
    int countCharacter(char mChar)
    {
         int curRow = curPos/W;
         int count =0;
         int lastRow = (str.size()-1)/W;
          
        for(int i =curRow;i<=lastRow;i++)
            {
            //System.out.println("i="+i+"mChar"+(mChar-'a'));
                count+=notepad[i][mChar-'a'];
            }
        int c =0;
        for(int i=curRow*W ; i< curPos;i++)   
        {
            if(str.get(i)== mChar)
                c++;
        }
        return (count-c);
    }
}
Leave a Comment