Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created January 10, 2020 05:51
Show Gist options
  • Select an option

  • Save blacksheep557/9a22c9ff8a15d5566710a394a4f5d69c to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/9a22c9ff8a15d5566710a394a4f5d69c to your computer and use it in GitHub Desktop.
day 63 part with permutatio // to get permutation with no same adjacent elements
void addToList(List<dynamic> numbers, List<List<dynamic>> temp) {
List<dynamic> tempo = [];
numbers.forEach((integer) {
tempo.add(integer);
});
temp.add(tempo);
}
void swap(List<dynamic> list, int i, int j) {
dynamic temp = list[i];
list[i] = list[j];
list[j] = temp;
}
List<List<dynamic>> permute(List<dynamic> numbers, int start) {
List<List<dynamic>> temp = [];
if (start == numbers.length - 1) {
addToList(numbers, temp);
return temp;
}
for (int i = start; i < numbers.length; i++) {
swap(numbers, start, i);
temp.addAll(permute(numbers, start + 1));
swap(numbers, start, i);
}
return temp;
}
int getPermutationWithoutSameAdjacentCharacters(String word) {
List<String> wordList = word.split('').toList();
List<List<dynamic>> output = permute(wordList, 0);
int count = 0;
output.forEach((word) {
if (!isSameCharactersAdjacent(word)) {
count++;
}
});
return count++;
}
bool isSameCharactersAdjacent(List<dynamic> word) {
for (int i = 0; i < word.length - 1; i++) {
if (word[i] == word[i + 1]) {
return true;
}
}
return false;
}
void main() {
print(getPermutationWithoutSameAdjacentCharacters("aabb"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment