## The Problem:

Given a string consisting of lowercase English letters, find the largest square which can be obtained by *reordering* its characters and *replacing* them with digits (leading zeros are not allowed) where same characters always map to the same digits and different characters always map to different digits.

If there is no solution, return `-1`

.

**Example**

- For
`s = "ab"`

, the output should be

`constructSquare(s) = 81`

; - For
`s = "zzz"`

, the output should be

`constructSquare(s) = -1`

.There are no 3-digit square numbers with identical digits.

- For
`s = "aba"`

, the output should be

`constructSquare(s) = 900`

.It can be obtained after reordering the initial string into

`"baa"`

.

**Input/Output**

**[time limit] 4000ms (py)**

**[input] string s***Constraints:*

`2 ≤ s.length < 10`

.**[output] integer**

## The Solution:

from itertools import permutations def constructSquare(s): sq=[] for x in range(100001): sq.append(x**2) l = len(s) found = -1 for x in sq: if len(str(x))>l: break elif len(str(x))==l: mm = permutations(s) for m in mm: d={} e = {} fl=1 for a,b in zip(str(x),m): if a in d: if d[a]!=b: fl=0 break if b in e: if e[b]!=a: fl=0 break d[a]=b e[b]=a if fl: found=x return found

## The Explanation:

Advertisements