Find the dimension of the set $S=\{B_{6\times 6}: AB=BA\}$. 
Let , $A_{6\times 6}$ diagonal matrix with characteristic polynomial $x(x+1)^2(x-1)^3$. Find the dimension of the set $S=\{B_{6\times 6}: AB=BA\}$.

From characteristic polynomial of $A$ , first we right the matrix as 
$$A=\left[\begin{matrix}0&&&&&&&\\&-1&&&&&&\\&&-1&&&&&\\&&&&1&&&\\&&&&&1&&\\&&&&&&1\end{matrix}\right].$$ Then I take $B$ in general form and find $AB$ and $BA$. Then  taking $AB=BA$ and solving we can find $$B=\left[\begin{matrix}a_1&0&0&&&\\0&a_2&a_3&&&\\0&a_4&a_5&&&\\&&& a_ 6 & a_ 7 & a_ 8\\&&& a_ 9 & a_{ 10 } & a_{ 11 }\\&&& a_{ 12 } & a_{ 13 } & a_{ 14 }\end{matrix}\right].$$
So, clearly  $dim(S)=14$.
But it is very lengthy and laborious...

Are there any other simplest way to find the dimension of $S$ without taking $B$ in general form ?


We can find the dimension of this space a bit more efficiently by breaking $A$ into block matrices.  In particular, we have
$$
A = \pmatrix{0\\&-I_2\\&&I_3}
$$
Now, an arbitrary $B$ can be written as 
$$
B = \pmatrix{
B_{11} & B_{12} & B_{13}\\
B_{21}& B_{22} & B_{23}\\
B_{31} & B_{32} & B_{33}
}
$$
Where the two matrices have been partitioned in the same way. So, we have
$$
AB = 
\pmatrix{
0 & 0 & 0\\
-B_{21}& -B_{22} & -B_{23}\\
B_{31} & B_{32} & B_{33}
}, \quad
BA =
\pmatrix{
0 & -B_{12} & B_{13}\\
0&  -B_{22} & B_{23}\\
0 & -B_{32} & B_{33}
}
$$
From there, we can quickly reach the desired conclusion.
I believe this is the quickest way to answer the question.

We could generalize this into an inductive argument.
Begin with a diagonal matrix $A$.  Then, let
$$
A' = 
\pmatrix{
A & 0\\
0 & cI_k
}
$$
for some $c \in \Bbb C$.  If we want to find the set of matrices $B'$ such that $A'B' = B'A'$, then we could write $B'$ as a block matrix of the form
$$
B' = \pmatrix{B_{11} & B_{12}\\B_{21} & B_{22}}
$$
We then have
$$
A'B' = 
\pmatrix{
AB_{11} & AB_{12}\\
cB_{21} & cB_{22}
}, \quad
B'A' = 
\pmatrix{
B_{11}A & cB_{12}\\
B_{21}A & cB_{22}
} 
$$
We can then conclude that $B_{11}A = AB_{11}$, $B_{12} = 0 = B_{21}$, and $B_{22}$ can be freely chosen.
Now, we can immediately deduce a general form for $S$ as long as $A$ is diagonal.
