Interview Question
Country: United States
If chars repeat more than twice, the output String will have the character more than once.
input : AAAa
output : AA
Since the chars.add of 'A' will be false twice in the above case, StringBuilder append will be run twice for the same 'A' character.
We should use Set to store the result String.
I hope this will work, Written in C#, I do not know anything about time& space complexity.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AlgorithmPractice
{
class Program
{
static void Main(string[] args)
{
string input = "AABBCcDdww";
char[] data = input.ToCharArray();
char[] charHolder = new char[52];
int i = 0;
foreach (char item in data)
{
if (charHolder.Contains(item))
Console.Write(item);
else
charHolder[i++] = item;
}
Console.ReadLine();
}
}
}
we can have two pointers here _lastchar and _newChar
char[] charArray = input.ToCharArray();
List<char> list = new List<char>();
char _lastChar = charArray[0];
for (int i = 1; i < charArray.Length; i++)
{
char _newchar = charArray[i];
if (_newchar != _lastChar)
{
if (((int)_newchar - (int)_lastChar) == 32 || ((int)_newchar - (int)_lastChar) == -32)
{
_lastChar = _newchar;
for (int j = i; j < charArray.Length; j++)
{
if(charArray[j]!=_lastChar && !list.Contains(_lastChar))
{
i = j;
_lastChar = charArray[j];
break;
}
}
}
else
{
list.Add(_lastChar);
_lastChar = _newchar;
}
}
else if (i == charArray.Length-1 && _newchar == _lastChar)
{
list.Add(_lastChar);
}
}
char[] charArray = input.ToCharArray();
List<char> list = new List<char>();
char _lastChar = charArray[0];
for (int i = 1; i < charArray.Length; i++)
{
char _newchar = charArray[i];
if (_newchar != _lastChar)
{
if (((int)_newchar - (int)_lastChar) == 32 || ((int)_newchar - (int)_lastChar) == -32)
{
_lastChar = _newchar;
for (int j = i; j < charArray.Length; j++)
{
if(charArray[j]!=_lastChar && !list.Contains(_lastChar))
{
i = j;
_lastChar = charArray[j];
break;
}
}
}
else
{
list.Add(_lastChar);
_lastChar = _newchar;
}
}
else if (i == charArray.Length-1 && _newchar == _lastChar)
{
list.Add(_lastChar);
}
}
char[] charArray = input.ToCharArray();
List<char> list = new List<char>();
char _lastChar = charArray[0];
for (int i = 1; i < charArray.Length; i++)
{
char _newchar = charArray[i];
if (_newchar != _lastChar)
{
if (((int)_newchar - (int)_lastChar) == 32 || ((int)_newchar - (int)_lastChar) == -32)
{
_lastChar = _newchar;
for (int j = i; j < charArray.Length; j++)
{
if(charArray[j]!=_lastChar && !list.Contains(_lastChar))
{
i = j;
_lastChar = charArray[j];
break;
}
}
}
else
{
list.Add(_lastChar);
_lastChar = _newchar;
}
}
else if (i == charArray.Length-1 && _newchar == _lastChar)
{
list.Add(_lastChar);
}
}
import java.util.ArrayList;
import java.util.HashMap;
public class e3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
String s = "AABBCcDdww";
char[] c = s.toCharArray();
ArrayList<Character> a = new ArrayList<>();
for(char x : c){
if(a.contains(x)){
sb.append(x);
}
else{
a.add(x);
}
}
System.out.println(sb.toString());
}
}
this one works in c++
don't know the speed but its using the maps indexing instead of additional loops so I imagine it's fairly quick.
string FindDuplicateChars(string str)
{
hash_map<char, int> dupMap;
string dups;
// loop through the string
for(auto const& curChar : str)
{
// if emplace fails for first time (== 0)
if(!dupMap.emplace(curChar, 0).second && dupMap[curChar] == 0)
{
// add curChar to 'unique' string and change dupMap value
dups += string(&curChar, sizeof(char));
dupMap[curChar] = 1;
}
}
return dups;
}
this one works in c++
don't know the speed but its using the maps indexing instead of additional loops so I imagine it's fairly quick.
string FindDuplicateChars(string str)
{
hash_map<char, int> dupMap;
string dups;
// loop through the string
for(auto const& curChar : str)
{
// if emplace fails for first time (== 0)
if(!dupMap.emplace(curChar, 0).second && dupMap[curChar] == 0)
{
// add curChar to 'unique' string and change dupMap value
dups += string(&curChar, sizeof(char));
dupMap[curChar] = 1;
}
}
return dups;
}
In this solution I expect that duplicates could be anywhere. Linear complexity:
- iKrumping September 26, 2015