j33vansh

Answer 1 Strings CPP

Jul 2nd, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string str;
  8. cin>>str;
  9.  
  10. int len=str.length();
  11.  
  12. int arr[26]={0};
  13.  
  14. //mapping freq
  15. for(int i=0; i<len; i++)
  16. {
  17. arr[str[i]-'a']++;
  18. }
  19.  
  20. int max=0;
  21. int min =1000000;
  22.  
  23. //finding max and min freq
  24. for(int i=0; i<26; i++)
  25. {
  26. if(max<arr[i])
  27. {
  28. max=arr[i];
  29. }
  30. if(min>arr[i] && arr[i]!=0)
  31. {
  32. min=arr[i];
  33. }
  34. }
  35.  
  36. for(int i=0; i<26; i++)
  37. {
  38. if(arr[i]!=0)
  39. {
  40. if(arr[i]==max || arr[i]==min)
  41. {
  42. continue;
  43. }
  44. else
  45. {
  46. cout<<"NO"<<endl;
  47. return 0;
  48. }
  49. }
  50. }
  51. if(max==min)
  52. {
  53. cout<<"YES"<<endl;
  54.  
  55. }
  56. else
  57. {
  58. //common and uncommon
  59. int freq_max=0;
  60. int freq_min=0;
  61. for(int i=0; i<26; i++)
  62. {
  63. if(arr[i]==max)
  64. {
  65. freq_max++;
  66. }
  67. if(arr[i]==min)
  68. {
  69. freq_min++;
  70. }
  71. }
  72.  
  73. if(max-min!=1)
  74. {
  75. if(min==1 && freq_min==1)
  76. {
  77. cout<<"YES"<<endl;
  78. }
  79. else
  80. {
  81. cout<<"NO"<<endl;
  82. }
  83. }
  84. else
  85. {
  86. if(freq_min==1 || freq_max==1)
  87. {
  88. cout<<"YES"<<endl;
  89. }
  90. else
  91. {
  92. cout<<"NO"<<endl;
  93. }
  94. }
  95. }
  96.  
  97.  
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment