comparing characters in string MIPS - string

there I'm currently trying to write a MIPS program that will sort the user inputted String and Bubble sort it. A being the in the front and Z last.
Right now I'm kind of confused on how I can compare each individual character in the string. So for example:
String: Stackoverflow
Compare S and T the first two letters. Since S is belongs in the front it stays and no swap happens.
How would I go about moving onto the next set of characters to compare so T and A would be the next set to compare.
I think I would use the lb (load byte) instruction but I'm not entirely sure of to use the offset correctly.
Thanks for the help.

Just as a reminder, in the loop, you must check whether the current pointer is > than the index (base pointer + length of the string - 1), or you can also check the value at index (current pointer + 1) equals to 0 (NUL) string terminating character or not.
Make sure you keep a copy of the base pointer somewhere (in register or memory).
In each loop, you will read the character currently pointed to by the current pointer by load byte at current pointer with offset 0, and read the next character by load byte at current pointer with offset 1. Then you can do the comparison and swapping. After that, you increase the current pointer by 1 (since a character in ASCII is 1 byte, you will increase the address by 1 byte only).

Related

Maximum number of consecutive 1's in a string

A string of length N (can be upto 10^5) is given which consists of only 0 and 1. We have to remove two substrings of length exactly K from the original string to maximize the number of consecutive 1's.
For example suppose the string is 1100110001and K=1.
So we can remove two substrings of length 1. The best possible option here is to remove the 0's at 3rd place and 4th place and get the output as 4 (as the new string will be 11110001)
If I try brute force it'll timeout for sure. I don't know if sliding window will work or not. Can anyone give me any hint on how to proceed? I am not demanding the full answer obviously, just some hints will work for me. Thanks in advance :)
This has a pretty straightforward dynamic programming solution.
For each index i, calculate:
The length of the sequence of 1s that immediately precedes it, if nothing has been removed;
The longest sequence of 1s that could immediately precede it, if exactly one substring is removed before it; and
The longest sequence of 1s that could immediately precede it, if exactly two substrings are removed before it.
For each index, these three values are easily calculated in constant time from the values for earlier indexes, so you can do this in a single pass in O(N) time.
For example, let BEST(i,r) be the best length immediately preceding position i after removing r substrings. If i >= K, then you can remove a substring ending at i and have BEST(i,r) = BEST(i-K,r-1) for r > 0. If string[i-1] = '1' then you could extend the sequence from the previous position and have BEST(i,r) = BEST(i-1,r)+1. Choose the best possibility for each i,r.
The largest value you find in step (3) is the answer.

duplicate each space in a given string

I found the following question in many interviews (not my interview).
given a string, you need to replace each space with 2 spaces.
you may assume that your string has enough place for adding the required spaces.
you need to do it in place, memory allocation is not allowed.
I don't understand how to implement this without override letters.
There's not a lot of context in your question. Let's assume it's a programming interview and you are dealing with a low level language like C or assembler. Let's also assume that the string has a count and/or ends in a null, like 'this is a string\0\0\0\0'
I would scan the string from beginning to end and count the spaces, let's call that C. Then I would work backward through the string on character at a time moving each character forward by C positions. Each time a space is encountered, copy the space forward by C positions, subtract one from C, and then move the space by C positions. Stop when C is 0.
Here, nulls/unused are represented by a period.
this is a string.... C=3
this is a string..g.
this is a string.ng.
this is a stringing.
this is a strinring.
this is a stritring.
this is a strstring.
this is a st string.
this is a s string. C=2
this is a a string.
this is a string.
this iss a string. C=1
this iis a string.
this is a string. C=0
Shifting the string following a found space is required by one letter. To reduce time needed to reduce the shifting part, I would use this approach:
Count the number of space. I will call this count c.
Shift the string to the number of spaces to the right (I'm assuming here a left to right reading direction.)
Start a loop starting at offset c until the end of the string:
Initialize a counter for the already duplicated space, called s, with 0
Copy the letter at current position to current position - c + s
If the letter was a space, increment s and add a space to position - c + 1
Not sure if all the offset are correctly calculated in my mind, correct it if needed. But because this is just an interview question to idea is just to sketch a correct algorithm.

How to find the first index in a string which is not a specific char

I have a phone number which i want to strip it's leading zeros.
What is the best way to find the first digit which is not 0? (input is a string)
you can simply create a for-loop that runs from 0 - ( insert string.length() or whatever it is, I'm not a node.js programmer) and check using an if-expression if the character at the index is a zero, then simply write the index out if the character is not a zero.

Why strings cannot be indexed by integer values

I learned that Swift strings cannot be indexed by integer values. I remembered it and I use the rule. But I've never fully understood the mechanic behind it.
The explanation of from the official document is as follows
"Different characters can require different amounts of memory to store, so in order to determine which Character is at a particular position, you must iterate over each Unicode scalar from the start or end of that String. For this reason, Swift strings cannot be indexed by integer values"
I've read it several times, I still don't quite get the point. Can someone explain me a bit more why Swift String cannot be indexed by integer values?
Many Thanks
A string is stored in memory as an array of bytes.
A given character can require 1 to 4 bytes for the basic codepoint, plus any number of combining diacritical mark.
For example, é requires 2 bytes.
Now, if you have the strings efgh and éfgh, to access the second character (f), for the first string, the character is in the byte array at index 1, for the second string, it is at index 2.
In order to know that, you need to inspect the first character. For accessing any character based on its index, you need to go through all the previous characters to know how many bytes each takes.

Finding Substring of Number with certain property

I am given a string consisting of only numbers from 0 to 9. I want to calculate how many sub strings of them are power of 2.
For example for substring 2560616 substring 256 and 16 are power of 2. I need to calculate how many such substrings are there in any given substring.
Note that the substring is very large so brute force can't work. So I mainly want to address 2 issues
How to efficiently count all substrings that are power of 2
How to efficiently calculate whether a substring is power of 2
I think there might be a DP approach, but I am not sure about it.
Create a tree from the digits of the powers of 2 with the following algorithm:
Start with a root representing the empty character.
Get the next power of 2, get its digits in reverse order.
Select the root. Select the last digit of the current number.
Go to the child node of the selected node corresponding to the selected digit. If it does not exists yet, create it.
Select the previous digit of the current number. Repeat from (2.) until there are no more digits.
Mark the current node as a valid endpoint.
Repeat from (2.) until number of digits > 10^5
This tree might take a couple GBs in the memory.
Now you have your tree. To count the number of substrings that are power of 2, do the following:
Start from the end of the string.
Get the previous character.
Select the root of the tree.
Select the previous character (starting with the one selected in the outer (2.)).
Select the child node of the selected node corresponding to the selected digit.
If the selected child node is marked as valid endpoint, increment count by 1.
Repeat from (2.) until selected node is null or reached first character of the string.
Go back to character selected in outer (2.)
Select previous character. Repeat from (2.) until reached beginning of string.
The description of the algorithm is not "exam-ready", but i hope its understandable enough.

Resources