{"id":"4e627e33-af41-4161-a9c0-0ce06b313137","name":"String Compression","description":"1. You are given a string. \r\n2. You have to compress the given string in the following two ways - \r\n First compression -> The string should be compressed such that consecutive duplicates of characters are replaced with a single character.\r\n For \"aaabbccdee\", the compressed string will be \"abcde\".\r\n Second compression -> The string should be compressed such that consecutive duplicates of characters are replaced with the character and followed by the number of consecutive duplicates.\r\n For \"aaabbccdee\", the compressed string will be \"a3b2c2de2\".","inputFormat":"A String","outputFormat":"Two strings representing first compressed string and second compressed string respectively.","constraints":"1 &lt;= length of string &lt;= 1000","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<string.h>\nusing namespace std;\n\n\nvoid String_compression01(string str)\n{\n //write your code here \n}\nvoid String_compression02(string str)\n{\n //write your code here\n}\n\nint main()\n{\n string str;\n getline(cin,str);\n String_compression01(str);\n String_compression02(str);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static String compression1(String str){\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static String compression2(String str){\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tString str = scn.next();\r\n\t\tSystem.out.println(compression1(str));\r\n\t\tSystem.out.println(compression2(str));\r\n\t}\r\n\r\n}"},"python":{"code":"def String_compression01(st):\n #write your code here\n \ndef String_compression02(st):\n #write your code here\n\n\nst=input()\nString_compression01(st)\nString_compression02(st)"}},"points":10,"difficulty":"easy","sampleInput":"wwwwaaadexxxxxx","sampleOutput":"wadex\r\nw4a3dex6","questionVideo":"https://www.youtube.com/embed/6W1MDK7nPZs?end=61","hints":[],"associated":[{"id":"118d44e5-41d6-4b5e-9f7a-779bda18d10b","name":"What is the correct first compression of string \"aabbccddeeeefg\"?","slug":"what-is-the-correct-first-compression-of-string-aabbccddeeeefg","type":4},{"id":"39322559-a3f1-4aa3-96f9-24cd77fb8ca9","name":"What is the correct second compression of the string \"abccdddeffffghhiiii\"?","slug":"what-is-the-correct-second-compression-of-the-string-abccdddeffffghhiiii","type":4},{"id":"3efdc5f7-700c-4865-8c82-a2f364e82406","name":"What should you use for this question?","slug":"what-should-you-use-for-this-question","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"28c4c414-93fc-4aa0-84dc-1c7e08313cdc","name":"String, Dynamic String and Dynamic Array","slug":"string-dynamic-string-and-dynamic-array","type":0},{"id":"8d70323f-f00c-4b50-ad47-070a292e27f3","name":"String Compression","slug":"string-compression","type":1}],"next":{"id":"0ed3378e-43f8-4ea1-b5d6-ab09ea8397e6","name":"Toggle Optica Document","type":1,"slug":"toggle-optica-document"},"prev":{"id":"c074693d-bc3c-46ce-a318-a2ecfb2ece49","name":"Print All Palindromic Substrings","type":1,"slug":"print-all-palindromic-substrings"}}}

String Compression

1. You are given a string. 2. You have to compress the given string in the following two ways - First compression -> The string should be compressed such that consecutive duplicates of characters are replaced with a single character. For "aaabbccdee", the compressed string will be "abcde". Second compression -> The string should be compressed such that consecutive duplicates of characters are replaced with the character and followed by the number of consecutive duplicates. For "aaabbccdee", the compressed string will be "a3b2c2de2".

{"id":"4e627e33-af41-4161-a9c0-0ce06b313137","name":"String Compression","description":"1. You are given a string. \r\n2. You have to compress the given string in the following two ways - \r\n First compression -> The string should be compressed such that consecutive duplicates of characters are replaced with a single character.\r\n For \"aaabbccdee\", the compressed string will be \"abcde\".\r\n Second compression -> The string should be compressed such that consecutive duplicates of characters are replaced with the character and followed by the number of consecutive duplicates.\r\n For \"aaabbccdee\", the compressed string will be \"a3b2c2de2\".","inputFormat":"A String","outputFormat":"Two strings representing first compressed string and second compressed string respectively.","constraints":"1 &lt;= length of string &lt;= 1000","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<string.h>\nusing namespace std;\n\n\nvoid String_compression01(string str)\n{\n //write your code here \n}\nvoid String_compression02(string str)\n{\n //write your code here\n}\n\nint main()\n{\n string str;\n getline(cin,str);\n String_compression01(str);\n String_compression02(str);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static String compression1(String str){\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static String compression2(String str){\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tString str = scn.next();\r\n\t\tSystem.out.println(compression1(str));\r\n\t\tSystem.out.println(compression2(str));\r\n\t}\r\n\r\n}"},"python":{"code":"def String_compression01(st):\n #write your code here\n \ndef String_compression02(st):\n #write your code here\n\n\nst=input()\nString_compression01(st)\nString_compression02(st)"}},"points":10,"difficulty":"easy","sampleInput":"wwwwaaadexxxxxx","sampleOutput":"wadex\r\nw4a3dex6","questionVideo":"https://www.youtube.com/embed/6W1MDK7nPZs?end=61","hints":[],"associated":[{"id":"118d44e5-41d6-4b5e-9f7a-779bda18d10b","name":"What is the correct first compression of string \"aabbccddeeeefg\"?","slug":"what-is-the-correct-first-compression-of-string-aabbccddeeeefg","type":4},{"id":"39322559-a3f1-4aa3-96f9-24cd77fb8ca9","name":"What is the correct second compression of the string \"abccdddeffffghhiiii\"?","slug":"what-is-the-correct-second-compression-of-the-string-abccdddeffffghhiiii","type":4},{"id":"3efdc5f7-700c-4865-8c82-a2f364e82406","name":"What should you use for this question?","slug":"what-should-you-use-for-this-question","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"28c4c414-93fc-4aa0-84dc-1c7e08313cdc","name":"String, Dynamic String and Dynamic Array","slug":"string-dynamic-string-and-dynamic-array","type":0},{"id":"8d70323f-f00c-4b50-ad47-070a292e27f3","name":"String Compression","slug":"string-compression","type":1}],"next":{"id":"0ed3378e-43f8-4ea1-b5d6-ab09ea8397e6","name":"Toggle Optica Document","type":1,"slug":"toggle-optica-document"},"prev":{"id":"c074693d-bc3c-46ce-a318-a2ecfb2ece49","name":"Print All Palindromic Substrings","type":1,"slug":"print-all-palindromic-substrings"}}}
plane

Editor


Loading...

String Compression

easy

1. You are given a string. 2. You have to compress the given string in the following two ways - First compression -> The string should be compressed such that consecutive duplicates of characters are replaced with a single character. For "aaabbccdee", the compressed string will be "abcde". Second compression -> The string should be compressed such that consecutive duplicates of characters are replaced with the character and followed by the number of consecutive duplicates. For "aaabbccdee", the compressed string will be "a3b2c2de2".

Constraints

1 <= length of string <= 1000

Format

Input

A String

Output

Two strings representing first compressed string and second compressed string respectively.

Example

Sample Input

wwwwaaadexxxxxx

Sample Output

wadex w4a3dex6

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode