fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. bool check(int arr[],int length);
  5. int count_anaagrans(char text[],char word[],int m,int n);
  6. void copy(int *source,int * destination,int length);
  7. bool check_word(char text[],int word[],int i,int j);
  8. int main()
  9. {
  10. char text[100];
  11. char word[100];
  12. int m,n;
  13. scanf("%s",text);
  14. scanf("%s",word);
  15. m = strlen(text);
  16. n = strlen(word);
  17. printf("%d",count_anaagrans(text,word,m,n));
  18. }
  19.  
  20. int count_anaagrans(char text[],char word[],int m,int n){
  21. int count=0;
  22. int occ[26];
  23. int temp[26];
  24. int i=0;
  25. int length =0;
  26. int start =0;
  27. for(i=0;i<26;i++){
  28. occ[i]=0;
  29. }
  30. for(i=0;i<n;i++){
  31. occ[*(word+i)-'a'] = occ[*(word+i)-'a']+1;
  32. }
  33. start =0;
  34. i=n-1;
  35. while(i<m){
  36. if(check_word(text,occ,start,i)){
  37. count++;
  38. start++;
  39. i++;
  40. }
  41. else{
  42. start++;
  43. i++;
  44. }
  45.  
  46. }
  47. return count;
  48. }
  49. bool check(int arr[],int length){
  50. bool ans = true;
  51. int i=0;
  52. for(i=0;i<length;i++){
  53. if(arr[i]>0){
  54. ans = false;
  55. break;
  56. }
  57. }
  58. return ans;
  59. }
  60.  
  61. void copy(int *source,int * destination,int length){
  62. int i=0;
  63. for(i=0;i<length;i++){
  64. *(destination+i) = *(source+i);
  65. }
  66. }
  67.  
  68. bool check_word(char text[],int word[],int i,int j){
  69. int temp[26];
  70. copy(word,temp,26);
  71. int k=0;
  72. for(k=i;k<=j;k++){
  73. temp[*(text+k)-'a'] = temp[*(text+k)-'a']-1;
  74. }
  75. if(check(temp,26)){
  76. return true;
  77. }
  78. else{
  79. return false;
  80. }
  81. }
  82.  
Runtime error #stdin #stdout 0s 2296KB
stdin
forfor
for
stdout
4