fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<stdbool.h>
  4.  
  5. // Function to check if two strings are equal after shifting
  6. bool canBeEqualAfterShifts(char S[], char goal[]) {
  7. int countS[26] = {0}, countG[26] = {0};
  8.  
  9. // Count the occurrences of each character in the initial string
  10. for (int i = 0; i < strlen(S); i++)
  11. countS[S[i] - 'a']++;
  12.  
  13. // Count the occurrences of each character in the goal string
  14. for (int i = 0; i < strlen(goal); i++)
  15. countG[goal[i] - 'a']++;
  16.  
  17. // If the count of each character in both strings is the same, return true
  18. for (int i = 0; i < 26; i++)
  19. if (countS[i]!= countG[i])
  20. return false;
  21.  
  22. return true;
  23. }
  24.  
  25. int main() {
  26. char S[] = "abcdefg";
  27. char goal[] = "defgabc";
  28.  
  29. if (canBeEqualAfterShifts(S, goal))
  30. printf("true");
  31. else
  32. printf("false");
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
true