Oracle interview Question


  • 0
    M

    Below question asked during oracle Phone screening, though to share.

    Given a string, output the run length encoding of that string so that repeat letters are compressed to the character and count.
    Example:
    abcccddeeeeea => abc3d2e5a

    string CompressString(string str)
    {
    
      if (str.Length < 2)
      {
        return str;
      }
      StringBuilder output = new StringBuilder();
      int count =1;
      for (int i=0 ; i < str.Length; i++ )
      {
         if (i+1 < str.Length && str[i] == str[i+1] )
         {
             count++;
             i++;
            }
         
           if (count > 1)
           {
            output.Append(count);
            count =1;
           } 
             i++;
         }
         
        
         return output.ToString();
      }
    
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.