Encoding String, by Amazon

Common problem being asked these days (this problem has been asked actually since 2016 at least), by Amazon (via Daily Coding Problem):

Good morning!
This is your coding interview problem for today.
This problem was asked by Amazon.
Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.
We will be sending the solution tomorrow, along with tomorrow's question. As always, feel free to shoot us an email if there's anything we can help with.
Have a great day!

First I want to say that encoding one simple character like "A" as "1A" is actually a bad thing, as you're increasing the length of the output string by 100%. Hence if there is only one char, just copy it over, no need to prefix it with the count.
I've solved a similar albeit more complicated problem from Leetcode, here: https://anothercasualcoder.blogspot.com/2016/12/leetcode-decode-string.html
It is really hard to mess up the solution for this problem. I've seen even nested loops solution able to solve in O(n) by properly manipulating the indexes.
My solution (O(n)) goes like this: if the previous char is the same as the current, increment the count. Otherwise, copy the previous char with/without prefix and keep going. Make sure to handle the last character too. That's it, cheers, Marcelo.

https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem10132018.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DailyCodingProblem
{
 /*
 Good morning!
 Here's a solution to yesterday's problem.
 This is your coding interview problem for today.
 This problem was asked by Amazon.
 Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
 Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.
 We will be sending the solution tomorrow, along with tomorrow's question. As always, feel free to shoot us an email if there's anything we can help with.
 Have a great day!
  */
 class DailyCodingProblem10132018
 {
  public string RunLengthEncode(string str)
  {
   if (String.IsNullOrEmpty(str) || str.Length == 1) return str;

   string retVal = "";
   int count = 1;

   for (int i = 1; i < str.Length; i++)
   {
    if (str[i] == str[i - 1]) count++;
    else
    {
     if (count > 1) retVal += count.ToString();
     retVal += str[i - 1].ToString();
     count = 1;
    }
   }
   if (count > 1) retVal += count.ToString();
   retVal += str[str.Length - 1].ToString();

   return retVal;
  }
 }
}

Comments

Popular posts from this blog

Changing the root of a binary tree

Prompt Engineering and LeetCode

ProjectEuler Problem 719 (some hints, but no spoilers)