private static int BruteForce(char[] data) { for (int i = data.Length; i > 1; i--) { for (int j = 0; j <= data.Length - i; j++) { int diff = 0; for (int k = j; k < j + i; k++) { if (LettersAndNumbers.IsNumber(data[k])) { diff++; } else { diff--; } } if (diff == 0) { return(i); } } } return(0); }
private static int HashTable(char[] data) { int[] diffs = new int[data.Length]; int diff = 0; for (int i = 0; i < data.Length; i++) { if (LettersAndNumbers.IsNumber(data[i])) { diff++; } else { diff--; } diffs[i] = diff; } HashTable <int, int> hashTable = new HashTable <int, int>(); hashTable[0] = -1; int max = 0; for (int i = 0; i < diffs.Length; i++) { if (hashTable.Contains(diffs[i])) { max = Math.Max(max, i - hashTable[diffs[i]]); } else { hashTable[diffs[i]] = i; } } return(max); }