## Strings and Lcp

We are given a string S with |S|=N
We have to answer Q queries
Each query has an integer x as input
We have to report
$$\sum_{i=x}^n LCP(i,x)$$
Where Lcp(i,x) denotes the length of the largest common prefix of suffix[i,N] and suffix[x,N]

## Strings and suffix

How can we find the sum of sizes of common prefix that a suffix of length r has with all suffices of length greater than r? We have to find this answer for all r. I tried this using suffix trees and got an O(n^2) algorithm where n is the length of the string. The main problem I could not handle was the restriction that only suffice of length greater than r were to be considered.

## Strings in python

How to find a specified letter follows an another letter or not. eg; the input is : ‘hi, how are you?’,’h’,’i’ then the output is true, because i followed by h at least one time .

## What is the time complexity of sorting n words length wise and then alphabetically? Should we consider the length of the strings in the complexity?

Let’s assume I have a list of some words found in the English dictionary: ["hat", "assume", "prepare", "cat", "ball", "brave", "help" …. ]

I want to sort these words (which are n in number) in a way, such that they are ordered based on their length, but if 2 words have the same length, they are ordered alphabetically.

What is the time complexity of this sorting operation?

Would it be fair to say that the complexity is just O(nlogn) and not take into consideration the length of the strings? If the largest length is S, can the complexity also involve a factor of S?

## What is the expected time complexity of checking equality of two arbitrary strings?

The simple (naive?) answer would be O(n) where n is the length of the shorter string. Because in the worst case you must compare every pair of characters.

So far so good. I think we can all agree that checking equality of two equal length strings requires O(n) runtime.

However many (most?) languages (I’m using Python 3.7) store the lengths of strings to allow for constant time lookups. So in the case of two unequal length strings, you can simply verify len(string_1) != len(string_2) in constant time. You can verify that Python 3 does indeed make this optimization.

Now, if we’re checking the equality of two truly arbitrary strings (of arbitrary length) then it is much more likely (infinitely, I believe) that the strings will be of unequal length than of equal length. Which (statistically) ensures we can nearly always compare them in constant time.

So we can compare two arbitrary strings at O(1) amortized, with a very rare worst-case of O(n). Should we consider strings comparisons then to be O(1) in the same way we consider hash table lookups to be O(1)?

Stack Overflow, and my copy of Cracking the Coding interview cite this operation as O(n).

## Construct a dfa and nfa for a set of strings on{0,1} : the left most symbol differs from the right most one and odd length string

Construct dfa and nfa for a set of string 0,1 such that left most and right most element are different and odd length string Can you plss draw it

## Can an $NDTM$ simultaneously perform a set of operations on all strings of a given length?

Can an $$NDTM$$ perform a set of operations on all strings of a given length $$b$$, at the same time? Aka can it operate on all strings of a given length by doing something like: spawn $$2^b$$ branches then operate on each string of length b on each branch?

How could it do this tho if the branches can’t communicate? That’s what I’m having a hard time with. How does any given branch, if it doesn’t know what strings the other branches are running, know what string to run the operations on (so that all the strings are covered by $$2^b$$ branches)?

## Double Hashing with Strings as key

How would you choose the second hash function with for double hashing with string as key? My first hash function is the scalar product of a random int array with the 16 bit number of each char. Is that a good first hash function? And what should I do for the second function now? hanks in advance.

## AES encryption (in Java) of different JSON strings always produce same encrypted string as result. Why?

I have a program written in Java which takes JSON string as argument, encrypts it using AES then encodes it using Base64. JSON string is like:

{"a": "b"} or {"a": "n"} or {"a": "k"}  

I.e related object would have one property a. Value part is randomly generated.

Program outputs for above JSON inputs looks like

UBNvKoRoGqk0PTQQL5K4Sw== bKwlToSND3HkceDExEDXSw== u/yKJq1FdoifBM+AnadC3A== 

i.e. they are unique.

Same goes for {"a":"gn"} — random string with length 2. Same for 3 and so on.

But starting from 7 program produces the same encoded string for different inputs. I mean following JSON strings taken as input:

{"a": "pzfovvs"} {"a": "bqwuvck"} 

produces same string as output:

Dwg0Xjkot8UBfn+vbcCfOS4KluXB6RCFQ932Y9ABtIg= 

Same goes for length 8 and 9. Starting from 10 results became unique again.

What is the explanation of this strange phenomenon?

(I can post code if needed.)

Ok, here is the code:

import java.security.Key; import java.security.NoSuchAlgorithmException; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.KeyGenerator;  public class JWTEncryptor {  private static String algorithm = "AES"; private static Key key; private static KeyGenerator keyGenerator; private static Cipher cipher;  public static String encrypt(String jwt) throws Exception {     if (key == null || cipher == null) {         setUp();     }     cipher.init(Cipher.ENCRYPT_MODE, key);     return Base64.getEncoder().encodeToString(cipher.doFinal(jwt.getBytes("UTF-8"))); }  private static void setUp() {     try {         cipher = Cipher.getInstance(algorithm);     } catch (Exception e1) {         e1.printStackTrace();     }     if (keyGenerator != null) {         key = keyGenerator.generateKey();         return;     }     try {         keyGenerator = KeyGenerator.getInstance(algorithm);         key = keyGenerator.generateKey();     } catch (NoSuchAlgorithmException e) {         e.printStackTrace();     } }  public static String decrypt(String encryptedJWT) throws Exception {     cipher.init(Cipher.DECRYPT_MODE, key);     return new     String(cipher.doFinal(Base64.getDecoder().decode(encryptedJWT))); }   } 

## Decide whether two strings $x, y$ can be split into substrings $a,b,c$ such that $x=abc$ and $y=cba$

What is the fastest algorithm for the following problem?

Given two strings $$x, y \in \Sigma^*$$ as input, decide whether there exists strings $$a, b, c \in \Sigma^*$$, such that $$x=abc$$ and $$y=cba$$.

By calculating all the length of the longest common prefix of $$s = x y$$ and all suffixes of $$s$$, we can compute all candidates for $$a$$ in $$O(n)$$ time.

For each candidate of $$a$$ with length $$k$$, we now just need to check whether $$x_k, x_{k + 1}, \ldots, x_{n}$$ is a rotation of $$y_0, y_1, \ldots, y_{n – k}$$. This can of course be done in $$O(n)$$.

However checking the rotations naively results in a worst-case $$O(n^2)$$ algorithm.

It seems that using the result from either https://arxiv.org/pdf/1601.08051.pdf or https://arxiv.org/pdf/1311.6235.pdf would make the algorithm run in expected $$O(n)$$ time.

Is there a simpler way of speeding up the rotation checking, where it is still faster than $$O(n^2)$$? Is there a way of making it deterministic, so that it still runs in $$O(n)$$ time?