Solution to Perm-Check by codility
Question: http://codility.com/demo/take-sample-test/perm_check Question Name: PermCheck This question is a simple variant of the counting question. The Python solution is:
1 2 3 4 5 6 7 8 9 10 11 12 | def solution(A): counter = [0]*len(A) limit = len(A) for element in A: if not 1 <= element <= limit: return 0 else: if counter[element-1] != 0: return 0 else: counter[element-1] = 1 return 1 |
The Java solution is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Solution { public static int solution(int[] A) { int[] counter = new int [A.length]; for(int i= 0; i< A.length; i++){ if (A[i] < 1 || A[i] > A.length) { // Out of range return 0; } else if(counter[A[i]-1] == 1) { // met before return 0; } else { // first time meet counter[A[i]-1] = 1; } } return 1; } } |