Time Given - 1 hrs
Number of Questions - 2
For each word in a list of words, if any two adjacent characters are equal, change one of them. Determine the minimum number of substitutions so final string contains no adjacent equal characters.
words = ['add','boook','break']
The return array is [1,1,0]
public static List<Integer> minimalOperations(List<String> words)
{
List<Integer> res = new ArrayList();
if(words.isEmpty()) return res;
for(String word: words)
{
int count = 0;
char prev = word.charAt(0);
for(int i=1; i<word.length(); i++)
{
char curr = word.charAt(i);
if(curr == prev)
{
prev = '-';
count++;
}
else{
prev = curr;
}
}
res.add(count);
}
return res;
}The above solution passed all test cases.
Given an integer n, determine the following:
n = 37
For the value n = 37 Binary representation is 100101
There are three one bits located at the respective 1st, 4th and 6th positions (From left to Right).
The return answer is
3
1
4
6
Solution
public static List<Integer> getOneBits(int n)
{
StringBuilder str = new StringBuilder();
while (n > 0) {
int num = n & 1;
str.append(num);
n >>= 1;
}
List<Integer> res = new ArrayList();
int count = 1;
for(char c : str.reverse().toString().toCharArray())
{
if(c == '1') res.add(count);
count++;
}
return res;
}The above solution passed all test cases too.