The problem of the Day

LeetCode + GeeksforGeeks { 08–09–2021 }

Akash Singh
2 min readSep 8, 2021

--

Shifting Letters: —

Input: s = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation: We start with "abc".
After shifting the first 1 letters of s by 3, we have "dbc".
After shifting the first 2 letters of s by 5, we have "igc".
After shifting the first 3 letters of s by 9, we have "rpl", the answer.

CODE: →

string shiftingLetters(string S, vector<int>& shifts) 
{
for(int i = shifts.size() - 2 ;i >=0; i--)
shifts[i] = (shifts[i] + shifts[i + 1]) % 26;
for(int i = 0; i < S.size(); i++)
S[i] = ( ((S[i] - 'a') + shifts[i]) % 26 + 'a');
return S;
}

Detect Loop in linked list: —

Input:
N = 4
value[] = {3,2,0,-4}
x = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 2nd node (1-indexed).

Input:
N = 3
value[] = {1,3,4}
x = 2
Output: true
Explanation:
In above test case N = 3.
The linked list with nodes N = 3 is given. Then value of x=2 is given which means last node is connected with xth node of linked list. Therefore, there exists a loop.

CODE: →

bool detectLoop(Node* head)    
{
// your code here
unordered_set<Node*> s;
while (head != NULL)
{
if (s.find(head) != s.end())
return true;

s.insert(head);
head = head->next;
}
return false;
}
bool detectLoop(Node* head)    
{
// your code here
Node* slow = head;
Node* fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}

Don’t forget to give us your 👏 !

Don’t forget to give us your 👏 !

--

--