Odd Even Problem | Practice | GeeksforGeeks

Given a string S of lowercase english characters, find out whether the summation of X and Y is even or odd, where X is the count of characters which occupy even positions in english alphabets and have positive even frequency, and Y is the count of characters which occupy odd positions in english alphabets and have positive odd frequency.
Note: Positive means greater than zero.
Example 1:
Input: S = "abbbcc"
Output: "ODD"
Explanation: X = 0 and Y = 1 so (X + Y) is 
ODD. 'a' occupies 1st place(odd) in english 
alphabets and its frequency is odd(1), 'b' 
occupies 2nd place(even) but its frequency 
is odd(3) so it doesn't get counted and 'c' 
occupies 3rd place(odd) but its frequency 
is even(2) so it also doesn't get counted.

Example 2:
Input: S = "nobitaa"
Output: "EVEN"
Explanation: X = 0 and Y = 2 so (X + Y) is
EVEN.

Your Task:  
You dont need to read input or print anything. Complete the function evenOdd() which takes S as input parameter and returns "EVEN"  if  X + Y is even, "ODD" otherwise.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1) 
Constraints:
1 ≤ |S| ≤ 1000
0
bishaldevacc3 months ago
JAVA SOLUTION: 0.2 / 3.7
 
 static String oddEven(String S) {       // code here       int X=0,Y=0;       Map<Integer,Integer> map=new HashMap<Integer,Integer>();              for(int i=0;i<S.length();i++){            int c=(int)S.charAt(i);       map.put(c,map.getOrDefault(c,0)+1);             }         for(int i=0;i<S.length();i++){         int c=(int)S.charAt(i);         if(c%2==0){          if(map.get(c)%2==0){              map.replace(c,-1);              X++;          }            }        else{            if(map.get(c)%2!=0){                 map.replace(c,-1);              Y++;          }            }         }                     if((X+Y)%2==0){           return "EVEN";       }       return "ODD";   }
+2
avinav26113 months ago
Easy C++ Solution
 
0
ansarizia93354 months ago
JAVA 0.1 sec
 static String oddEven(String s) {       int alphabet[]=new int[26];       for(int i=0;i<26;i++)       {alphabet[i]=0;}       int n=s.length();       for(int i=0;i<n;i++)       {           alphabet[(int)s.charAt(i)-97]++;       }       int x=0,y=0;       for(int i=0;i<26;i++)       {  if(alphabet[i]!=0)          { if(((97+i)&1)==0 && (alphabet[i]&1)==0)              x++;             else if(((97+i)&1)==1 && (alphabet[i]&1)==1)             y++;          }       }       return (((x+y)&1)==0?"EVEN":"ODD");   }
0
badgujarsachin836 months ago
string oddEven(string S) {
        // code here
        int even=0;
         int odd=0;
         unordered_map<int,int> mp;
         for(int i=0;i<S.size();i++){
             mp[S[i]]++;
         }
         for(auto it:mp){
             if(it.second%2==0 && (it.first-'a'+1)%2==0){
                 even++;
             }
             if(it.second%2!=0 && (it.first-'a'+1)%2!=0){
                 odd++;
             }
         }
         if((even+odd)%2==0){
             return "EVEN";
         }else{
             return "ODD";
         }
    }
-2
imranwahid6 months ago
Easy C++ solutions
-1
Shobhit Yadav11 months ago
Shobhit Yadav
EASY JAVA SOLN
 Map<character,integer> m=new HashMap<>();      for(int i=0;i<s.length();i++) {="" char="" c="S.charAt(i);" if(m.containskey(c))="" {="" m.put(c,m.get(c)+1);="" }="" else="" m.put(c,1);="" }="" int="" x="0;" int="" y="0;" for(map.entry<character,integer=""> ma : m.entrySet()){            if((ma.getKey()-'a')%2==0){                if(ma.getValue()%2!=0){                    X++;                }            }else{                if(ma.getValue()%2==0){                    Y++;                }            }        }        if((X+Y)%2==0){            return "EVEN";        }else{            return "ODD";        }
0
Sakshi Agarwal1 year ago
Sakshi Agarwal
string oddEven(string S) {        // code here        unordered_map<char,int>m;        for(int i=0;i<s.size();i++){ m[s[i]]++;="" }="" int="" x="0,y" =="" 0;="" for(auto="" it:m){="" if(((it.first-'a')%2!="0)" &&="" it.second%2="=0)" x++;="" else="" if(((it.first-'a')%2="=0)" &&="" it.second%2!="0)" y++;="" }="" return="" (x+y)%2!="0" ?="" "odd"="" :="" "even";="" }<="" code="">
0
Vipul Roy1 year ago
Vipul Roy
string oddEven(string s) {        int n=s.length(),x=0,y=0;        int c[123]={0};        for(int i=0;i<n;i++){ c[s[i]]+="1;" }="" for(int="" i="0;i&lt;n;i++){" if(s[i]%2="=0&amp;&amp;c[s[i]]%2==0){" x++;="" c[s[i]]+="1;" }="" else="" if(s[i]%2="=1&amp;&amp;c[s[i]]%2==1){" y++;="" c[s[i]]+="1;" }="" }="" if((x+y)%2="=0)" return="" "even";="" else="" return="" "odd";="" }="" };="">
0
abhikarsh gupta1 year ago
abhikarsh gupta
C++ using hashing. The description is kind of confusing at first but need to read twice to get the gist of it.  Even indexed alphabet should have even frequency in string, and vice versa, if condition is true, increment count of that type by 1.
Approach :-1. map freq for each element.2. we are aware of index position of each alphabet. a is odd., b is even,  so, we iterate over each element of map and see if its index is even or odd.3. if even index, check if frequency is even. if both even, increment count of even by 1, same for odd.4. check if sum of even and odd count is even or odd.
string oddEven(string S)     {        // code here        unordered_map<char,int> mp;        for(auto it : S)            mp[it]++;        int even = 0, odd = 0, sum = 0;        for(auto it : mp)        {            if((it.first - 'a' + 1) % 2 == 0)            {                if(it.second % 2 == 0)                    even++;            }            else            {                if(it.second % 2)                    odd++;            }        }        sum = odd + even;        return (sum % 2) ? "ODD" : "EVEN";    }
0
Tejas Jhamnani
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.