Interview Question
Country: United States
I think calculating the value of d = Chracter.getNumericValue(c[I]) every time is not necessary so better you calculate only when the isDigit is true.
Thanks vinod.
private static void unCompressString(String s) {
char[] c = s.toCharArray();
StringBuffer str = new StringBuffer();
for (int i = 0; i < c.length; i++) {
if(Character.isDigit(c[i]))
{
int d=Character.getNumericValue(c[i]);
for(int j=0;j<d;j++)
{
str.append(c[i-1]);
}
}
}
System.out.println(str);
}
public static String uncompressString(String source) {
String[] strArray = source.split("\\d+");
String[] numberArray = source.split("\\p{Alpha}");
StringBuilder sb = new StringBuilder();
for (int index = 0; index < Math.min(strArray.length, numberArray.length); index++) {
for (int looper = 1; looper <= Integer.valueOf(numberArray[index + 1]); looper++)
sb.append(strArray[index]);
}
return sb.toString();
}
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
char c,S[101];
int a;
cin>>S;
for (int i = 0; i < strlen(S); ++i)
{
if(isdigit(S[i]))
{
c = S[i-1];
a = S[i] - '0';
for (int i = 0; i < a; ++i)
{
cout<<c;
}
}
}
return 0;
}
public static String stringBuilder(String inputString) {
if(inputString.length() == 0) {
return "";
}
StringBuffer resultString = new StringBuffer();
Character currentChar = null;
String currentNum = "0";
for (int i = 0; i < inputString.length(); i++) {
if (Character.isLetter(inputString.charAt(i))) {
appendCharacters(currentChar, Integer.parseInt(currentNum), resultString);
currentChar = inputString.charAt(i);
currentNum = "";
} else {
currentNum = currentNum + inputString.charAt(i);
if(i == inputString.length() -1 ) {
appendCharacters(currentChar, Integer.parseInt(currentNum), resultString);
}
}
}
return resultString.toString();
}
private static StringBuffer appendCharacters(Character ch, Integer count,
StringBuffer resultString) {
for (int i = 0; i < count; i++) {
resultString.append(ch);
}
return resultString;
}
public static String stringBuilder(String inputString) {
if(inputString.length() == 0) {
return "";
}
StringBuffer resultString = new StringBuffer();
Character currentChar = null;
String currentNum = "0";
for (int i = 0; i < inputString.length(); i++) {
if (Character.isLetter(inputString.charAt(i))) {
appendCharacters(currentChar, Integer.parseInt(currentNum), resultString);
currentChar = inputString.charAt(i);
currentNum = "";
} else {
currentNum = currentNum + inputString.charAt(i);
if(i == inputString.length() -1 ) {
appendCharacters(currentChar, Integer.parseInt(currentNum), resultString);
}
}
}
return resultString.toString();
}
private static StringBuffer appendCharacters(Character ch, Integer count,
StringBuffer resultString) {
for (int i = 0; i < count; i++) {
resultString.append(ch);
}
return resultString;
}
import java.util.Stack;
public class StringExpand {
public static void main(String [] args) {
Stack<Integer> stack = new Stack<>();
String str = "a10b3c0d1";
StringBuilder st = new StringBuilder();
int len = str.length();
char ch = 0;
for (int i =0; i< len;i++) {
if (isDigit(str.charAt(i))) {
int val = Integer.parseInt(String.valueOf(str.charAt(i)));
stack.push(val);
} else {
createString(stack, st, ch);
ch = str.charAt(i);
}
}
//fill up the last element
createString(stack, st, ch);
System.out.println(st.toString());
}
private static void createString(Stack<Integer> stack, StringBuilder st, char ch) {
if(!stack.isEmpty()) {
int actual = getActualValue(stack);
for (int j=0;j<actual;j++){
st.append(ch);
}
}
}
private static int getActualValue(Stack<Integer> stack) {
int multiplier = 1;
int actual = 0;
while(!stack.isEmpty()) {
actual = actual + stack.pop()*multiplier;
multiplier = multiplier*10;
}
return actual;
}
private static boolean isDigit(int ch) {
if (ch > 47 && ch < 58) {
return true;
}
return false;
}
}
vector<string> convert (char arr []){
vector <string> converted,words;
vector <int> numbers;
string c;
int d,t=0,length=0;
for (int i = 0; i < 7; i++){
if (isdigit(arr[i])){
d = (arr[i]-'0') + d*t;
t += 10;
words.push_back(c);
c.clear();
}
else{
c += arr[i];
if (d > 0){
numbers.push_back(d);
d = 0;
t = 0;
}
}
}
numbers.push_back(d);
for (int i = 0; i < numbers.size(); i ++){
for (int j = 0; j < numbers.at(i); j ++){
converted.push_back(words.at(i));
}
}
return converted;
}
#include <cstdio>
int getNumber (char ** p) {
unsigned number = 0;
while (**p >= '0' && **p <= '9') {
unsigned n = **p - '0';
number = (number * 10) + n;
(*p)++;
}
return number;
}
int main () {
char buf[256];
scanf("%s", buf);
char * p = buf;
while (*p) {
char c = *p++;
unsigned repeat = getNumber(&p);
for (unsigned j = 0; j < repeat; j++)
printf("%c", c);
}
printf("\n");
return 0;
}
#include <cstdio>
int getNumber (char ** p) {
unsigned number = 0;
while (**p >= '0' && **p <= '9') {
unsigned n = **p - '0';
number = (number * 10) + n;
(*p)++;
}
return number;
}
int main () {
char buf[256];
scanf("%s", buf);
char * p = buf;
while (*p) {
char c = *p++;
unsigned repeat = getNumber(&p);
for (unsigned j = 0; j < repeat; j++)
printf("%c", c);
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <string.h>
void add_char(int count,char *m);
int main()
{
char *p = "a10";
char *m = p;
int count = 0;
while(*p!='\0')
{
if(isdigit(*p))
{
count = count + atoi(p);
}
else
{
count += 1;
}
p++;
}
add_char(count,m);
return 0;
}
void add_char(int count,char *m)
{
char chr[count],cr;
int i = 0,j =0,cnt;;
while(i < count)
{
cnt=0;
if(!isdigit(*m)) {
cr = *m;
chr[i] = *m;
i++;
}
else if(isdigit(*m))
{
cnt = atoi(m);
for(j=0;j<cnt-1;j++){
chr[i] = cr;
i++;
}
}
m++;
}
printf("new chr arry= %s\n",chr);
}
#include <stdio.h>
#include <string.h>
void add_char(int count,char *m);
int main()
{
char *p = "a10";
char *m = p;
int count = 0;
while(*p!='\0')
{
if(isdigit(*p))
{
count = count + atoi(p);
}
else
{
count += 1;
}
p++;
}
add_char(count,m);
return 0;
}
void add_char(int count,char *m)
{
char chr[count],cr;
int i = 0,j =0,cnt;;
while(i < count)
{
cnt=0;
if(!isdigit(*m)) {
cr = *m;
chr[i] = *m;
i++;
}
else if(isdigit(*m))
{
cnt = atoi(m);
for(j=0;j<cnt-1;j++){
chr[i] = cr;
i++;
}
}
m++;
}
printf("new chr arry= %s\n",chr);
}
#include <stdio.h>
#include <string.h>
void add_char(int count,char *m);
int main()
{
char *p = "a10";
char *m = p;
int count = 0;
while(*p!='\0')
{
if(isdigit(*p))
{
count = count + atoi(p);
}
else
{
count += 1;
}
p++;
}
add_char(count,m);
return 0;
}
void add_char(int count,char *m)
{
char chr[count],cr;
int i = 0,j =0,cnt;;
while(i < count)
{
cnt=0;
if(!isdigit(*m)) {
cr = *m;
chr[i] = *m;
i++;
}
else if(isdigit(*m))
{
cnt = atoi(m);
for(j=0;j<cnt-1;j++){
chr[i] = cr;
i++;
}
}
m++;
}
printf("new chr arry= %s\n",chr);
}
#include <stdio.h>
#include <string.h>
void add_char(int count,char *m);
int main()
{
char *p = "a10";
char *m = p;
int count = 0;
while(*p!='\0')
{
if(isdigit(*p))
{
count = count + atoi(p);
}
else
{
count += 1;
}
p++;
}
add_char(count,m);
return 0;
}
void add_char(int count,char *m)
{
char chr[count],cr;
int i = 0,j =0,cnt;;
while(i < count)
{
cnt=0;
if(!isdigit(*m)) {
cr = *m;
chr[i] = *m;
i++;
}
else if(isdigit(*m))
{
cnt = atoi(m);
for(j=0;j<cnt-1;j++){
chr[i] = cr;
i++;
}
}
m++;
}
printf("new chr arry= %s\n",chr);
}
public static String uncompressString(String source) {
String[] strArray = source.split("\\d+");
String[] numberArray = source.split("\\p{Alpha}");
StringBuilder sb = new StringBuilder();
for (int index = 0; index < Math.min(strArray.length, numberArray.length); index++) {
for (int looper = 1; looper <= Integer.valueOf(numberArray[index + 1]); looper++)
sb.append(strArray[index]);
}
return sb.toString();
}
public static String uncompressString(String source) {
String[] strArray = source.split("\\d+");
String[] numberArray = source.split("\\p{Alpha}");
StringBuilder sb = new StringBuilder();
for (int index = 0; index < Math.min(strArray.length, numberArray.length); index++) {
for (int looper = 1; looper <= Integer.valueOf(numberArray[index + 1]); looper++)
sb.append(strArray[index]);
}
return sb.toString();
}
public class careercup1 {
public static void main(String main[]){
String str="a1b2c3";
System.out.println(str);
String str1="";
int i = 0;
while(i<str.length()){
System.out.println(str.charAt(i));
System.out.println(str.charAt(i+1));
char c=str.charAt(i+1);
int ct= Character.getNumericValue(c);
System.out.println(ct);
for(int j=0;j<ct;j++)
str1+=str.charAt(i);
i+=2;
}
System.out.println(str1);
}
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
bool isAlphabet(char a);
int main()
{
// cout << "Hello World" << endl;
// char *testString = "a1b2c3d4";
//test string 1
// char *testString = "a10";
//test string 2
// char *testString = "a10b20";
//test string 3
//char *testString = "abc";
//test string 4
char *testString = "1234";
int nLength = strlen(testString);
for ( int index = 0; index < nLength; )
{
char toPrint;
int occurence = 0;
if( isAlphabet(testString[index]))
{
toPrint = testString[index];
index++;
//this will hold the string to count the number of time the letter will be printed
char *length = new char[10];
//count the occurence of the alphabet
int ocurInd = 0;
while( !isAlphabet(testString[index]))
{
length[ocurInd++] = testString[index];
index++; //Move to next character to read the occurence
}
length[ocurInd] = '\0';
//get the value in integer ising built in function
occurence = atoi(length) ;
//print the character
for(int i=0; i<occurence; i++)
std::cout<<toPrint;
}
else
{
index++;
}
}
return 0;
}
bool isAlphabet(char a)
{
if(a >= 'a' && a <= 'z')
return true;
else
return false;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
bool isAlphabet(char a);
int main()
{
// cout << "Hello World" << endl;
// char *testString = "a1b2c3d4";
//test string 1
// char *testString = "a10";
//test string 2
// char *testString = "a10b20";
//test string 3
//char *testString = "abc";
//test string 4
char *testString = "1234";
int nLength = strlen(testString);
for ( int index = 0; index < nLength; )
{
char toPrint;
int occurence = 0;
if( isAlphabet(testString[index]))
{
toPrint = testString[index];
index++;
//this will hold the string to count the number of time the letter will be printed
char *length = new char[10];
//count the occurence of the alphabet
int ocurInd = 0;
while( !isAlphabet(testString[index]))
{
length[ocurInd++] = testString[index];
index++; //Move to next character to read the occurence
}
length[ocurInd] = '\0';
//get the value in integer ising built in function
occurence = atoi(length) ;
//print the character
for(int i=0; i<occurence; i++)
std::cout<<toPrint;
}
else
{
index++;
}
}
return 0;
}
bool isAlphabet(char a)
{
if(a >= 'a' && a <= 'z')
return true;
else
return false;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
bool isAlphabet(char a);
int main()
{
// cout << "Hello World" << endl;
// char *testString = "a1b2c3d4";
//test string 1
// char *testString = "a10";
//test string 2
// char *testString = "a10b20";
//test string 3
//char *testString = "abc";
//test string 4
char *testString = "1234";
int nLength = strlen(testString);
for ( int index = 0; index < nLength; )
{
char toPrint;
int occurence = 0;
if( isAlphabet(testString[index]))
{
toPrint = testString[index];
index++;
//this will hold the string to count the number of time the letter will be printed
char *length = new char[10];
//count the occurence of the alphabet
int ocurInd = 0;
while( !isAlphabet(testString[index]))
{
length[ocurInd++] = testString[index];
index++; //Move to next character to read the occurence
}
length[ocurInd] = '\0';
//get the value in integer ising built in function
occurence = atoi(length) ;
//print the character
for(int i=0; i<occurence; i++)
std::cout<<toPrint;
}
else
{
index++;
}
}
return 0;
}
bool isAlphabet(char a)
{
if(a >= 'a' && a <= 'z')
return true;
else
return false;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
bool isAlphabet(char a);
int main()
{
// cout << "Hello World" << endl;
char *testString = "a1b2c3d4";
//test string 1
// char *testString = "a10";
//test string 2
// char *testString = "a10b20";
//test string 3
//char *testString = "abc";
//test string 4
// char *testString = "1234";
int nLength = strlen(testString);
for ( int index = 0; index < nLength; )
{
char toPrint;
int occurence = 0;
if( isAlphabet(testString[index]))
{
toPrint = testString[index];
index++;
//this will hold the string to count the number of time the letter will be printed
char *length = new char[10];
//count the occurence of the alphabet
int ocurInd = 0;
while( !isAlphabet(testString[index]))
{
length[ocurInd++] = testString[index];
index++; //Move to next character to read the occurence
}
length[ocurInd] = '\0';
//get the value in integer ising built in function
occurence = atoi(length) ;
//print the character
for(int i=0; i<occurence; i++)
std::cout<<toPrint;
}
else
{
index++;
}
}
return 0;
}
bool isAlphabet(char a)
{
if(a >= 'a' && a <= 'z')
return true;
else
return false;
}
private static void generateOutput(String input) {
StringBuffer output = new StringBuffer();
for(int i=1;i<input.length();i+=2){
char alphabet = input.charAt(i-1);
int count = Character.getNumericValue(input.charAt(i));
for(int j=0;j<count;j++){
output.append(alphabet);
}
}
System.out.println(output);
}
private static void generateOutput(String input) {
StringBuffer output = new StringBuffer();
for(int i=1;i<input.length();i+=2){
char alphabet = input.charAt(i-1);
int count = Character.getNumericValue(input.charAt(i));
for(int j=0;j<count;j++){
output.append(alphabet);
}
}
System.out.println(output);
}
We need to clarify certain things about the questions.
Will there be a sequence of one char followed by some number.
Assuming number can be bigger then 9 and smaller to 0
Based on this I have written a C++ code of O(n).
Also assuming given buffer have enough space to accommodate extended string
Idea is first calculate how much extra space will be required to expand the string.
There iterate from end to start, and keep track of Expanded End index
For each char find the length of chars to be expanded and then put it as end by Expanded End index. Continue this till you finish doing for all chars
bool IsDigit(char aChar);
// we have given char array like 'a1b2c3' we have to convert this array to array like this 'abbccc'
// Assumption there is digit after each char
// assupmtion there is enough space in the array
int ExpandCharByNumber(char s[], int length)
{
if (s == NULL)
{
return 0;
}
int ExpandedLength = 0;
int index = 0;
while (index < length && s[index] != NULL)
{
if (IsDigit(s[index]))
{
int unit = 1;
int value = (s[index++] - '0') * unit;
ExpandedLength--; // Extra space released by longer digit
unit *= 10;
while (IsDigit(s[index]))
{
value = value * unit + (s[index++] - '0');
unit *= 10;
ExpandedLength--; // Extra space released by longer digit
}
ExpandedLength += value - 1; // -1 for one space released by the char
}
else {
index++;
}
}
int endIndex = index + ExpandedLength;
int retValue = endIndex;
s[endIndex--] = NULL;
index--;
// We will move backward from Extra length, use index to know what to expand
while (endIndex > 0 && index > 0)
{
if (IsDigit(s[index]))
{
int unit = 1;
int value = (s[index--] - '0') * unit;
unit *= 10;
while (IsDigit(s[index]))
{
value = value + (s[index--] - '0') * unit;
unit *= 10;
}
for (int backIndex = value; backIndex > 0; backIndex--)
{
s[endIndex--] = s[index];
}
}
index--;
}
return retValue;
}
bool IsDigit(char aChar)
{
return aChar >= '0' && aChar <= '9';
}
int _tmain(int argc, _TCHAR* argv[])
{
/* Test Driver for ExpandCharByNumber */
char anString[250] = "a2b3c1";
int size = ExpandCharByNumber(anString, 250);
PrintArray(anString, size);
char anString1[250] = "a0b4c0";
size = ExpandCharByNumber(anString1, 250);
PrintArray(anString1, size);
char anString2[250] = "a0b0c0";
size = ExpandCharByNumber(anString2, 250);
PrintArray(anString2, size);
return 0;
}
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int isNum(char ch)
{
if(ch>=48 && ch<=57)
return 1;
else
return 0;
}
int StrToNum(char s[],int strt,int end)
{
int i,tmp,res=0;
for(i=strt;i<=end;i++)
{
tmp=s[i]-'0';
res = res*10+tmp;
}
return res;
}
int main()
{
int p,q,ctr,i,x;
char ch,str[105];
//char ch,str[105]="a1b12c3d11e5g2";
cin>>str;
for(i=0;i<strlen(str);i++)
{
if(isNum(str[i]))
{
ch=str[i-1];
p=i;
while(isNum(str[i]))
i++;
q=i-1;
ctr = StrToNum(str,p,q);
for(x=1;x<=ctr;x++)
cout<<ch;
i--;
}
}
return 0;
}
public class myfirst {
private static char[] a1b2c3d4;
public static void main(String args[])
{
String str="a1b2c3";
char[] chararray = str.toCharArray();
for(int i=0;i<=str.length();i++)
{
if(Character.isDigit(chararray[i]))
{
int p=0;
int z=Character.getNumericValue(chararray[i]);
char l=chararray[i-1];
while(p<z)
{
System.out.print(l);
p++;
}
} else {
}
}
public class myfirst {
private static char[] a1b2c3d4;
public static void main(String args[])
{
String str="a1b2c3";
char[] chararray = str.toCharArray();
for(int i=0;i<=str.length();i++)
{
if(Character.isDigit(chararray[i]))
{
int p=0;
int z=Character.getNumericValue(chararray[i]);
char l=chararray[i-1];
while(p<z)
{
System.out.print(l);
p++;
}
} else {
}
}
public static String expand(String str){
int loc = 0;
StringBuilder output = new StringBuilder();
while(loc < str.length()){
//get the next character to repeat
char c= str.charAt(loc);
loc++;
//get the next number
int num = 0;
while(loc < str.length){
char n = str.charAt(loc);
if(isDigit(n)){
num = num * 10 + n;
}
}
//add character to output
while(num > 0){
output.append(c);
num --;
}
}
return output.toString();
}
private static boolean isDigit(char c){
int asciiOffset = (int)c - (int)'1';
return asciiOffset > -1 && asciiOffset < 10;
}
package GoogleAlgo;
import java.util.ArrayList;
import java.util.Scanner;
public class Array1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
ArrayList arraylist = new ArrayList();
int i;
for(i=0;i < s.length();i++)
{
if (Character.isDigit(s.charAt(i)))
{
for(int j = 0; j < (s.charAt(i)-48); j++)
{
arraylist.add(s.charAt(i-1));
}
}
}
System.out.println(arraylist);
}
}
- Vir Pratap Uttam May 25, 2015