Cracking The Coding Interview : String Compression

Cracking The Coding Interview : String Compression

Video Link

Description

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string?aabcccccaaa?would become?a2b1c5a3.

If the "compressed" string would not become smaller than the original string, your method should return the original string.

You can assume the string has only upper and lower case letters (a-z).

Example

Example 1:

Input: str = "aabcccccaaa"
Output: "a2b1c5a3"        

Example 2:


Input: str = "aabbcc"
Output: "aabbcc"        

Solution

The solution to this problem involves trivial string manipulation with two pointers approach. First we create a?copy?string and will call it a "dummy String" with an extra delimiter character. After that will declare two pointers one will mark the start of the similar consecutive chars and the second one will indicate the end of the similar consecutive chars and when we hit two chars not similar will append the repetitive char with the number of repetitions. Finally will check if the length of the final compressed string is smaller than the original chars to apply mentioned condition

"If the "compressed" string would not become smaller than the original string, your method should return the original string."


No alt text provided for this image
Mahmoud Ramadan

TechOps Engineer @Sumerge | SRE | DevOps

2 å¹´

?????? ?? ???? ??? ??????? ?????? ??

Ahmed Safwat

Senior SDE @ Pixelogic Media | ex-Orange Labs | Backend Enthusiast

2 å¹´

???? feedback ???? ??? ???? ????? documented ????? ??? ????? ???? ? follow up ??? ??????? ??????? ????? ???? ???? ?? ????? ?? ???? ???? ?? ???? ?? articles ????? LinkedIn

要查看或添加评论,请登录

Ahmed Safwat的更多文章

  • Service Discovery Pattern: The Secret Behind How Microservices Call to Each Other

    Service Discovery Pattern: The Secret Behind How Microservices Call to Each Other

    It all started during a casual lunch break with the team. Our team leader, Fahmy looked frustrated as he stared at his…

    1 条评论
  • Reverse Proxy VS. API Gateway

    Reverse Proxy VS. API Gateway

    It all started during a casual team break. I had been reading some articles online about API Gateways, and a question…

    3 条评论
  • A Journey of Discovery and Decision “API Gateway”

    A Journey of Discovery and Decision “API Gateway”

    It was a regular Monday meeting — one of those where everyone was settling in, coffee cups in hand, waiting for Khaled…

    12 条评论
  • A Tale of Saving White Friday …

    A Tale of Saving White Friday …

    Disclaimer: This is a fictional story created to illustrate the Outbox Pattern. Any resemblance to real people, events,…

    1 条评论
  • Command Your Queries: Unraveling the CQRS Advantage

    Command Your Queries: Unraveling the CQRS Advantage

    Introduction to CQRS Command Query Responsibility Segregation (CQRS) is an architectural pattern that separates the…

    4 条评论
  • Spring Boot Projections Uncovered: How to Fetch Just What You Need

    Spring Boot Projections Uncovered: How to Fetch Just What You Need

    Spring Boot, powered by Spring Data JPA, simplifies the development of data-driven applications. One of its powerful…

    3 条评论
  • BIGINT vs. BIGSERIAL in PostgreSQL

    BIGINT vs. BIGSERIAL in PostgreSQL

    In PostgreSQL, managing large integers efficiently is crucial for many applications, especially when dealing with…

    1 条评论
  • ORM: The Database Magician

    ORM: The Database Magician

    the realm of software development, databases are an indispensable component for storing and managing data. Relational…

    2 条评论
  • Query Plan in a Nutshell

    Query Plan in a Nutshell

    Working with large databases often comes with the challenge of slow query performance. The root cause of this problem…

  • Discover the Secrets of Java Reflection

    Discover the Secrets of Java Reflection

    Java Reflection is a powerful feature in the Java programming language that allows developers to inspect and manipulate…

    3 条评论

社区洞察

其他会员也浏览了